From 2166062f2e147233df099d22e28426fb6a1a8c28 Mon Sep 17 00:00:00 2001 From: Ruslan Aliev Date: Sat, 29 Aug 2020 19:01:29 -0500 Subject: [PATCH] Refactor config init command Config init command will no longer use AirshipCTLSettings for defining a config object which needs to be saved. Change-Id: I303f05a8df1e7439d1b3dcfd41cf2679d729a9f8 Signed-off-by: Ruslan Aliev Relates-To: #327 --- cmd/config/config.go | 2 +- cmd/config/init.go | 15 ++++++++++++--- cmd/config/init_test.go | 2 +- pkg/config/config.go | 11 +++++++++++ 4 files changed, 25 insertions(+), 5 deletions(-) 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 {