Skip to content

Commit

Permalink
feat: add command to generate template configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonid Zaikin committed Mar 18, 2024
1 parent 73f0195 commit 692c2d3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
3 changes: 2 additions & 1 deletion cmd/cleura/configcmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,12 @@ func TrySetConfigFromFile(c *cli.Context) error {
func Command() *cli.Command {
return &cli.Command{
Name: "config",
Description: "Command used for preparing cleura cli credentials and setting default parameter values",
Description: "Command used for working with configuration file for the cleura cli",
Subcommands: []*cli.Command{
setCommand(),
listCommand(),
showActiveCommand(),
generateConfigTemplateCommand(),
},
}
}
52 changes: 52 additions & 0 deletions cmd/cleura/configcmd/genconftmplcmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package configcmd

import (
"os"

"github.com/aztekas/cleura-client-go/cmd/cleura/utils"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
)

func generateConfigTemplateCommand() *cli.Command {
return &cli.Command{
Name: "generate-template",
Description: "Generate configuration file template on the given path",
Usage: "Generate configuration file template on the given path",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output-file",
Aliases: []string{"o"},
Usage: "Path to configuration file. $HOME/.config/cleura/config if not set. NB: Overwrites existing if found",
},
},
Action: func(ctx *cli.Context) error {
config := &Config{
ActiveConfig: "default",
Configurations: map[string]Configuration{
"default": {},
},
}
templateByte, err := yaml.Marshal(config)
if err != nil {
return err
}
path, err := utils.ChoosePath(ctx.String("output-file"))
if err != nil {
return err
}
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return err
}
if _, err := f.Write(templateByte); err != nil {
f.Close() // ignore error; Write error takes precedence
return err
}
if err := f.Close(); err != nil {
return err
}
return nil
},
}
}
4 changes: 2 additions & 2 deletions cmd/cleura/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func GetDefaultConfigPath() (string, error) {
return filepath.Join(homedir, ".config", "cleura", "config"), nil
}

// Choose path for the configuration file. Choose `--path PATH` if supplied
// otherwise set default user directory
// Choose path for the configuration file. Choose path` if supplied
// otherwise set path within default user directory
func ChoosePath(path string) (string, error) {
var err error
if path == "" {
Expand Down

0 comments on commit 692c2d3

Please sign in to comment.