From 37c044a5b7e8de504c8a3cd348cc7bb6c4d1e570 Mon Sep 17 00:00:00 2001 From: Ruslan Aliev Date: Fri, 10 Jul 2020 19:37:58 -0500 Subject: [PATCH] Fix config init command airshipctl config init command currently fails with "no such file or directory" error, this patch fixes it. Change-Id: I78cff26ed71bdb400ce897630102350e72dc495d Signed-off-by: Ruslan Aliev --- cmd/config/config.go | 4 +++- pkg/config/config.go | 16 +++++++++------- pkg/environment/settings.go | 7 +++++-- testutil/testconfig.go | 2 +- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/cmd/config/config.go b/cmd/config/config.go index e40be2b9b..3fb5566c7 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -29,7 +29,9 @@ func NewConfigCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comma Short: "Manage the airshipctl config file", PersistentPreRun: func(cmd *cobra.Command, args []string) { log.Init(rootSettings.Debug, cmd.OutOrStderr()) - + if cmd.Use == "init" { + rootSettings.Create = true + } // Load or Initialize airship Config rootSettings.InitConfig() }, diff --git a/pkg/config/config.go b/pkg/config/config.go index 1e64135aa..807336f29 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -83,13 +83,13 @@ type Config struct { // LoadConfig populates the Config object using the files found at // airshipConfigPath and kubeConfigPath -func (c *Config) LoadConfig(airshipConfigPath, kubeConfigPath string) error { - err := c.loadFromAirConfig(airshipConfigPath) +func (c *Config) LoadConfig(airshipConfigPath, kubeConfigPath string, create bool) error { + err := c.loadFromAirConfig(airshipConfigPath, create) if err != nil { return err } - err = c.loadKubeConfig(kubeConfigPath) + err = c.loadKubeConfig(kubeConfigPath, create) if err != nil { return err } @@ -104,7 +104,7 @@ func (c *Config) LoadConfig(airshipConfigPath, kubeConfigPath string) error { // * airshipConfigPath is the empty string // * the file at airshipConfigPath is inaccessible // * the file at airshipConfigPath cannot be marshaled into Config -func (c *Config) loadFromAirConfig(airshipConfigPath string) error { +func (c *Config) loadFromAirConfig(airshipConfigPath string, create bool) error { if airshipConfigPath == "" { return errors.New("configuration file location was not provided") } @@ -114,20 +114,22 @@ func (c *Config) loadFromAirConfig(airshipConfigPath string) error { // If I can read from the file, load from it // throw an error otherwise - if _, err := os.Stat(airshipConfigPath); err != nil { + if _, err := os.Stat(airshipConfigPath); os.IsNotExist(err) && create { + return nil + } else if err != nil { return err } return util.ReadYAMLFile(airshipConfigPath, c) } -func (c *Config) loadKubeConfig(kubeConfigPath string) error { +func (c *Config) loadKubeConfig(kubeConfigPath string, create bool) error { // Will need this for persisting the changes c.kubeConfigPath = kubeConfigPath // If I can read from the file, load from it var err error - if _, err = os.Stat(kubeConfigPath); os.IsNotExist(err) { + if _, err = os.Stat(kubeConfigPath); os.IsNotExist(err) && create { // Default kubeconfig matching Airship target cluster c.kubeConfig = &clientcmdapi.Config{ Clusters: map[string]*clientcmdapi.Cluster{ diff --git a/pkg/environment/settings.go b/pkg/environment/settings.go index 88d9a42f8..6e3528b5b 100644 --- a/pkg/environment/settings.go +++ b/pkg/environment/settings.go @@ -33,6 +33,7 @@ type AirshipCTLSettings struct { Debug bool AirshipConfigPath string KubeConfigPath string + Create bool Config *config.Config } @@ -64,6 +65,8 @@ func (a *AirshipCTLSettings) InitFlags(cmd *cobra.Command) { clientcmd.RecommendedConfigPathFlag, "", `Path to kubeconfig associated with airshipctl configuration. (default "`+defaultKubeConfigPath+`")`) + + a.Create = false } // InitConfig - Initializes and loads Config it exists. @@ -74,10 +77,10 @@ func (a *AirshipCTLSettings) InitConfig() { a.InitKubeConfigPath() InitPluginPath() - err := a.Config.LoadConfig(a.AirshipConfigPath, a.KubeConfigPath) + err := a.Config.LoadConfig(a.AirshipConfigPath, a.KubeConfigPath, a.Create) if err != nil { // Should stop airshipctl - log.Fatal(err) + log.Fatal("Failed to load or initialize config: ", err) } } diff --git a/testutil/testconfig.go b/testutil/testconfig.go index e0a4ded46..30ca38e12 100644 --- a/testutil/testconfig.go +++ b/testutil/testconfig.go @@ -190,7 +190,7 @@ func InitConfig(t *testing.T) (conf *config.Config, cleanup func(*testing.T)) { conf = config.NewConfig() - err = conf.LoadConfig(configPath, kubeConfigPath) + err = conf.LoadConfig(configPath, kubeConfigPath, false) require.NoError(t, err) return conf, cleanup