/
parse.go
53 lines (41 loc) · 1.1 KB
/
parse.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 cli
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"github.com/spf13/pflag"
)
type XCreatePoolInputs createPoolInputs
type XCreatePoolInputsExceptions struct {
XCreatePoolInputs
Other *string // Other won't raise an error
}
// UnmarshalJSON should error if there are fields unexpected
func (release *createPoolInputs) UnmarshalJSON(data []byte) error {
var createPoolE XCreatePoolInputsExceptions
dec := json.NewDecoder(bytes.NewReader(data))
dec.DisallowUnknownFields() // Force
if err := dec.Decode(&createPoolE); err != nil {
return err
}
*release = createPoolInputs(createPoolE.XCreatePoolInputs)
return nil
}
func parseCreatePoolFlags(fs *pflag.FlagSet) (*createPoolInputs, error) {
pool := &createPoolInputs{}
poolFile, _ := fs.GetString(FlagPoolFile)
if poolFile == "" {
return nil, fmt.Errorf("must pass in a pool json using the --%s flag", FlagPoolFile)
}
contents, err := ioutil.ReadFile(poolFile)
if err != nil {
return nil, err
}
// make exception if unknown field exists
err = pool.UnmarshalJSON(contents)
if err != nil {
return nil, err
}
return pool, nil
}