-
Notifications
You must be signed in to change notification settings - Fork 162
/
ops_file_arg.go
53 lines (40 loc) · 1.13 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
package opts
import (
"fmt"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
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 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 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)
}
for i := range opDefs {
errorStr := fmt.Sprintf("operation [%d] in %s failed", i, filePath)
opDefs[i].Error = &errorStr
}
ops, err := patch.NewOpsFromDefinitions(opDefs)
if err != nil {
return bosherr.WrapErrorf(err, "Building ops")
}
(*a).Ops = ops
return nil
}