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 <raliev@mirantis.com>
This commit is contained in:
parent
1cafd674df
commit
37c044a5b7
@ -29,7 +29,9 @@ func NewConfigCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Comma
|
|||||||
Short: "Manage the airshipctl config file",
|
Short: "Manage the airshipctl config file",
|
||||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||||
log.Init(rootSettings.Debug, cmd.OutOrStderr())
|
log.Init(rootSettings.Debug, cmd.OutOrStderr())
|
||||||
|
if cmd.Use == "init" {
|
||||||
|
rootSettings.Create = true
|
||||||
|
}
|
||||||
// Load or Initialize airship Config
|
// Load or Initialize airship Config
|
||||||
rootSettings.InitConfig()
|
rootSettings.InitConfig()
|
||||||
},
|
},
|
||||||
|
@ -83,13 +83,13 @@ type Config struct {
|
|||||||
|
|
||||||
// LoadConfig populates the Config object using the files found at
|
// LoadConfig populates the Config object using the files found at
|
||||||
// airshipConfigPath and kubeConfigPath
|
// airshipConfigPath and kubeConfigPath
|
||||||
func (c *Config) LoadConfig(airshipConfigPath, kubeConfigPath string) error {
|
func (c *Config) LoadConfig(airshipConfigPath, kubeConfigPath string, create bool) error {
|
||||||
err := c.loadFromAirConfig(airshipConfigPath)
|
err := c.loadFromAirConfig(airshipConfigPath, create)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.loadKubeConfig(kubeConfigPath)
|
err = c.loadKubeConfig(kubeConfigPath, create)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -104,7 +104,7 @@ func (c *Config) LoadConfig(airshipConfigPath, kubeConfigPath string) error {
|
|||||||
// * airshipConfigPath is the empty string
|
// * airshipConfigPath is the empty string
|
||||||
// * the file at airshipConfigPath is inaccessible
|
// * the file at airshipConfigPath is inaccessible
|
||||||
// * the file at airshipConfigPath cannot be marshaled into Config
|
// * 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 == "" {
|
if airshipConfigPath == "" {
|
||||||
return errors.New("configuration file location was not provided")
|
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
|
// If I can read from the file, load from it
|
||||||
// throw an error otherwise
|
// 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 err
|
||||||
}
|
}
|
||||||
|
|
||||||
return util.ReadYAMLFile(airshipConfigPath, c)
|
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
|
// Will need this for persisting the changes
|
||||||
c.kubeConfigPath = kubeConfigPath
|
c.kubeConfigPath = kubeConfigPath
|
||||||
|
|
||||||
// If I can read from the file, load from it
|
// If I can read from the file, load from it
|
||||||
var err error
|
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
|
// Default kubeconfig matching Airship target cluster
|
||||||
c.kubeConfig = &clientcmdapi.Config{
|
c.kubeConfig = &clientcmdapi.Config{
|
||||||
Clusters: map[string]*clientcmdapi.Cluster{
|
Clusters: map[string]*clientcmdapi.Cluster{
|
||||||
|
@ -33,6 +33,7 @@ type AirshipCTLSettings struct {
|
|||||||
Debug bool
|
Debug bool
|
||||||
AirshipConfigPath string
|
AirshipConfigPath string
|
||||||
KubeConfigPath string
|
KubeConfigPath string
|
||||||
|
Create bool
|
||||||
Config *config.Config
|
Config *config.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,6 +65,8 @@ func (a *AirshipCTLSettings) InitFlags(cmd *cobra.Command) {
|
|||||||
clientcmd.RecommendedConfigPathFlag,
|
clientcmd.RecommendedConfigPathFlag,
|
||||||
"",
|
"",
|
||||||
`Path to kubeconfig associated with airshipctl configuration. (default "`+defaultKubeConfigPath+`")`)
|
`Path to kubeconfig associated with airshipctl configuration. (default "`+defaultKubeConfigPath+`")`)
|
||||||
|
|
||||||
|
a.Create = false
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitConfig - Initializes and loads Config it exists.
|
// InitConfig - Initializes and loads Config it exists.
|
||||||
@ -74,10 +77,10 @@ func (a *AirshipCTLSettings) InitConfig() {
|
|||||||
a.InitKubeConfigPath()
|
a.InitKubeConfigPath()
|
||||||
InitPluginPath()
|
InitPluginPath()
|
||||||
|
|
||||||
err := a.Config.LoadConfig(a.AirshipConfigPath, a.KubeConfigPath)
|
err := a.Config.LoadConfig(a.AirshipConfigPath, a.KubeConfigPath, a.Create)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Should stop airshipctl
|
// Should stop airshipctl
|
||||||
log.Fatal(err)
|
log.Fatal("Failed to load or initialize config: ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,7 +190,7 @@ func InitConfig(t *testing.T) (conf *config.Config, cleanup func(*testing.T)) {
|
|||||||
|
|
||||||
conf = config.NewConfig()
|
conf = config.NewConfig()
|
||||||
|
|
||||||
err = conf.LoadConfig(configPath, kubeConfigPath)
|
err = conf.LoadConfig(configPath, kubeConfigPath, false)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
return conf, cleanup
|
return conf, cleanup
|
||||||
|
Loading…
Reference in New Issue
Block a user