forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vars_file_arg.go
40 lines (30 loc) · 875 Bytes
/
vars_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
package template
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
"gopkg.in/yaml.v2"
)
type VarsFileArg struct {
FS boshsys.FileSystem
Vars StaticVariables
}
func (a *VarsFileArg) UnmarshalFlag(filePath string) error {
if a.FS == nil {
a.FS = boshsys.NewOsFileSystemWithStrictTempRoot(boshlog.NewLogger(boshlog.LevelNone))
}
if len(filePath) == 0 {
return bosherr.Errorf("Expected file path to be non-empty")
}
bytes, err := a.FS.ReadFile(filePath)
if err != nil {
return bosherr.WrapErrorf(err, "Reading variables file '%s'", filePath)
}
var vars StaticVariables
err = yaml.Unmarshal(bytes, &vars)
if err != nil {
return bosherr.WrapErrorf(err, "Deserializing variables file '%s'", filePath)
}
a.Vars = vars
return nil
}