diff --git a/cmd/config/config.go b/cmd/config/config.go index 94a14d2f2..0c92a5526 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -55,6 +55,6 @@ func NewConfigCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comma configRootCmd.AddCommand(NewSetManifestCommand(cfgFactory)) // Init will have different factory - configRootCmd.AddCommand(NewInitCommand(rootSettings)) + configRootCmd.AddCommand(NewInitCommand()) return configRootCmd } diff --git a/cmd/config/init.go b/cmd/config/init.go index ef4a34bf9..858d1353d 100644 --- a/cmd/config/init.go +++ b/cmd/config/init.go @@ -19,7 +19,7 @@ package config import ( "github.com/spf13/cobra" - "opendev.org/airship/airshipctl/pkg/environment" + "opendev.org/airship/airshipctl/pkg/config" ) const ( @@ -33,7 +33,7 @@ NOTE: This will overwrite any existing config files in $HOME/.airship ) // NewInitCommand creates a command for generating default airshipctl config files. -func NewInitCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { +func NewInitCommand() *cobra.Command { // TODO(howell): It'd be nice to have a flag to tell // airshipctl where to store the new files. // TODO(howell): Currently, this command overwrites whatever the user @@ -44,7 +44,16 @@ func NewInitCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command Short: "Generate initial configuration files for airshipctl", Long: initLong[1:], RunE: func(cmd *cobra.Command, args []string) error { - return rootSettings.Config.PersistConfig(true) + airshipConfigPath, err := cmd.Flags().GetString("airshipconf") + if err != nil { + airshipConfigPath = "" + } + + kubeConfigPath, err := cmd.Flags().GetString("kubeconfig") + if err != nil { + kubeConfigPath = "" + } + return config.CreateConfig(airshipConfigPath, kubeConfigPath) }, } diff --git a/cmd/config/init_test.go b/cmd/config/init_test.go index dc5473234..9c50306e3 100644 --- a/cmd/config/init_test.go +++ b/cmd/config/init_test.go @@ -27,7 +27,7 @@ func TestConfigInit(t *testing.T) { { Name: "config-init-help", CmdLine: "-h", - Cmd: NewInitCommand(nil), + Cmd: NewInitCommand(), }, } diff --git a/pkg/config/config.go b/pkg/config/config.go index b517cb553..d4d79267e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -116,6 +116,17 @@ func CreateFactory(airshipConfigPath *string, kubeConfigPath *string) Factory { } } +// CreateConfig saves default config to specified paths +func CreateConfig(airshipConfigPath string, kubeConfigPath string) error { + cfg := NewConfig() + cfg.kubeConfig = NewKubeConfig() + cfg.initConfigPath(airshipConfigPath, kubeConfigPath) + if err := cfg.reconcileConfig(); err != nil { + return err + } + return cfg.PersistConfig(true) +} + // initConfigPath - Initializes loadedConfigPath and kubeConfigPath variable for Config object func (c *Config) initConfigPath(airshipConfigPath string, kubeConfigPath string) { switch {