You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2019/07/24 16:50:55 UTC

[mynewt-newtmgr] branch master updated: res: `-j` option to specify payload as JSON

This is an automated email from the ASF dual-hosted git repository.

ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newtmgr.git


The following commit(s) were added to refs/heads/master by this push:
     new a6d89ab  res: `-j` option to specify payload as JSON
a6d89ab is described below

commit a6d89abeb97759484d823ddd23640e1afe14b8c8
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Wed May 29 17:23:10 2019 -0700

    res: `-j` option to specify payload as JSON
    
    Before this commit, the `res` command accepted a payload in the form of
    `k=v` pairs, e.g.,
    
        newtmgr res put /my/res a=start dur=1
    
    This commit adds a new option to the `res` command: `-j`.  When this
    option is specified, the CoAP message body is expressed as a JSON
    string, e.g.,
    
        newtmgr res -j put /my/res '{"a":"start","dur":1}'
    
    This allows for more complex payloads, and it removes ambiguity about
    what data types are used.
---
 newtmgr/cli/res.go | 37 ++++++++++++++++++++++++++++++++++---
 1 file changed, 34 insertions(+), 3 deletions(-)

diff --git a/newtmgr/cli/res.go b/newtmgr/cli/res.go
index 1a95bb7..9131fcd 100644
--- a/newtmgr/cli/res.go
+++ b/newtmgr/cli/res.go
@@ -36,6 +36,7 @@ import (
 )
 
 var details bool
+var resJson bool
 
 func indent(s string, numSpaces int) string {
 	b := make([]byte, numSpaces)
@@ -220,7 +221,7 @@ func resGetCmd(cmd *cobra.Command, args []string) {
 	}
 }
 
-func parsePutPostMap(args []string) ([]byte, error) {
+func parsePayloadMap(args []string) ([]byte, error) {
 	if len(args) == 0 {
 		return nil, nil
 	}
@@ -238,6 +239,33 @@ func parsePutPostMap(args []string) ([]byte, error) {
 	return b, nil
 }
 
+func parsePayloadJson(args []string) ([]byte, error) {
+	if len(args) == 0 {
+		return nil, nil
+	}
+
+	var obj interface{}
+
+	if err := json.Unmarshal([]byte(args[0]), &obj); err != nil {
+		return nil, util.ChildNewtError(err)
+	}
+
+	b, err := nmxutil.EncodeCbor(obj)
+	if err != nil {
+		return nil, util.ChildNewtError(err)
+	}
+
+	return b, nil
+}
+
+func parsePayload(args []string) ([]byte, error) {
+	if resJson {
+		return parsePayloadJson(args)
+	} else {
+		return parsePayloadMap(args)
+	}
+}
+
 func resPutCmd(cmd *cobra.Command, args []string) {
 	if len(args) == 0 {
 		nmUsage(cmd, nil)
@@ -248,7 +276,7 @@ func resPutCmd(cmd *cobra.Command, args []string) {
 		nmUsage(nil, err)
 	}
 
-	m, err := parsePutPostMap(args[1:])
+	m, err := parsePayload(args[1:])
 	if err != nil {
 		nmUsage(cmd, err)
 	}
@@ -289,7 +317,7 @@ func resPostCmd(cmd *cobra.Command, args []string) {
 		nmUsage(nil, err)
 	}
 
-	m, err := parsePutPostMap(args[1:])
+	m, err := parsePayload(args[1:])
 	if err != nil {
 		nmUsage(cmd, err)
 	}
@@ -378,6 +406,9 @@ func resCmd() *cobra.Command {
 		},
 	}
 
+	resCmd.PersistentFlags().BoolVarP(&resJson, "json", "j", false,
+		"Accept a JSON string for the CoAP message body (not `k=v` pairs)")
+
 	resCmd.AddCommand(&cobra.Command{
 		Use:   "get <path>",
 		Short: "Send a CoAP GET request",