-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli_flag_assigned_variables.go
68 lines (58 loc) · 1.96 KB
/
cli_flag_assigned_variables.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
package golden
import (
"fmt"
"github.com/spf13/afero"
"path/filepath"
"strings"
)
type CliFlagAssignedVariables interface {
Variables(c *BaseConfig) (map[string]VariableValueRead, error)
}
var _ CliFlagAssignedVariables = CliFlagAssignedVariable{}
type CliFlagAssignedVariable struct {
varName string
rawValue string
}
func NewCliFlagAssignedVariable(varName, rawValue string) CliFlagAssignedVariable {
return CliFlagAssignedVariable{
varName: varName,
rawValue: rawValue,
}
}
func (v CliFlagAssignedVariable) Variables(c *BaseConfig) (map[string]VariableValueRead, error) {
variableBlocks := Blocks[*VariableBlock](c)
variables := make(map[string]*VariableBlock)
for _, vb := range variableBlocks {
variables[vb.Name()] = vb
}
vb, ok := variables[v.varName]
if !ok {
if c.ignoreUnknownVariables {
return map[string]VariableValueRead{}, nil
}
return nil, fmt.Errorf(`a variable named "%s" was assigned on the command line, but cannot find a variable of that name. To use this value, add a "variable" block to the configuraion`, v.varName)
}
read := vb.parseVariableValueFromString(v.rawValue, false)
return map[string]VariableValueRead{
read.Name: read,
}, nil
}
var _ CliFlagAssignedVariables = CliFlagAssignedVariableFile{}
type CliFlagAssignedVariableFile struct {
varFileName string
}
func NewCliFlagAssignedVariableFile(varFileName string) CliFlagAssignedVariableFile {
return CliFlagAssignedVariableFile{
varFileName: varFileName,
}
}
func (v CliFlagAssignedVariableFile) Variables(c *BaseConfig) (map[string]VariableValueRead, error) {
exist, err := afero.Exists(configFs, v.varFileName)
if err != nil {
return nil, fmt.Errorf("cannot check existance of %s: %+v", v.varFileName, err)
}
if !exist && !strings.HasPrefix(v.varFileName, c.variableConfigFilesDir()) {
return CliFlagAssignedVariableFile{varFileName: filepath.Join(c.variableConfigFilesDir(), v.varFileName)}.Variables(c)
}
return c.readVariablesFromVarFile(v.varFileName)
}