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 b772504  Added new option to change default cmk config file. (#70)
b772504 is described below

commit b77250473f4c3e19dfb00c4aea94fad5c205b29b
Author: Sina Kashipazha <[email protected]>
AuthorDate: Thu Mar 18 11:21:02 2021 +0100

    Added new option to change default cmk config file. (#70)
    
    Cloudmonkey command lets us use a different config file rather than the 
default one. This pull request added the same functionality to the cmk command.
    
         cmk -c ./my-config
---
 cmd/command.go   |  1 +
 cmk.go           |  3 ++-
 config/config.go | 10 +++++++++-
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/cmd/command.go b/cmd/command.go
index ab8106c..15d8714 100644
--- a/cmd/command.go
+++ b/cmd/command.go
@@ -68,6 +68,7 @@ Allowed flags:
   -o        API response output format: json, text, table, column, csv
   -p        Server profile
   -d        Enable debug mode
+  -c        different config file path
 
 Default commands:
 %s
diff --git a/cmk.go b/cmk.go
index 4f70cb5..eb94999 100644
--- a/cmk.go
+++ b/cmk.go
@@ -45,10 +45,11 @@ func main() {
        showVersion := flag.Bool("v", false, "show version")
        debug := flag.Bool("d", false, "enable debug mode")
        profile := flag.String("p", "", "server profile")
+       configFilePath := flag.String("c", "", "config file path")
 
        flag.Parse()
 
-       cfg := config.NewConfig()
+       cfg := config.NewConfig(configFilePath)
 
        if *showVersion {
                fmt.Printf("%s %s (build: %s, %s)\n", cfg.Name(), 
cfg.Version(), GitSHA, BuildDate)
diff --git a/config/config.go b/config/config.go
index 664d99b..592438a 100644
--- a/config/config.go
+++ b/config/config.go
@@ -24,6 +24,7 @@ import (
        "net/http/cookiejar"
        "os"
        "path"
+       "path/filepath"
        "strconv"
        "time"
 
@@ -324,10 +325,17 @@ func (c *Config) UpdateConfig(key string, value string, 
update bool) {
 }
 
 // NewConfig creates or reload config and loads API cache
-func NewConfig() *Config {
+func NewConfig(configFilePath *string) *Config {
        defaultConf := defaultConfig()
        defaultConf.Core = nil
        defaultConf.ActiveProfile = nil
+       if *configFilePath != "" {
+               defaultConf.ConfigFile, _ = filepath.Abs(*configFilePath)
+               if _, err := os.Stat(defaultConf.ConfigFile); 
os.IsNotExist(err) {
+                       fmt.Println("Config file doesn't exist.")
+                       os.Exit(1)
+               }
+       }
        cfg := reloadConfig(defaultConf)
        LoadCache(cfg)
        return cfg

Reply via email to