diff --git a/cmd/cleura/configcmd/config.go b/cmd/cleura/configcmd/config.go index 425d6e2..ece5f43 100644 --- a/cmd/cleura/configcmd/config.go +++ b/cmd/cleura/configcmd/config.go @@ -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(), }, } } diff --git a/cmd/cleura/configcmd/genconftmplcmd.go b/cmd/cleura/configcmd/genconftmplcmd.go new file mode 100644 index 0000000..f42034c --- /dev/null +++ b/cmd/cleura/configcmd/genconftmplcmd.go @@ -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 + }, + } +} diff --git a/cmd/cleura/utils/utils.go b/cmd/cleura/utils/utils.go index 8c30066..48a2dac 100644 --- a/cmd/cleura/utils/utils.go +++ b/cmd/cleura/utils/utils.go @@ -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 == "" {