Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

Commit

Permalink
added -o/--output flag along with question to override default config…
Browse files Browse the repository at this point in the history
… name
  • Loading branch information
Joice M. Joseph committed Apr 24, 2020
1 parent 2321788 commit e75693b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ GLOBAL OPTIONS:

Either of these options create a config file `kubekutr.yml` in your current working directory. You can edit this file further to suit your needs and scaffold a project using this.

- **Define output file**

`kubekutr init -o <filename.yml>`

Override the default config filename.

### Scaffold a new project

```bash
Expand Down
21 changes: 16 additions & 5 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"zerodha.tech/kubekutr/models"
)

const (
defaultConfigName = "kubekutr.yml"
var (
configName = "kubekutr.yml"
)

// InitProject initializes git repo and copies a sample config
Expand All @@ -26,6 +26,10 @@ func (hub *Hub) InitProject(config models.Config) cli.Command {
Name: "default, d",
Usage: "Use the default config file",
},
cli.StringFlag{
Name: "output, o",
Usage: "Config file name.",
},
},
}
}
Expand All @@ -38,18 +42,25 @@ func (hub *Hub) init(cliCtx *cli.Context) error {
return fmt.Errorf("error while initializing git repo: %v", err)
}
var configFile []byte
output := cliCtx.String("output")
if output != "" {
configName = output
}
if cliCtx.Bool("default") {
configFile, err = hub.Fs.Read("templates/config.sample.yml")
if err != nil {
return fmt.Errorf("error reading default config file template: %v", err)
}
err = createDefaultConfig(configFile, defaultConfigName)
err = createDefaultConfig(configFile, configName)
if err != nil {
return fmt.Errorf("error creating default config: %v", err)
}
} else {
workloads := []models.Workload{}
workloadsLen := gatherBasicInfo()
if output == "" {
configName = gatherOutputFileInfo()
}
// Iterate for all workloads
for i := 0; i < workloadsLen; i++ {
wd, err := gatherWorkloadsInfo()
Expand All @@ -65,11 +76,11 @@ func (hub *Hub) init(cliCtx *cli.Context) error {
if err != nil {
return fmt.Errorf("Error while marshalling yaml: %v", err)
}
err = createDefaultConfig(configFile, defaultConfigName)
err = createDefaultConfig(configFile, configName)
if err != nil {
return fmt.Errorf("error creating default config: %v", err)
}
}
log.Printf("Congrats! Your default configuration is created at %s", defaultConfigName)
log.Printf("Congrats! Your default configuration is created at %s", configName)
return nil
}
13 changes: 13 additions & 0 deletions cmd/questions.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@ func gatherContainerInfo() (models.Container, error) {
return ctr, nil
}

func gatherOutputFileInfo() string {
var (
fileName = "kubekutr.yml"
)
// Gather information from user.
exitOnInterrupt(survey.AskOne(&survey.Input{
Message: "What should be the config file name?",
Help: "Override the default config filename",
Default: "kubekutr.yml",
}, &fileName, survey.WithValidator(survey.Required)))
return fileName
}

func exitOnInterrupt(err error) error {
if err == terminal.InterruptErr {
fmt.Println("quitting kubekutr. bye...")
Expand Down

0 comments on commit e75693b

Please sign in to comment.