Skip to content

Commit

Permalink
cli: add command to set desired state
Browse files Browse the repository at this point in the history
  • Loading branch information
birdayz committed Mar 31, 2019
1 parent fde2def commit 3111837
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions cmd/inf/state.go
Expand Up @@ -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)
}

Expand All @@ -32,6 +37,29 @@ var stateCmd = &cobra.Command{
},
}

var stateSetCmd = &cobra.Command{
Use: "set <deviceID> <JSON State>",
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 <deviceID>",
Args: cobra.ExactArgs(1),
Expand Down

0 comments on commit 3111837

Please sign in to comment.