From ad7f80f2bfe9b9c247089f22b2b87cc1eef145bf Mon Sep 17 00:00:00 2001 From: Ruslan Aliev <raliev@mirantis.com> Date: Wed, 6 May 2020 05:09:18 -0500 Subject: [PATCH] Return an error if there is a problem to access config file Currently, if config file doesn't exist - airshipctl doesn't throw an error and uses empty config object instead. It seems this behavior was an attempt to hide an error if there was a call of subcommand which is not require proper config file. Since we moved config load calls to the specific subcommands from root level - current behavior should be changed and we have to throw an error instead if it's not possible to access config file. Change-Id: I7e949fbc9d48d682e26500e0b6b6b9bd12665e24 Relates-To: #199 Closes: #199 Signed-off-by: Ruslan Aliev <raliev@mirantis.com> --- pkg/config/config.go | 5 ++--- pkg/environment/settings.go | 16 +++++++++------- pkg/environment/settings_test.go | 15 ++++++++++----- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 21b8c3d00..da8c32138 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -113,9 +113,8 @@ func (c *Config) loadFromAirConfig(airshipConfigPath string) error { c.loadedConfigPath = airshipConfigPath // If I can read from the file, load from it - if _, err := os.Stat(airshipConfigPath); os.IsNotExist(err) { - return nil - } else if err != nil { + // throw an error otherwise + if _, err := os.Stat(airshipConfigPath); err != nil { return err } diff --git a/pkg/environment/settings.go b/pkg/environment/settings.go index b50bc94ac..88d9a42f8 100644 --- a/pkg/environment/settings.go +++ b/pkg/environment/settings.go @@ -70,9 +70,9 @@ func (a *AirshipCTLSettings) InitFlags(cmd *cobra.Command) { func (a *AirshipCTLSettings) InitConfig() { a.Config = config.NewConfig() - a.initAirshipConfigPath() - a.initKubeConfigPath() - initPluginPath() + a.InitAirshipConfigPath() + a.InitKubeConfigPath() + InitPluginPath() err := a.Config.LoadConfig(a.AirshipConfigPath, a.KubeConfigPath) if err != nil { @@ -81,7 +81,8 @@ func (a *AirshipCTLSettings) InitConfig() { } } -func (a *AirshipCTLSettings) initAirshipConfigPath() { +// InitAirshipConfigPath - Initializes AirshipConfigPath variable for Config object +func (a *AirshipCTLSettings) InitAirshipConfigPath() { // The airshipConfigPath may already have been received as a command line argument if a.AirshipConfigPath != "" { return @@ -98,7 +99,8 @@ func (a *AirshipCTLSettings) initAirshipConfigPath() { a.AirshipConfigPath = filepath.Join(homeDir, config.AirshipConfigDir, config.AirshipConfig) } -func (a *AirshipCTLSettings) initKubeConfigPath() { +// InitKubeConfigPath - Initializes KubeConfigPath variable for Config object +func (a *AirshipCTLSettings) InitKubeConfigPath() { // NOTE(howell): This function will set the kubeConfigPath to the // default location under the airship directory unless the user // *explicitly* specifies a different location, either by setting the @@ -122,8 +124,8 @@ func (a *AirshipCTLSettings) initKubeConfigPath() { a.KubeConfigPath = filepath.Join(homeDir, config.AirshipConfigDir, config.AirshipKubeConfig) } -// Sets the location to look for kustomize plugins (including airshipctl itself). -func initPluginPath() { +// InitPluginPath - Sets the location to look for kustomize plugins (including airshipctl itself). +func InitPluginPath() { pluginPathLock.Lock() defer pluginPathLock.Unlock() diff --git a/pkg/environment/settings_test.go b/pkg/environment/settings_test.go index c5895fc40..3d6adf342 100644 --- a/pkg/environment/settings_test.go +++ b/pkg/environment/settings_test.go @@ -49,7 +49,9 @@ func TestInitConfig(t *testing.T) { expectedKubeConfig := filepath.Join(testDir, config.AirshipConfigDir, config.AirshipKubeConfig) expectedPluginPath := filepath.Join(testDir, config.AirshipConfigDir, config.AirshipPluginPath) - testSettings.InitConfig() + testSettings.InitAirshipConfigPath() + testSettings.InitKubeConfigPath() + environment.InitPluginPath() assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath) assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath) assert.Equal(t, expectedPluginPath, environment.PluginPath()) @@ -73,7 +75,9 @@ func TestInitConfig(t *testing.T) { defer os.Unsetenv(config.AirshipKubeConfigEnv) defer os.Unsetenv(config.AirshipPluginPathEnv) - testSettings.InitConfig() + testSettings.InitAirshipConfigPath() + testSettings.InitKubeConfigPath() + environment.InitPluginPath() assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath) assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath) assert.Equal(t, expectedPluginPath, environment.PluginPath()) @@ -93,8 +97,8 @@ func TestInitConfig(t *testing.T) { KubeConfigPath: expectedKubeConfig, } - // InitConfig should not change any values - testSettings.InitConfig() + testSettings.InitAirshipConfigPath() + testSettings.InitKubeConfigPath() assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath) assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath) }) @@ -123,7 +127,8 @@ func TestInitConfig(t *testing.T) { KubeConfigPath: expectedKubeConfig, } - testSettings.InitConfig() + testSettings.InitAirshipConfigPath() + testSettings.InitKubeConfigPath() assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath) assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath) })