-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparams.go
102 lines (83 loc) · 2.4 KB
/
params.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
package main
import (
"flag"
"path/filepath"
"os"
"strconv"
log "github.com/Sirupsen/logrus"
)
func check(e error, m string) {
if e != nil {
log.Error("[Error]: ", m , e)
}
}
type Params struct {
Url string
Prefix string
Logfile string
Files []string
Refresh int
Self bool
Debug bool
}
func (p *Params) init() {
var file_path string
flag.StringVar(&p.Url, "url", "http://rancher-metadata.rancher.internal", "Rancher metadata url. RANCHER_TEMPLATE_URL")
flag.StringVar(&p.Prefix, "prefix", "2016-07-29", "Rancher metadata prefix. RANCHER_TEMPLATE_PREFIX")
flag.StringVar(&p.Logfile, "logfile", "/opt/tools/rancher-template/log/rancher-template.log", "Rancher template log fie. RANCHER_TEMPLATE_LOGFILE")
flag.StringVar(&file_path, "templates", "/opt/tools/rancher-template/etc/*.yml", "Templates config files, wildcard allowed between quotes. RANCHER_TEMPLATE_FILES")
flag.IntVar(&p.Refresh, "refresh", 300, "Rancher metadata refresh time in seconds. RANCHER_TEMPLATE_REFRESH")
flag.BoolVar(&p.Self, "self", false, "Get self stack data or all. RANCHER_TEMPLATE_SELF")
flag.BoolVar(&p.Debug, "debug", false, "Run in debug mode. RANCHER_TEMPLATE_DEBUG")
flag.Parse()
p.setEnvVar(&file_path)
p.getFiles(file_path)
}
func (p *Params) setEnvVar(file_path *string) {
var err error
var aux_int int
var aux_bool bool
url := os.Getenv("RANCHER_TEMPLATE_URL")
if len(url) > 0 { p.Url = url }
prefix := os.Getenv("RANCHER_TEMPLATE_PREFIX")
if len(prefix) > 0 { p.Prefix = prefix }
logfile := os.Getenv("RANCHER_TEMPLATE_LOGFILE")
if len(logfile) > 0 { p.Logfile = logfile }
files := os.Getenv("RANCHER_TEMPLATE_FILES")
if len(files) > 0 { *file_path = files }
refresh := os.Getenv("RANCHER_TEMPLATE_REFRESH")
if len(refresh) > 0 {
aux_int, err = strconv.Atoi(refresh)
if err == nil {
p.Refresh = aux_int
}
}
self := os.Getenv("RANCHER_TEMPLATE_SELF")
if len(self) > 0 {
aux_bool, err = strconv.ParseBool(self)
if err == nil {
p.Self = aux_bool
}
}
debug := os.Getenv("RANCHER_TEMPLATE_DEBUG")
if len(debug) > 0 {
aux_bool, err = strconv.ParseBool(debug)
if err == nil {
p.Debug = aux_bool
}
}
}
func (p *Params) getFiles(f string) {
var err error
p.Files , err = filepath.Glob(f)
if err != nil {
log.Fatal(err)
}
}
func getenv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}