forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
var_file_arg.go
43 lines (32 loc) · 983 Bytes
/
var_file_arg.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
package template
import (
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type VarFileArg struct {
FS boshsys.FileSystem
Vars StaticVariables
}
func (a *VarFileArg) UnmarshalFlag(data string) error {
pieces := strings.SplitN(data, "=", 2)
if len(pieces) != 2 {
return bosherr.Errorf("Expected var '%s' to be in format 'name=path'", data)
}
if len(pieces[0]) == 0 {
return bosherr.Errorf("Expected var '%s' to specify non-empty name", data)
}
if len(pieces[1]) == 0 {
return bosherr.Errorf("Expected var '%s' to specify non-empty path", data)
}
absPath, err := a.FS.ExpandPath(pieces[1])
if err != nil {
return bosherr.WrapErrorf(err, "Getting absolute path '%s'", pieces[1])
}
bytes, err := a.FS.ReadFile(absPath)
if err != nil {
return bosherr.WrapErrorf(err, "Reading variable from file '%s'", absPath)
}
(*a).Vars = StaticVariables{pieces[0]: string(bytes)}
return nil
}