forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
var_flags.go
58 lines (42 loc) · 1.73 KB
/
var_flags.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
package cmd
import (
cfgtypes "config_server/types"
boshtpl "github.com/cloudfoundry/bosh-cli/director/template"
)
// Shared
type VarFlags struct {
VarKVs []boshtpl.VarKV `long:"var" short:"v" value-name:"VAR=VALUE" description:"Set variable"`
VarFiles []boshtpl.VarFileArg `long:"var-file" value-name:"VAR=PATH" description:"Set variable to file contents"`
VarsFiles []boshtpl.VarsFileArg `long:"vars-file" short:"l" value-name:"PATH" description:"Load variables from a YAML file"`
VarsEnvs []boshtpl.VarsEnvArg `long:"vars-env" value-name:"PREFIX" description:"Load variables from environment variables (e.g.: 'MY' to load MY_var=value)"`
VarsFSStore VarsFSStore `long:"vars-store" value-name:"PATH" description:"Load/save variables from/to a YAML file"`
}
func (f VarFlags) AsVariables() boshtpl.Variables {
var firstToUse []boshtpl.Variables
firstToUse = append(firstToUse, f.kvsAsVars())
for i, _ := range f.VarFiles {
firstToUse = append(firstToUse, f.VarFiles[len(f.VarFiles)-i-1].Vars)
}
for i, _ := range f.VarsFiles {
firstToUse = append(firstToUse, f.VarsFiles[len(f.VarsFiles)-i-1].Vars)
}
for i, _ := range f.VarsEnvs {
firstToUse = append(firstToUse, f.VarsEnvs[len(f.VarsEnvs)-i-1].Vars)
}
store := &f.VarsFSStore
if f.VarsFSStore.IsSet() {
firstToUse = append(firstToUse, store)
}
vars := boshtpl.NewMultiVars(firstToUse)
if f.VarsFSStore.IsSet() {
store.ValueGeneratorFactory = cfgtypes.NewValueGeneratorConcrete(NewVarsCertLoader(vars))
}
return vars
}
func (f VarFlags) kvsAsVars() boshtpl.Variables {
vars := boshtpl.StaticVariables{}
for _, kv := range f.VarKVs {
vars[kv.Name] = kv.Value
}
return vars
}