This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
config.go
110 lines (92 loc) · 2.58 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Author Seth Hoenig
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/shoenig/marathonctl/config"
)
type flags struct {
version bool
semver bool
configfile string
host string
login string
format string
insecure bool
}
// cli arguments override configuration file
func cliargs() flags {
var f flags
flag.BoolVar(&f.version, "v", false, "display version (git sha1) and exit")
flag.BoolVar(&f.semver, "s", false, "display semversion and exit")
flag.StringVar(&f.configfile, "c", "", "path to configfile")
flag.StringVar(&f.host, "h", "", "override marathon host(s) (with transport and port)")
flag.StringVar(&f.login, "u", "", "override username and password")
flag.StringVar(&f.format, "f", "", "override output format (raw, json, jsonpp)")
flag.BoolVar(&f.insecure, "k", false, "insecure - do not verify certificate authority")
flag.Parse()
return f
}
func readConfigfile(filename string) (string, string, string, error) {
props, err := config.ReadProperties(filename)
if err != nil {
return "", "", "", err
}
host := props.GetStringOr("marathon.host", "")
user := props.GetStringOr("marathon.user", "")
pass := props.GetStringOr("marathon.password", "")
format := props.GetStringOr("marathon.format", "")
login := ""
if user != "" && pass != "" {
login = user + ":" + pass
}
return host, login, format, nil
}
func defaultConfigfileLocations() []string {
return []string{
filepath.Clean(filepath.Join(os.Getenv("HOME"), ".config", "marathonctl", "config")),
filepath.FromSlash("/etc/marathonctl"),
}
}
func findBestConfigfile() string {
for _, location := range defaultConfigfileLocations() {
if _, err := os.Stat(location); err == nil {
return location
}
}
return ""
}
// loadConfig will parse the CLI flags.
// If --version or --semver are set, no further configuration
// is read. Otherwise, configuration is read from --configfile as
// specified, and then overridden with provided CLI flags.
func loadConfig() (flags, error) {
f := cliargs()
if f.version || f.semver {
return f, nil
}
if f.host != "" && f.login != "" {
return f, nil
}
if f.configfile == "" {
f.configfile = findBestConfigfile()
}
if f.configfile != "" {
fileHost, fileLogin, fileFormat, err := readConfigfile(f.configfile)
if err != nil {
return flags{}, fmt.Errorf("failed to read config file: %v", err)
}
if f.host == "" && fileHost != "" {
f.host = fileHost
}
if f.login == "" && fileLogin != "" {
f.login = fileLogin
}
if f.format == "" && fileFormat != "" {
f.format = fileFormat
}
}
return f, nil
}