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>
This commit is contained in:
Ruslan Aliev 2020-05-06 05:09:18 -05:00
parent f05693a8bf
commit ad7f80f2bf
3 changed files with 21 additions and 15 deletions

@ -113,9 +113,8 @@ func (c *Config) loadFromAirConfig(airshipConfigPath string) error {
c.loadedConfigPath = airshipConfigPath c.loadedConfigPath = airshipConfigPath
// If I can read from the file, load from it // If I can read from the file, load from it
if _, err := os.Stat(airshipConfigPath); os.IsNotExist(err) { // throw an error otherwise
return nil if _, err := os.Stat(airshipConfigPath); err != nil {
} else if err != nil {
return err return err
} }

@ -70,9 +70,9 @@ func (a *AirshipCTLSettings) InitFlags(cmd *cobra.Command) {
func (a *AirshipCTLSettings) InitConfig() { func (a *AirshipCTLSettings) InitConfig() {
a.Config = config.NewConfig() a.Config = config.NewConfig()
a.initAirshipConfigPath() a.InitAirshipConfigPath()
a.initKubeConfigPath() a.InitKubeConfigPath()
initPluginPath() InitPluginPath()
err := a.Config.LoadConfig(a.AirshipConfigPath, a.KubeConfigPath) err := a.Config.LoadConfig(a.AirshipConfigPath, a.KubeConfigPath)
if err != nil { 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 // The airshipConfigPath may already have been received as a command line argument
if a.AirshipConfigPath != "" { if a.AirshipConfigPath != "" {
return return
@ -98,7 +99,8 @@ func (a *AirshipCTLSettings) initAirshipConfigPath() {
a.AirshipConfigPath = filepath.Join(homeDir, config.AirshipConfigDir, config.AirshipConfig) 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 // NOTE(howell): This function will set the kubeConfigPath to the
// default location under the airship directory unless the user // default location under the airship directory unless the user
// *explicitly* specifies a different location, either by setting the // *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) a.KubeConfigPath = filepath.Join(homeDir, config.AirshipConfigDir, config.AirshipKubeConfig)
} }
// Sets the location to look for kustomize plugins (including airshipctl itself). // InitPluginPath - Sets the location to look for kustomize plugins (including airshipctl itself).
func initPluginPath() { func InitPluginPath() {
pluginPathLock.Lock() pluginPathLock.Lock()
defer pluginPathLock.Unlock() defer pluginPathLock.Unlock()

@ -49,7 +49,9 @@ func TestInitConfig(t *testing.T) {
expectedKubeConfig := filepath.Join(testDir, config.AirshipConfigDir, config.AirshipKubeConfig) expectedKubeConfig := filepath.Join(testDir, config.AirshipConfigDir, config.AirshipKubeConfig)
expectedPluginPath := filepath.Join(testDir, config.AirshipConfigDir, config.AirshipPluginPath) 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, expectedAirshipConfig, testSettings.AirshipConfigPath)
assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath) assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath)
assert.Equal(t, expectedPluginPath, environment.PluginPath()) assert.Equal(t, expectedPluginPath, environment.PluginPath())
@ -73,7 +75,9 @@ func TestInitConfig(t *testing.T) {
defer os.Unsetenv(config.AirshipKubeConfigEnv) defer os.Unsetenv(config.AirshipKubeConfigEnv)
defer os.Unsetenv(config.AirshipPluginPathEnv) defer os.Unsetenv(config.AirshipPluginPathEnv)
testSettings.InitConfig() testSettings.InitAirshipConfigPath()
testSettings.InitKubeConfigPath()
environment.InitPluginPath()
assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath) assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath)
assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath) assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath)
assert.Equal(t, expectedPluginPath, environment.PluginPath()) assert.Equal(t, expectedPluginPath, environment.PluginPath())
@ -93,8 +97,8 @@ func TestInitConfig(t *testing.T) {
KubeConfigPath: expectedKubeConfig, KubeConfigPath: expectedKubeConfig,
} }
// InitConfig should not change any values testSettings.InitAirshipConfigPath()
testSettings.InitConfig() testSettings.InitKubeConfigPath()
assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath) assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath)
assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath) assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath)
}) })
@ -123,7 +127,8 @@ func TestInitConfig(t *testing.T) {
KubeConfigPath: expectedKubeConfig, KubeConfigPath: expectedKubeConfig,
} }
testSettings.InitConfig() testSettings.InitAirshipConfigPath()
testSettings.InitKubeConfigPath()
assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath) assert.Equal(t, expectedAirshipConfig, testSettings.AirshipConfigPath)
assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath) assert.Equal(t, expectedKubeConfig, testSettings.KubeConfigPath)
}) })