Merge "Return an error if there is a problem to access config file"

This commit is contained in:
Zuul 2020-06-13 00:14:57 +00:00 committed by Gerrit Code Review
commit 42857f527b
3 changed files with 21 additions and 15 deletions

View File

@ -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
} }

View File

@ -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()

View File

@ -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)
}) })