forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops_file_arg.go
41 lines (31 loc) · 854 Bytes
/
ops_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
package cmd
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
"github.com/cppforlife/go-patch/patch"
"gopkg.in/yaml.v2"
)
type OpsFileArg struct {
FS boshsys.FileSystem
Ops patch.Ops
}
func (a *OpsFileArg) UnmarshalFlag(filePath string) error {
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 ops file '%s'", filePath)
}
var opDefs []patch.OpDefinition
err = yaml.Unmarshal(bytes, &opDefs)
if err != nil {
return bosherr.WrapErrorf(err, "Deserializing ops file '%s'", filePath)
}
ops, err := patch.NewOpsFromDefinitions(opDefs)
if err != nil {
return bosherr.WrapErrorf(err, "Building ops")
}
(*a).Ops = ops
return nil
}