You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/10/01 08:51:42 UTC

[GitHub] mkiiskila closed pull request #101: res - add details flag for res commands

mkiiskila closed pull request #101: res - add details flag for res commands
URL: https://github.com/apache/mynewt-newtmgr/pull/101
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/newtmgr/cli/res.go b/newtmgr/cli/res.go
index af47117c..44263782 100644
--- a/newtmgr/cli/res.go
+++ b/newtmgr/cli/res.go
@@ -36,6 +36,8 @@ import (
 	"mynewt.apache.org/newtmgr/nmxact/xact"
 )
 
+var details bool
+
 func indent(s string, numSpaces int) string {
 	b := make([]byte, numSpaces)
 	for i, _ := range b {
@@ -102,6 +104,33 @@ func extractResKv(params []string) (map[string]interface{}, error) {
 	return m, nil
 }
 
+func printCode(code coap.COAPCode) string {
+	var s string
+	class := (code & 0xE0)>>5
+	d1 := (code & 0x18)>>3
+	d2 := code & 0x07
+	s += fmt.Sprintf("CoAP Response Code: %d.%d%d %s\n", class, d1, d2, code)
+	return s
+}
+
+func printDetails(sres interface{}) string {
+	var s string
+	switch sres := sres.(type) {
+	case *xact.GetResResult:
+		s += printCode(sres.Code)
+		if sres.Token != nil {
+			s += fmt.Sprintf("CoAP Response Token: %v\n", hex.EncodeToString(sres.Token))
+		}
+	case *xact.PutResResult:
+		s += printCode(sres.Code)
+	case *xact.PostResResult:
+		s += printCode(sres.Code)
+	case *xact.DeleteResResult:
+		s += printCode(sres.Code)
+	}
+	return s
+}
+
 /* Helper functions to convert JSON object into pretty format
    Adapted from elastic/beats/libbeat/common/mapstr.go
 */
@@ -192,6 +221,10 @@ func resGetCmd(cmd *cobra.Command, args []string) {
 	if sres.Value != nil {
 		fmt.Printf("%s\n", resResponseStr(c.Path, sres.Value))
 	}
+
+	if details {
+		fmt.Printf(printDetails(sres))
+	}
 }
 
 func resPutCmd(cmd *cobra.Command, args []string) {
@@ -243,6 +276,10 @@ func resPutCmd(cmd *cobra.Command, args []string) {
 	if sres.Value != nil {
 		fmt.Printf("%s\n", resResponseStr(c.Path, sres.Value))
 	}
+
+	if details {
+		fmt.Printf(printDetails(sres))
+	}
 }
 
 func resPostCmd(cmd *cobra.Command, args []string) {
@@ -294,6 +331,10 @@ func resPostCmd(cmd *cobra.Command, args []string) {
 	if sres.Value != nil {
 		fmt.Printf("%s\n", resResponseStr(c.Path, sres.Value))
 	}
+
+	if details {
+		fmt.Printf(printDetails(sres))
+	}
 }
 
 func resDeleteCmd(cmd *cobra.Command, args []string) {
@@ -345,6 +386,10 @@ func resDeleteCmd(cmd *cobra.Command, args []string) {
 	if sres.Value != nil {
 		fmt.Printf("%s\n", resResponseStr(c.Path, sres.Value))
 	}
+
+	if details {
+		fmt.Printf(printDetails(sres))
+	}
 }
 
 func resCmd() *cobra.Command {
@@ -380,5 +425,7 @@ func resCmd() *cobra.Command {
 		Run:   resDeleteCmd,
 	})
 
+	resCmd.PersistentFlags().BoolVarP(&details, "details", "d", false, "Show more details about the CoAP response")
+
 	return resCmd
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services