1package cmd
2
3import (
4	bosherr "github.com/cloudfoundry/bosh-utils/errors"
5	boshsys "github.com/cloudfoundry/bosh-utils/system"
6	"github.com/cppforlife/go-patch/patch"
7	"gopkg.in/yaml.v2"
8)
9
10type OpsFileArg struct {
11	FS boshsys.FileSystem
12
13	Ops patch.Ops
14}
15
16func (a *OpsFileArg) UnmarshalFlag(filePath string) error {
17	if len(filePath) == 0 {
18		return bosherr.Errorf("Expected file path to be non-empty")
19	}
20
21	bytes, err := a.FS.ReadFile(filePath)
22	if err != nil {
23		return bosherr.WrapErrorf(err, "Reading ops file '%s'", filePath)
24	}
25
26	var opDefs []patch.OpDefinition
27
28	err = yaml.Unmarshal(bytes, &opDefs)
29	if err != nil {
30		return bosherr.WrapErrorf(err, "Deserializing ops file '%s'", filePath)
31	}
32
33	ops, err := patch.NewOpsFromDefinitions(opDefs)
34	if err != nil {
35		return bosherr.WrapErrorf(err, "Building ops")
36	}
37
38	(*a).Ops = ops
39
40	return nil
41}
42