From 3111837aba1ad8e8c08bbce1403c4ec6622abe65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Br=C3=BCderl?= Date: Sun, 31 Mar 2019 23:10:07 +0200 Subject: [PATCH] cli: add command to set desired state --- cmd/inf/state.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/cmd/inf/state.go b/cmd/inf/state.go index b6c5de2ee..12687b172 100644 --- a/cmd/inf/state.go +++ b/cmd/inf/state.go @@ -14,11 +14,16 @@ import ( "github.com/golang/protobuf/ptypes" "github.com/golang/protobuf/ptypes/timestamp" + "strings" + "github.com/infinimesh/infinimesh/pkg/shadow/shadowpb" + + _struct "github.com/golang/protobuf/ptypes/struct" ) func init() { stateCmd.AddCommand(stateGetCmd) + stateCmd.AddCommand(stateSetCmd) rootCmd.AddCommand(stateCmd) } @@ -32,6 +37,29 @@ var stateCmd = &cobra.Command{ }, } +var stateSetCmd = &cobra.Command{ + Use: "set ", + Args: cobra.ExactArgs(2), + Run: func(cmd *cobra.Command, args []string) { + u := &jsonpb.Unmarshaler{} + var state _struct.Value + err := u.Unmarshal(strings.NewReader(args[1]), &state) + if err != nil { + fmt.Fprintf(os.Stderr, "Invalid state: %v\n", err) + } + + _, err = shadowClient.PatchDesiredState(ctx, &shadowpb.PatchDesiredStateRequest{ + Id: args[0], + Data: &state, + }) + + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to set state: %v", err) + } + fmt.Println("Successfully set state.") + }, +} + var stateGetCmd = &cobra.Command{ Use: "get ", Args: cobra.ExactArgs(1),