forked from cloudfoundry/bosh-cli
/
file_bytes_arg.go
51 lines (38 loc) · 936 Bytes
/
file_bytes_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
package opts
import (
"io/ioutil"
"os"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type FileBytesArg struct {
FS boshsys.FileSystem
Bytes []byte
}
func (a *FileBytesArg) UnmarshalFlag(data string) error {
if a.FS == nil {
a.FS = boshsys.NewOsFileSystemWithStrictTempRoot(boshlog.NewLogger(boshlog.LevelNone))
}
if len(data) == 0 {
return bosherr.Errorf("Expected file path to be non-empty")
}
if data == "-" {
bs, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return bosherr.WrapErrorf(err, "Reading from stdin")
}
(*a).Bytes = bs
return nil
}
absPath, err := a.FS.ExpandPath(data)
if err != nil {
return bosherr.WrapErrorf(err, "Getting absolute path '%s'", data)
}
bytes, err := a.FS.ReadFile(absPath)
if err != nil {
return err
}
(*a).Bytes = bytes
return nil
}