You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ro...@apache.org on 2018/12/07 21:51:57 UTC
[cloudstack-cloudmonkey] branch master updated: config: Fixes #35
fix panic due to inappropriate config loading
This is an automated email from the ASF dual-hosted git repository.
rohit pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cloudstack-cloudmonkey.git
The following commit(s) were added to refs/heads/master by this push:
new dd613c3 config: Fixes #35 fix panic due to inappropriate config loading
dd613c3 is described below
commit dd613c303eab81b806f65f0c6f9062e8fce8ccd5
Author: Rohit Yadav <ro...@shapeblue.com>
AuthorDate: Sat Dec 8 03:20:38 2018 +0530
config: Fixes #35 fix panic due to inappropriate config loading
Signed-off-by: Rohit Yadav <ro...@shapeblue.com>
---
cmk.go | 2 +-
config/config.go | 51 ++++++++++++++++++++++++++++++++++++---------------
2 files changed, 37 insertions(+), 16 deletions(-)
diff --git a/cmk.go b/cmk.go
index fad4f49..83e8b7e 100644
--- a/cmk.go
+++ b/cmk.go
@@ -63,7 +63,7 @@ func main() {
}
if *profile != "" {
- cfg.UpdateConfig("profile", *profile, false)
+ cfg.LoadProfile(*profile)
}
cli.SetConfig(cfg)
diff --git a/config/config.go b/config/config.go
index f0eafad..56f7c37 100644
--- a/config/config.go
+++ b/config/config.go
@@ -28,8 +28,8 @@ import (
"time"
"github.com/gofrs/flock"
- "github.com/mitchellh/go-homedir"
- "gopkg.in/ini.v1"
+ homedir "github.com/mitchellh/go-homedir"
+ ini "gopkg.in/ini.v1"
)
// Output formats
@@ -150,6 +150,11 @@ func newHTTPClient(cfg *Config) *http.Client {
return client
}
+func setActiveProfile(cfg *Config, profile *ServerProfile) {
+ cfg.ActiveProfile = profile
+ cfg.ActiveProfile.Client = newHTTPClient(cfg)
+}
+
func reloadConfig(cfg *Config) *Config {
fileLock := flock.New(path.Join(getDefaultConfigDir(), "lock"))
err := fileLock.Lock()
@@ -162,6 +167,18 @@ func reloadConfig(cfg *Config) *Config {
return cfg
}
+func readConfig(cfg *Config) *ini.File {
+ conf, err := ini.LoadSources(ini.LoadOptions{
+ IgnoreInlineComment: true,
+ }, cfg.ConfigFile)
+
+ if err != nil {
+ fmt.Printf("Fail to read config file: %v", err)
+ os.Exit(1)
+ }
+ return conf
+}
+
func saveConfig(cfg *Config) *Config {
if _, err := os.Stat(cfg.Dir); err != nil {
os.Mkdir(cfg.Dir, 0700)
@@ -177,18 +194,10 @@ func saveConfig(cfg *Config) *Config {
conf.SaveTo(cfg.ConfigFile)
}
- // Read config
- conf, err := ini.LoadSources(ini.LoadOptions{
- IgnoreInlineComment: true,
- }, cfg.ConfigFile)
-
- if err != nil {
- fmt.Printf("Fail to read config file: %v", err)
- os.Exit(1)
- }
+ conf := readConfig(cfg)
core, err := conf.GetSection(ini.DEFAULT_SECTION)
- if core == nil {
+ if core == nil || err != nil {
defaultCore := defaultCoreConfig()
section, _ := conf.NewSection(ini.DEFAULT_SECTION)
section.ReflectFrom(&defaultCore)
@@ -209,7 +218,7 @@ func saveConfig(cfg *Config) *Config {
activeProfile := defaultProfile()
section, _ := conf.NewSection(cfg.Core.ProfileName)
section.ReflectFrom(&activeProfile)
- cfg.ActiveProfile = &activeProfile
+ setActiveProfile(cfg, &activeProfile)
} else {
// Write
if cfg.ActiveProfile != nil {
@@ -218,7 +227,7 @@ func saveConfig(cfg *Config) *Config {
// Update
profile := new(ServerProfile)
conf.Section(cfg.Core.ProfileName).MapTo(profile)
- cfg.ActiveProfile = profile
+ setActiveProfile(cfg, profile)
}
// Save
conf.SaveTo(cfg.ConfigFile)
@@ -232,10 +241,22 @@ func saveConfig(cfg *Config) *Config {
profiles = append(profiles, profile.Name())
}
- cfg.ActiveProfile.Client = newHTTPClient(cfg)
return cfg
}
+// LoadProfile loads an existing profile
+func (c *Config) LoadProfile(name string) {
+ conf := readConfig(c)
+ section, err := conf.GetSection(name)
+ if err != nil || section == nil {
+ fmt.Printf("Unable to load profile '%s': %v", name, err)
+ os.Exit(1)
+ }
+ profile := new(ServerProfile)
+ conf.Section(name).MapTo(profile)
+ setActiveProfile(c, profile)
+}
+
// UpdateConfig updates and saves config
func (c *Config) UpdateConfig(key string, value string, update bool) {
switch key {