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 <raliev@mirantis.com>
Relates-To: #327
This commit is contained in:
Ruslan Aliev 2020-08-29 19:01:29 -05:00
parent 88ec55d34b
commit 2166062f2e
4 changed files with 25 additions and 5 deletions

View File

@ -55,6 +55,6 @@ func NewConfigCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comma
configRootCmd.AddCommand(NewSetManifestCommand(cfgFactory)) configRootCmd.AddCommand(NewSetManifestCommand(cfgFactory))
// Init will have different factory // Init will have different factory
configRootCmd.AddCommand(NewInitCommand(rootSettings)) configRootCmd.AddCommand(NewInitCommand())
return configRootCmd return configRootCmd
} }

View File

@ -19,7 +19,7 @@ package config
import ( import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/config"
) )
const ( 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. // 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 // TODO(howell): It'd be nice to have a flag to tell
// airshipctl where to store the new files. // airshipctl where to store the new files.
// TODO(howell): Currently, this command overwrites whatever the user // 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", Short: "Generate initial configuration files for airshipctl",
Long: initLong[1:], Long: initLong[1:],
RunE: func(cmd *cobra.Command, args []string) error { 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)
}, },
} }

View File

@ -27,7 +27,7 @@ func TestConfigInit(t *testing.T) {
{ {
Name: "config-init-help", Name: "config-init-help",
CmdLine: "-h", CmdLine: "-h",
Cmd: NewInitCommand(nil), Cmd: NewInitCommand(),
}, },
} }

View File

@ -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 // initConfigPath - Initializes loadedConfigPath and kubeConfigPath variable for Config object
func (c *Config) initConfigPath(airshipConfigPath string, kubeConfigPath string) { func (c *Config) initConfigPath(airshipConfigPath string, kubeConfigPath string) {
switch { switch {