Skip to content

Commit b772504

Browse files
authored
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
1 parent 2d9fd04 commit b772504

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

cmd/command.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Allowed flags:
6868
-o API response output format: json, text, table, column, csv
6969
-p Server profile
7070
-d Enable debug mode
71+
-c different config file path
7172
7273
Default commands:
7374
%s

cmk.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ func main() {
4545
showVersion := flag.Bool("v", false, "show version")
4646
debug := flag.Bool("d", false, "enable debug mode")
4747
profile := flag.String("p", "", "server profile")
48+
configFilePath := flag.String("c", "", "config file path")
4849

4950
flag.Parse()
5051

51-
cfg := config.NewConfig()
52+
cfg := config.NewConfig(configFilePath)
5253

5354
if *showVersion {
5455
fmt.Printf("%s %s (build: %s, %s)\n", cfg.Name(), cfg.Version(), GitSHA, BuildDate)

config/config.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"net/http/cookiejar"
2525
"os"
2626
"path"
27+
"path/filepath"
2728
"strconv"
2829
"time"
2930

@@ -324,10 +325,17 @@ func (c *Config) UpdateConfig(key string, value string, update bool) {
324325
}
325326

326327
// NewConfig creates or reload config and loads API cache
327-
func NewConfig() *Config {
328+
func NewConfig(configFilePath *string) *Config {
328329
defaultConf := defaultConfig()
329330
defaultConf.Core = nil
330331
defaultConf.ActiveProfile = nil
332+
if *configFilePath != "" {
333+
defaultConf.ConfigFile, _ = filepath.Abs(*configFilePath)
334+
if _, err := os.Stat(defaultConf.ConfigFile); os.IsNotExist(err) {
335+
fmt.Println("Config file doesn't exist.")
336+
os.Exit(1)
337+
}
338+
}
331339
cfg := reloadConfig(defaultConf)
332340
LoadCache(cfg)
333341
return cfg

0 commit comments

Comments
 (0)