Skip to content

Commit

Permalink
Reduce complexity of (*Config).create()
Browse files Browse the repository at this point in the history
This also fixes a bug where setupDefaults was erroring if the
directory existed even if the file was missing.

i.e. ~/.circleci was already there
  • Loading branch information
Zachary Scott committed Jun 12, 2018
1 parent 941ee94 commit a150d3e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
8 changes: 3 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"os"

"github.com/circleci/circleci-cli/config"
Expand All @@ -27,9 +26,8 @@ var Logger *logger.Logger

// Config is the current configuration available to all commands in `cmd` package.
var Config = &config.Config{
Verbose: false,
Name: "cli",
DefaultPath: fmt.Sprintf("%s/.circleci/cli.yml", os.Getenv("HOME")),
Verbose: false,
Name: "cli",
}

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -59,7 +57,7 @@ func init() {
func setup() {
Logger = logger.NewLogger(Config.Verbose)
Logger.FatalOnError(
"Failed to setup configuration",
"Failed to setup configuration: ",
Config.Init(),
)
}
54 changes: 32 additions & 22 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import (

// Config is a struct of the current configuration available at runtime.
type Config struct {
Verbose bool
File string
Name string
DefaultPath string
Verbose bool
File string
Name string
}

// Init is called on initialize of the root command.
Expand All @@ -27,7 +26,7 @@ func (c *Config) Init() error {
return err
}

c.File = c.DefaultPath
c.File = c.defaultFile()

// reload after creating config
err := c.read()
Expand Down Expand Up @@ -55,30 +54,41 @@ func (c *Config) read() error {
return err
}

// create will generate a new config, after asking the user for their token.
func (c *Config) create() error {
// Don't support creating config at --config flag, only default
if c.File != "" {
fmt.Printf("Setting up default config at: %v\n", c.DefaultPath)
}
// defaultPath is the default path to the parent directory that contains the config file
func (c *Config) defaultPath() string {
return fmt.Sprintf("%s/.circleci", os.Getenv("HOME"))
}

path := fmt.Sprintf("%s/.circleci", os.Getenv("HOME"))
// defaultFile is the path to the default config file
func (c *Config) defaultFile() string {
return fmt.Sprintf("%s/%s.yml", c.defaultPath(), c.Name)
}

if _, err := os.Stat(path); os.IsNotExist(err) {
if err = os.Mkdir(path, 0700); err != nil {
return fmt.Errorf("Error creating directory: '%s'", path)
func (c *Config) setupDefaults() error {
if _, err := os.Stat(c.defaultPath()); os.IsNotExist(err) {
if err = os.Mkdir(c.defaultPath(), 0700); err != nil {
return fmt.Errorf("Error creating directory: '%s'", c.defaultPath())
}
} else {
return fmt.Errorf("Error accessing directory: '%s'", path)
}

// Create default config file
if _, err := os.Create(c.DefaultPath); err != nil {
_, err := os.Create(c.defaultFile())
return err
}

// create will generate a new config, after asking the user for their token.
func (c *Config) create() error {
// Don't support creating config at --config flag, only default
if c.File != "" {
fmt.Printf("Setting up default config at: %v\n", c.defaultFile())
}

if err := c.setupDefaults(); err != nil {
return err
}

// open file with read & write
file, err := os.OpenFile(c.DefaultPath, os.O_RDWR, 0600)
file, err := os.OpenFile(c.defaultFile(), os.O_RDWR, 0600)
if err != nil {
return err
}
Expand All @@ -95,8 +105,8 @@ func (c *Config) create() error {

if token == "token" || token == "" {
fmt.Print("Please enter your CircleCI API token: ")
if _, err = fmt.Scanln(&token); err != nil {
return err
if n, es := fmt.Scanln(&token); n < 0 {
return es
}
fmt.Println("OK.")
}
Expand All @@ -109,7 +119,7 @@ func (c *Config) create() error {
return err
}

fmt.Printf("Your configuration has been created in `%v`.\n", c.DefaultPath)
fmt.Printf("Your configuration has been created in `%v`.\n", c.defaultFile())
fmt.Println("It can edited manually for advanced settings.")
return err
}

0 comments on commit a150d3e

Please sign in to comment.