diff --git a/cmd/ovp8xx/cmd/getinit.go b/cmd/ovp8xx/cmd/getinit.go index 528bb12..7bb70a9 100644 --- a/cmd/ovp8xx/cmd/getinit.go +++ b/cmd/ovp8xx/cmd/getinit.go @@ -23,7 +23,9 @@ func getInitCommand(cmd *cobra.Command, args []string) error { if result, err = o3r.GetInit(); err != nil { return err } - helper.printJSONResult(result.String()) + if err = helper.printJSONResult(result.String()); err != nil { + return err + } return nil } diff --git a/cmd/ovp8xx/cmd/helper.go b/cmd/ovp8xx/cmd/helper.go new file mode 100644 index 0000000..a6f9877 --- /dev/null +++ b/cmd/ovp8xx/cmd/helper.go @@ -0,0 +1,56 @@ +package cmd + +import ( + "encoding/json" + "errors" + "fmt" + + "github.com/spf13/cobra" +) + +type helperConfig struct { + pretty bool + host string + pointers []string +} + +func (c *helperConfig) printJSONResult(data string) error { + var message string = data + if c.pretty { + var js json.RawMessage + if err := json.Unmarshal([]byte(data), &js); err != nil { + return errors.New("malformed json") + } + jsonIndented, err := json.MarshalIndent(js, "", " ") + if err != nil { + return err + } + message = string(jsonIndented) + } + fmt.Print(message) + return nil +} + +func (c *helperConfig) hostname() string { + return c.host +} + +func (c *helperConfig) jsonPointers() []string { + return c.pointers +} + +func NewHelper(cmd *cobra.Command) (helperConfig, error) { + var conf = helperConfig{} + var err error + conf.pretty, err = cmd.Flags().GetBool("pretty") + if err != nil { + return conf, err + } + conf.host, err = rootCmd.PersistentFlags().GetString("ip") + if err != nil { + return conf, err + } + // Pointers can be empty + conf.pointers, _ = cmd.Flags().GetStringSlice("pointer") + return conf, nil +}