migrate oc init command from urfave/cli to spf13/cobra
Signed-off-by: Christian Richter <c.richter@opencloud.eu>
This commit is contained in:
committed by
Florian Schade
parent
35900f8875
commit
1e970499af
@@ -11,49 +11,18 @@ import (
|
||||
"github.com/opencloud-eu/opencloud/opencloud/pkg/register"
|
||||
"github.com/opencloud-eu/opencloud/pkg/config"
|
||||
"github.com/opencloud-eu/opencloud/pkg/config/defaults"
|
||||
cli "github.com/urfave/cli/v2"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// InitCommand is the entrypoint for the init command
|
||||
func InitCommand(cfg *config.Config) *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "init",
|
||||
Usage: "initialise an OpenCloud config",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "insecure",
|
||||
EnvVars: []string{"OC_INSECURE"},
|
||||
Value: "ask",
|
||||
Usage: "Allow insecure OpenCloud config",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "diff",
|
||||
Aliases: []string{"d"},
|
||||
Usage: "Show the difference between the current config and the new one",
|
||||
Value: false,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "force-overwrite",
|
||||
Aliases: []string{"f"},
|
||||
EnvVars: []string{"OC_FORCE_CONFIG_OVERWRITE"},
|
||||
Value: false,
|
||||
Usage: "Force overwrite existing config file",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "config-path",
|
||||
Value: defaults.BaseConfigPath(),
|
||||
Usage: "Config path for the OpenCloud runtime",
|
||||
EnvVars: []string{"OC_CONFIG_DIR", "OC_BASE_DATA_PATH"},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "admin-password",
|
||||
Aliases: []string{"ap"},
|
||||
EnvVars: []string{"ADMIN_PASSWORD", "IDM_ADMIN_PASSWORD"},
|
||||
Usage: "Set admin password instead of using a random generated one",
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
insecureFlag := c.String("insecure")
|
||||
func InitCommand(cfg *config.Config) *cobra.Command {
|
||||
initCmd := &cobra.Command{
|
||||
Use: "init",
|
||||
Short: "initialise an OpenCloud config",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
insecureFlag := cmd.Flag("insecure").Value.String()
|
||||
insecure := false
|
||||
if insecureFlag == "ask" {
|
||||
answer := strings.ToLower(stringPrompt("Do you want to configure OpenCloud with certificate checking disabled?\n This is not recommended for public instances! [yes | no = default]"))
|
||||
@@ -63,13 +32,59 @@ func InitCommand(cfg *config.Config) *cli.Command {
|
||||
} else if insecureFlag == strings.ToLower("true") || insecureFlag == strings.ToLower("yes") || insecureFlag == strings.ToLower("y") {
|
||||
insecure = true
|
||||
}
|
||||
err := ocinit.CreateConfig(insecure, c.Bool("force-overwrite"), c.Bool("diff"), c.String("config-path"), c.String("admin-password"))
|
||||
err := ocinit.CreateConfig(insecure, cmd.Flag("force-overwrite").Changed,
|
||||
cmd.Flag("diff").Changed, cmd.Flag("config-path").Value.String(),
|
||||
cmd.Flag("admin-password").Value.String())
|
||||
if err != nil {
|
||||
log.Fatalf("Could not create config: %s", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
initCmd.Flags().String("insecure", "ask", "Allow insecure OpenCloud config")
|
||||
err := viper.BindEnv("insecure", "OC_INSECURE")
|
||||
if err != nil {
|
||||
log.Fatalf("Could not bind environment variable OC_INSECURE: %s", err)
|
||||
}
|
||||
err = viper.BindPFlag("insecure", initCmd.Flags().Lookup("insecure"))
|
||||
if err != nil {
|
||||
log.Fatalf("Could not bind flag OC_INSECURE: %s", err)
|
||||
}
|
||||
|
||||
initCmd.Flags().BoolP("diff", "d", false, "Show the difference between the current config and the new one")
|
||||
|
||||
initCmd.Flags().BoolP("force-overwrite", "f", false, "Force overwrite existing config file")
|
||||
err = viper.BindEnv("force-overwrite", "OC_FORCE_CONFIG_OVERWRITE")
|
||||
if err != nil {
|
||||
log.Fatalf("Could not bind environment variable OC_FORCE_CONFIG_OVERWRITE: %s", err)
|
||||
}
|
||||
err = viper.BindPFlag("force-overwrite", initCmd.Flags().Lookup("force-overwrite"))
|
||||
if err != nil {
|
||||
log.Fatalf("Could not bind flag OC_FORCE_CONFIG_OVERWRITE: %s", err)
|
||||
}
|
||||
|
||||
initCmd.Flags().String("config-path", defaults.BaseConfigPath(), "Config path for the OpenCloud runtime")
|
||||
err = viper.BindEnv("config-path", "OC_CONFIG_DIR")
|
||||
if err != nil {
|
||||
log.Fatalf("Could not bind environment variable OC_CONFIG_DIR: %s", err)
|
||||
}
|
||||
err = viper.BindEnv("config-path", "OC_BASE_DATA_PATH")
|
||||
if err != nil {
|
||||
log.Fatalf("Could not bind environment variable OC_BASE_DATA_PATH: %s", err)
|
||||
}
|
||||
err = viper.BindPFlag("config-path", initCmd.Flags().Lookup("config-path"))
|
||||
|
||||
initCmd.Flags().String("admin-password", "", "Set admin password instead of using a random generated one")
|
||||
err = viper.BindEnv("admin-password", "ADMIN_PASSWORD")
|
||||
if err != nil {
|
||||
log.Fatalf("Could not bind environment variable ADMIN_PASSWORD: %s", err)
|
||||
}
|
||||
err = viper.BindEnv("admin-password", "IDM_ADMIN_PASSWORD")
|
||||
if err != nil {
|
||||
log.Fatalf("Could not bind environment variable IDM_ADMIN_PASSWORD: %s", err)
|
||||
}
|
||||
err = viper.BindPFlag("admin-password", initCmd.Flags().Lookup("admin-password"))
|
||||
return initCmd
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
Reference in New Issue
Block a user