From 2b526af741834f4000f7be271134482aa332a91a Mon Sep 17 00:00:00 2001 From: Christian Ege Date: Wed, 9 Aug 2023 16:17:51 +0000 Subject: [PATCH] feat: add the command getSchema This command is handy to retrieve the schema used on the device. This provides the information about the available parameters and the minimum and maximum value ranges. As well as their defaults. Signed-off-by: Christian Ege --- cmd/ovp8xx/cmd/getSchema.go | 41 +++++++++++++++++++++++++++++++++++++ pkg/ovp8xx/rpc.go | 17 +++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 cmd/ovp8xx/cmd/getSchema.go diff --git a/cmd/ovp8xx/cmd/getSchema.go b/cmd/ovp8xx/cmd/getSchema.go new file mode 100644 index 0000000..0553410 --- /dev/null +++ b/cmd/ovp8xx/cmd/getSchema.go @@ -0,0 +1,41 @@ +/* +Copyright © 2023 Christian Ege +*/ +package cmd + +import ( + "fmt" + + "github.com/graugans/go-ovp8xx/pkg/ovp8xx" + "github.com/spf13/cobra" +) + +func getSchemaCommand(cmd *cobra.Command, args []string) error { + + host, err := rootCmd.PersistentFlags().GetString("ip") + if err != nil { + return err + } + + o3r := ovp8xx.NewClient( + ovp8xx.WithHost(host), + ) + + if result, err := o3r.GetSchema(); err != nil { + return err + } else { + fmt.Printf("%s\n", result) + } + return nil +} + +// getCmd represents the get command +var getSchemaCmd = &cobra.Command{ + Use: "getSchema", + Short: "Retrieve the currently used JSON schema from the device", + RunE: getSchemaCommand, +} + +func init() { + rootCmd.AddCommand(getSchemaCmd) +} diff --git a/pkg/ovp8xx/rpc.go b/pkg/ovp8xx/rpc.go index 0223601..a68fa01 100644 --- a/pkg/ovp8xx/rpc.go +++ b/pkg/ovp8xx/rpc.go @@ -94,3 +94,20 @@ func (device *Client) FactoryReset(keepNetworkSettings bool) error { } return client.Call("factoryReset", arg, nil) } + +func (device *Client) GetSchema() (string, error) { + client, err := xmlrpc.NewClient(device.url) + if err != nil { + return "", err + } + defer client.Close() + + result := &struct { + JSON string + }{} + + if err := client.Call("getSchema", nil, result); err != nil { + return "", err + } + return result.JSON, nil +}