You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by st...@apache.org on 2016/01/28 20:04:21 UTC

[1/2] incubator-mynewt-newt git commit: display help for image list. Have the connection profile be an interface, so that other programs who want to use the connection libraries, can provide their own connection profiles.

Repository: incubator-mynewt-newt
Updated Branches:
  refs/heads/master d1008e6d9 -> fd1b20289


display help for image list.  Have the connection profile be an interface, so that other programs who want to use the connection libraries, can provide their own connection profiles.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/fd1b2028
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/fd1b2028
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/fd1b2028

Branch: refs/heads/master
Commit: fd1b202899ff0416abc7b373f556433272d893df
Parents: a724701
Author: Sterling Hughes <st...@apache.org>
Authored: Thu Jan 28 11:04:06 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Thu Jan 28 11:04:16 2016 -0800

----------------------------------------------------------------------
 newtmgr/cli/cp.go               | 45 +++++++++++++++++++++++++-----------
 newtmgr/newtmgr.go              | 16 +++++++------
 newtmgr/protocol/stats.go       |  9 ++++----
 newtmgr/transport/conn.go       |  8 +++----
 newtmgr/transport/connserial.go | 11 +++++----
 5 files changed, 56 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fd1b2028/newtmgr/cli/cp.go
----------------------------------------------------------------------
diff --git a/newtmgr/cli/cp.go b/newtmgr/cli/cp.go
index fe291a0..7dca0c7 100644
--- a/newtmgr/cli/cp.go
+++ b/newtmgr/cli/cp.go
@@ -16,19 +16,26 @@
 package cli
 
 import (
+	"log"
+
 	"git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/util"
 	"github.com/mitchellh/go-homedir"
-	"log"
 )
 
 type CpMgr struct {
 	cDb *util.CfgDb
 }
 
+type NewtmgrConnProfile interface {
+	Name() string
+	Type() string
+	ConnString() string
+}
+
 type ConnProfile struct {
-	Name       string
-	Type       string
-	ConnString string
+	MyName       string
+	MyType       string
+	MyConnString string
 }
 
 func NewCpMgr() (*CpMgr, error) {
@@ -91,16 +98,16 @@ func (cpm *CpMgr) DeleteConnProfile(name string) error {
 }
 
 func (cpm *CpMgr) AddConnProfile(cp *ConnProfile) error {
-	sect := "_conn_profile_" + cp.Name
+	sect := "_conn_profile_" + cp.Name()
 	cDb := cpm.cDb
 
 	// First serialize the conn profile into the configuration database
-	cDb.SetKey(sect, "name", cp.Name)
-	cDb.SetKey(sect, "type", cp.Type)
-	cDb.SetKey(sect, "connstring", cp.ConnString)
+	cDb.SetKey(sect, "name", cp.Name())
+	cDb.SetKey(sect, "type", cp.Type())
+	cDb.SetKey(sect, "connstring", cp.ConnString())
 
 	// Then write the ConnProfile to the ConnProfileList
-	cDb.SetKey("conn_profile_list", cp.Name, cp.Name)
+	cDb.SetKey("conn_profile_list", cp.Name(), cp.Name())
 
 	return nil
 }
@@ -127,11 +134,11 @@ func (cpm *CpMgr) GetConnProfile(pName string) (*ConnProfile, error) {
 	for k, v := range cpVals {
 		switch k {
 		case "name":
-			cp.Name = v
+			cp.MyName = v
 		case "type":
-			cp.Type = v
+			cp.MyType = v
 		case "connstring":
-			cp.ConnString = v
+			cp.MyConnString = v
 		default:
 			return nil, util.NewNewtError(
 				"Invalid key " + k + " with val " + v)
@@ -141,9 +148,21 @@ func (cpm *CpMgr) GetConnProfile(pName string) (*ConnProfile, error) {
 	return cp, nil
 }
 
+func (cp *ConnProfile) Name() string {
+	return cp.MyName
+}
+
+func (cp *ConnProfile) Type() string {
+	return cp.MyType
+}
+
+func (cp *ConnProfile) ConnString() string {
+	return cp.MyConnString
+}
+
 func NewConnProfile(pName string) (*ConnProfile, error) {
 	cp := &ConnProfile{}
-	cp.Name = pName
+	cp.MyName = pName
 
 	return cp, nil
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fd1b2028/newtmgr/newtmgr.go
----------------------------------------------------------------------
diff --git a/newtmgr/newtmgr.go b/newtmgr/newtmgr.go
index d2d9992..9aed1db 100644
--- a/newtmgr/newtmgr.go
+++ b/newtmgr/newtmgr.go
@@ -75,11 +75,11 @@ func connProfileAddCmd(cmd *cobra.Command, args []string) {
 		s := strings.Split(vdef, "=")
 		switch s[0] {
 		case "name":
-			cp.Name = s[1]
+			cp.MyName = s[1]
 		case "type":
-			cp.Type = s[1]
+			cp.MyType = s[1]
 		case "connstring":
-			cp.ConnString = s[1]
+			cp.MyConnString = s[1]
 		default:
 			nmUsage(cmd, util.NewNewtError("Unknown variable "+s[0]))
 		}
@@ -112,7 +112,7 @@ func connProfileShowCmd(cmd *cobra.Command, args []string) {
 	for _, cp := range cpList {
 		// Print out the connection profile, if name is "" or name
 		// matches cp.Name
-		if name != "" && cp.Name != name {
+		if name != "" && cp.Name() != name {
 			continue
 		}
 
@@ -120,8 +120,8 @@ func connProfileShowCmd(cmd *cobra.Command, args []string) {
 			found = true
 			fmt.Printf("Connection profiles: \n")
 		}
-		fmt.Printf("  %s: type=%s, connstring='%s'\n", cp.Name, cp.Type,
-			cp.ConnString)
+		fmt.Printf("  %s: type=%s, connstring='%s'\n", cp.MyName, cp.MyType,
+			cp.MyConnString)
 	}
 
 	if !found {
@@ -697,7 +697,9 @@ func imageCmd() *cobra.Command {
 	imageCmd := &cobra.Command{
 		Use:   "image",
 		Short: "Manage images on remote instance",
-		Run:   imageListCmd,
+		Run: func(cmd *cobra.Command, args []string) {
+			cmd.Help()
+		},
 	}
 
 	listCmd := &cobra.Command{

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fd1b2028/newtmgr/protocol/stats.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/stats.go b/newtmgr/protocol/stats.go
index bb8d6a6..30cb710 100644
--- a/newtmgr/protocol/stats.go
+++ b/newtmgr/protocol/stats.go
@@ -31,13 +31,14 @@ const (
 )
 
 type StatsReadReq struct {
-	Name string `json:"n"`
+	Name string `json:"name"`
 }
 
 type StatsReadRsp struct {
-	ReturnCode int                    `json:"r"`
-	Name       string                 `json:"n"`
-	Fields     map[string]interface{} `json:"f"`
+	ReturnCode int                    `json:"rc"`
+	Name       string                 `json:"name"`
+	Group      string                 `json:"group"`
+	Fields     map[string]interface{} `json:"fields"`
 }
 
 func NewStatsReadReq() (*StatsReadReq, error) {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fd1b2028/newtmgr/transport/conn.go
----------------------------------------------------------------------
diff --git a/newtmgr/transport/conn.go b/newtmgr/transport/conn.go
index e2d2ece..560b349 100644
--- a/newtmgr/transport/conn.go
+++ b/newtmgr/transport/conn.go
@@ -23,7 +23,7 @@ import (
 )
 
 type Conn interface {
-	Open(cp *cli.ConnProfile) error
+	Open(cp cli.NewtmgrConnProfile) error
 	ReadPacket() (*Packet, error)
 	WritePacket(pkt *Packet) error
 }
@@ -56,18 +56,18 @@ func (pkt *Packet) GetBytes() []byte {
 	return pkt.buffer.Bytes()
 }
 
-func NewConn(cp *cli.ConnProfile) (Conn, error) {
+func NewConn(cp cli.NewtmgrConnProfile) (Conn, error) {
 	// Based on ConnProfile, instantiate the right type of conn object, that
 	// implements the conn interface.
 	var c Conn
-	switch cp.Type {
+	switch cp.Type() {
 	case "serial":
 		c = &ConnSerial{}
 		if err := c.Open(cp); err != nil {
 			return nil, err
 		}
 	default:
-		return nil, util.NewNewtError("Invalid conn profile " + cp.Type +
+		return nil, util.NewNewtError("Invalid conn profile " + cp.Type() +
 			" not implemented")
 	}
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fd1b2028/newtmgr/transport/connserial.go
----------------------------------------------------------------------
diff --git a/newtmgr/transport/connserial.go b/newtmgr/transport/connserial.go
index b0e51ee..1115fa4 100644
--- a/newtmgr/transport/connserial.go
+++ b/newtmgr/transport/connserial.go
@@ -28,18 +28,18 @@ import (
 )
 
 type ConnSerial struct {
-	cp            *cli.ConnProfile
+	cp            cli.NewtmgrConnProfile
 	currentPacket *Packet
 
 	scanner       *bufio.Scanner
 	serialChannel *serial.Port
 }
 
-func (cs *ConnSerial) Open(cp *cli.ConnProfile) error {
+func (cs *ConnSerial) Open(cp cli.NewtmgrConnProfile) error {
 	var err error
 
 	c := &serial.Config{
-		Name: cp.ConnString,
+		Name: cp.ConnString(),
 		Baud: 115200,
 	}
 
@@ -77,8 +77,9 @@ func (cs *ConnSerial) ReadPacket() (*Packet, error) {
 
 		data, err := base64.StdEncoding.DecodeString(base64Data)
 		if err != nil {
-			return nil, util.NewNewtError(fmt.Sprintf("Couldn't decode base64 string: %b",
-				line))
+			return nil, util.NewNewtError(
+				fmt.Sprintf("Couldn't decode base64 string: %b",
+					line))
 		}
 
 		if line[0] == 6 && line[1] == 9 {


[2/2] incubator-mynewt-newt git commit: add HasSect() and HasKey() functions to cfg db

Posted by st...@apache.org.
add HasSect() and HasKey() functions to cfg db


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/a7247014
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/a7247014
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/a7247014

Branch: refs/heads/master
Commit: a72470143a9ce6bd1b6ca07904c20fd113e70670
Parents: d1008e6
Author: Sterling Hughes <st...@apache.org>
Authored: Mon Jan 25 17:34:51 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Thu Jan 28 11:04:16 2016 -0800

----------------------------------------------------------------------
 util/cfgdb.go | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/a7247014/util/cfgdb.go
----------------------------------------------------------------------
diff --git a/util/cfgdb.go b/util/cfgdb.go
index e9f079d..0c8dd96 100644
--- a/util/cfgdb.go
+++ b/util/cfgdb.go
@@ -18,8 +18,9 @@ package util
 import (
 	"database/sql"
 	"fmt"
-	_ "github.com/mattn/go-sqlite3"
 	"log"
+
+	_ "github.com/mattn/go-sqlite3"
 )
 
 type CfgDb struct {
@@ -101,6 +102,28 @@ func (c *CfgDb) Init(prefix string, dbPath string) error {
 	return nil
 }
 
+func (c *CfgDb) HasSect(sect string) bool {
+	_, ok := c.Config[sect]
+	if !ok {
+		return false
+	}
+	return true
+}
+
+func (c *CfgDb) HasKey(sect string, key string) bool {
+	sMap, ok := c.Config[sect]
+	if !ok {
+		return false
+	}
+
+	_, ok = sMap[key]
+	if !ok {
+		return false
+	}
+
+	return true
+}
+
 func (c *CfgDb) GetKey(sect string, key string) (string, error) {
 	sMap, ok := c.Config[sect]
 	if !ok {


Re: [1/2] incubator-mynewt-newt git commit: display help for image list. Have the connection profile be an interface, so that other programs who want to use the connection libraries, can provide their own connection profiles.

Posted by Sterling Hughes <st...@apache.org>.
I should point out that one other commit made it in here.  I've
changed the fields back from statsreadresp to be a bit more
descriptive, and decided to burn a few bytes.

sterling

On Thu, Jan 28, 2016 at 11:04 AM,  <st...@apache.org> wrote:
> Repository: incubator-mynewt-newt
> Updated Branches:
>   refs/heads/master d1008e6d9 -> fd1b20289
>
>
> display help for image list.  Have the connection profile be an interface, so that other programs who want to use the connection libraries, can provide their own connection profiles.
>
>
> Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
> Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/fd1b2028
> Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/fd1b2028
> Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/fd1b2028
>
> Branch: refs/heads/master
> Commit: fd1b202899ff0416abc7b373f556433272d893df
> Parents: a724701
> Author: Sterling Hughes <st...@apache.org>
> Authored: Thu Jan 28 11:04:06 2016 -0800
> Committer: Sterling Hughes <st...@apache.org>
> Committed: Thu Jan 28 11:04:16 2016 -0800
>
> ----------------------------------------------------------------------
>  newtmgr/cli/cp.go               | 45 +++++++++++++++++++++++++-----------
>  newtmgr/newtmgr.go              | 16 +++++++------
>  newtmgr/protocol/stats.go       |  9 ++++----
>  newtmgr/transport/conn.go       |  8 +++----
>  newtmgr/transport/connserial.go | 11 +++++----
>  5 files changed, 56 insertions(+), 33 deletions(-)
> ----------------------------------------------------------------------
>
>
> http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fd1b2028/newtmgr/cli/cp.go
> ----------------------------------------------------------------------
> diff --git a/newtmgr/cli/cp.go b/newtmgr/cli/cp.go
> index fe291a0..7dca0c7 100644
> --- a/newtmgr/cli/cp.go
> +++ b/newtmgr/cli/cp.go
> @@ -16,19 +16,26 @@
>  package cli
>
>  import (
> +       "log"
> +
>         "git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/util"
>         "github.com/mitchellh/go-homedir"
> -       "log"
>  )
>
>  type CpMgr struct {
>         cDb *util.CfgDb
>  }
>
> +type NewtmgrConnProfile interface {
> +       Name() string
> +       Type() string
> +       ConnString() string
> +}
> +
>  type ConnProfile struct {
> -       Name       string
> -       Type       string
> -       ConnString string
> +       MyName       string
> +       MyType       string
> +       MyConnString string
>  }
>
>  func NewCpMgr() (*CpMgr, error) {
> @@ -91,16 +98,16 @@ func (cpm *CpMgr) DeleteConnProfile(name string) error {
>  }
>
>  func (cpm *CpMgr) AddConnProfile(cp *ConnProfile) error {
> -       sect := "_conn_profile_" + cp.Name
> +       sect := "_conn_profile_" + cp.Name()
>         cDb := cpm.cDb
>
>         // First serialize the conn profile into the configuration database
> -       cDb.SetKey(sect, "name", cp.Name)
> -       cDb.SetKey(sect, "type", cp.Type)
> -       cDb.SetKey(sect, "connstring", cp.ConnString)
> +       cDb.SetKey(sect, "name", cp.Name())
> +       cDb.SetKey(sect, "type", cp.Type())
> +       cDb.SetKey(sect, "connstring", cp.ConnString())
>
>         // Then write the ConnProfile to the ConnProfileList
> -       cDb.SetKey("conn_profile_list", cp.Name, cp.Name)
> +       cDb.SetKey("conn_profile_list", cp.Name(), cp.Name())
>
>         return nil
>  }
> @@ -127,11 +134,11 @@ func (cpm *CpMgr) GetConnProfile(pName string) (*ConnProfile, error) {
>         for k, v := range cpVals {
>                 switch k {
>                 case "name":
> -                       cp.Name = v
> +                       cp.MyName = v
>                 case "type":
> -                       cp.Type = v
> +                       cp.MyType = v
>                 case "connstring":
> -                       cp.ConnString = v
> +                       cp.MyConnString = v
>                 default:
>                         return nil, util.NewNewtError(
>                                 "Invalid key " + k + " with val " + v)
> @@ -141,9 +148,21 @@ func (cpm *CpMgr) GetConnProfile(pName string) (*ConnProfile, error) {
>         return cp, nil
>  }
>
> +func (cp *ConnProfile) Name() string {
> +       return cp.MyName
> +}
> +
> +func (cp *ConnProfile) Type() string {
> +       return cp.MyType
> +}
> +
> +func (cp *ConnProfile) ConnString() string {
> +       return cp.MyConnString
> +}
> +
>  func NewConnProfile(pName string) (*ConnProfile, error) {
>         cp := &ConnProfile{}
> -       cp.Name = pName
> +       cp.MyName = pName
>
>         return cp, nil
>  }
>
> http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fd1b2028/newtmgr/newtmgr.go
> ----------------------------------------------------------------------
> diff --git a/newtmgr/newtmgr.go b/newtmgr/newtmgr.go
> index d2d9992..9aed1db 100644
> --- a/newtmgr/newtmgr.go
> +++ b/newtmgr/newtmgr.go
> @@ -75,11 +75,11 @@ func connProfileAddCmd(cmd *cobra.Command, args []string) {
>                 s := strings.Split(vdef, "=")
>                 switch s[0] {
>                 case "name":
> -                       cp.Name = s[1]
> +                       cp.MyName = s[1]
>                 case "type":
> -                       cp.Type = s[1]
> +                       cp.MyType = s[1]
>                 case "connstring":
> -                       cp.ConnString = s[1]
> +                       cp.MyConnString = s[1]
>                 default:
>                         nmUsage(cmd, util.NewNewtError("Unknown variable "+s[0]))
>                 }
> @@ -112,7 +112,7 @@ func connProfileShowCmd(cmd *cobra.Command, args []string) {
>         for _, cp := range cpList {
>                 // Print out the connection profile, if name is "" or name
>                 // matches cp.Name
> -               if name != "" && cp.Name != name {
> +               if name != "" && cp.Name() != name {
>                         continue
>                 }
>
> @@ -120,8 +120,8 @@ func connProfileShowCmd(cmd *cobra.Command, args []string) {
>                         found = true
>                         fmt.Printf("Connection profiles: \n")
>                 }
> -               fmt.Printf("  %s: type=%s, connstring='%s'\n", cp.Name, cp.Type,
> -                       cp.ConnString)
> +               fmt.Printf("  %s: type=%s, connstring='%s'\n", cp.MyName, cp.MyType,
> +                       cp.MyConnString)
>         }
>
>         if !found {
> @@ -697,7 +697,9 @@ func imageCmd() *cobra.Command {
>         imageCmd := &cobra.Command{
>                 Use:   "image",
>                 Short: "Manage images on remote instance",
> -               Run:   imageListCmd,
> +               Run: func(cmd *cobra.Command, args []string) {
> +                       cmd.Help()
> +               },
>         }
>
>         listCmd := &cobra.Command{
>
> http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fd1b2028/newtmgr/protocol/stats.go
> ----------------------------------------------------------------------
> diff --git a/newtmgr/protocol/stats.go b/newtmgr/protocol/stats.go
> index bb8d6a6..30cb710 100644
> --- a/newtmgr/protocol/stats.go
> +++ b/newtmgr/protocol/stats.go
> @@ -31,13 +31,14 @@ const (
>  )
>
>  type StatsReadReq struct {
> -       Name string `json:"n"`
> +       Name string `json:"name"`
>  }
>
>  type StatsReadRsp struct {
> -       ReturnCode int                    `json:"r"`
> -       Name       string                 `json:"n"`
> -       Fields     map[string]interface{} `json:"f"`
> +       ReturnCode int                    `json:"rc"`
> +       Name       string                 `json:"name"`
> +       Group      string                 `json:"group"`
> +       Fields     map[string]interface{} `json:"fields"`
>  }
>
>  func NewStatsReadReq() (*StatsReadReq, error) {
>
> http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fd1b2028/newtmgr/transport/conn.go
> ----------------------------------------------------------------------
> diff --git a/newtmgr/transport/conn.go b/newtmgr/transport/conn.go
> index e2d2ece..560b349 100644
> --- a/newtmgr/transport/conn.go
> +++ b/newtmgr/transport/conn.go
> @@ -23,7 +23,7 @@ import (
>  )
>
>  type Conn interface {
> -       Open(cp *cli.ConnProfile) error
> +       Open(cp cli.NewtmgrConnProfile) error
>         ReadPacket() (*Packet, error)
>         WritePacket(pkt *Packet) error
>  }
> @@ -56,18 +56,18 @@ func (pkt *Packet) GetBytes() []byte {
>         return pkt.buffer.Bytes()
>  }
>
> -func NewConn(cp *cli.ConnProfile) (Conn, error) {
> +func NewConn(cp cli.NewtmgrConnProfile) (Conn, error) {
>         // Based on ConnProfile, instantiate the right type of conn object, that
>         // implements the conn interface.
>         var c Conn
> -       switch cp.Type {
> +       switch cp.Type() {
>         case "serial":
>                 c = &ConnSerial{}
>                 if err := c.Open(cp); err != nil {
>                         return nil, err
>                 }
>         default:
> -               return nil, util.NewNewtError("Invalid conn profile " + cp.Type +
> +               return nil, util.NewNewtError("Invalid conn profile " + cp.Type() +
>                         " not implemented")
>         }
>
>
> http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fd1b2028/newtmgr/transport/connserial.go
> ----------------------------------------------------------------------
> diff --git a/newtmgr/transport/connserial.go b/newtmgr/transport/connserial.go
> index b0e51ee..1115fa4 100644
> --- a/newtmgr/transport/connserial.go
> +++ b/newtmgr/transport/connserial.go
> @@ -28,18 +28,18 @@ import (
>  )
>
>  type ConnSerial struct {
> -       cp            *cli.ConnProfile
> +       cp            cli.NewtmgrConnProfile
>         currentPacket *Packet
>
>         scanner       *bufio.Scanner
>         serialChannel *serial.Port
>  }
>
> -func (cs *ConnSerial) Open(cp *cli.ConnProfile) error {
> +func (cs *ConnSerial) Open(cp cli.NewtmgrConnProfile) error {
>         var err error
>
>         c := &serial.Config{
> -               Name: cp.ConnString,
> +               Name: cp.ConnString(),
>                 Baud: 115200,
>         }
>
> @@ -77,8 +77,9 @@ func (cs *ConnSerial) ReadPacket() (*Packet, error) {
>
>                 data, err := base64.StdEncoding.DecodeString(base64Data)
>                 if err != nil {
> -                       return nil, util.NewNewtError(fmt.Sprintf("Couldn't decode base64 string: %b",
> -                               line))
> +                       return nil, util.NewNewtError(
> +                               fmt.Sprintf("Couldn't decode base64 string: %b",
> +                                       line))
>                 }
>
>                 if line[0] == 6 && line[1] == 9 {
>

Re: [1/2] incubator-mynewt-newt git commit: display help for image list. Have the connection profile be an interface, so that other programs who want to use the connection libraries, can provide their own connection profiles.

Posted by Sterling Hughes <st...@apache.org>.
I should point out that one other commit made it in here.  I've
changed the fields back from statsreadresp to be a bit more
descriptive, and decided to burn a few bytes.

sterling

On Thu, Jan 28, 2016 at 11:04 AM,  <st...@apache.org> wrote:
> Repository: incubator-mynewt-newt
> Updated Branches:
>   refs/heads/master d1008e6d9 -> fd1b20289
>
>
> display help for image list.  Have the connection profile be an interface, so that other programs who want to use the connection libraries, can provide their own connection profiles.
>
>
> Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
> Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/fd1b2028
> Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/fd1b2028
> Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/fd1b2028
>
> Branch: refs/heads/master
> Commit: fd1b202899ff0416abc7b373f556433272d893df
> Parents: a724701
> Author: Sterling Hughes <st...@apache.org>
> Authored: Thu Jan 28 11:04:06 2016 -0800
> Committer: Sterling Hughes <st...@apache.org>
> Committed: Thu Jan 28 11:04:16 2016 -0800
>
> ----------------------------------------------------------------------
>  newtmgr/cli/cp.go               | 45 +++++++++++++++++++++++++-----------
>  newtmgr/newtmgr.go              | 16 +++++++------
>  newtmgr/protocol/stats.go       |  9 ++++----
>  newtmgr/transport/conn.go       |  8 +++----
>  newtmgr/transport/connserial.go | 11 +++++----
>  5 files changed, 56 insertions(+), 33 deletions(-)
> ----------------------------------------------------------------------
>
>
> http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fd1b2028/newtmgr/cli/cp.go
> ----------------------------------------------------------------------
> diff --git a/newtmgr/cli/cp.go b/newtmgr/cli/cp.go
> index fe291a0..7dca0c7 100644
> --- a/newtmgr/cli/cp.go
> +++ b/newtmgr/cli/cp.go
> @@ -16,19 +16,26 @@
>  package cli
>
>  import (
> +       "log"
> +
>         "git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/util"
>         "github.com/mitchellh/go-homedir"
> -       "log"
>  )
>
>  type CpMgr struct {
>         cDb *util.CfgDb
>  }
>
> +type NewtmgrConnProfile interface {
> +       Name() string
> +       Type() string
> +       ConnString() string
> +}
> +
>  type ConnProfile struct {
> -       Name       string
> -       Type       string
> -       ConnString string
> +       MyName       string
> +       MyType       string
> +       MyConnString string
>  }
>
>  func NewCpMgr() (*CpMgr, error) {
> @@ -91,16 +98,16 @@ func (cpm *CpMgr) DeleteConnProfile(name string) error {
>  }
>
>  func (cpm *CpMgr) AddConnProfile(cp *ConnProfile) error {
> -       sect := "_conn_profile_" + cp.Name
> +       sect := "_conn_profile_" + cp.Name()
>         cDb := cpm.cDb
>
>         // First serialize the conn profile into the configuration database
> -       cDb.SetKey(sect, "name", cp.Name)
> -       cDb.SetKey(sect, "type", cp.Type)
> -       cDb.SetKey(sect, "connstring", cp.ConnString)
> +       cDb.SetKey(sect, "name", cp.Name())
> +       cDb.SetKey(sect, "type", cp.Type())
> +       cDb.SetKey(sect, "connstring", cp.ConnString())
>
>         // Then write the ConnProfile to the ConnProfileList
> -       cDb.SetKey("conn_profile_list", cp.Name, cp.Name)
> +       cDb.SetKey("conn_profile_list", cp.Name(), cp.Name())
>
>         return nil
>  }
> @@ -127,11 +134,11 @@ func (cpm *CpMgr) GetConnProfile(pName string) (*ConnProfile, error) {
>         for k, v := range cpVals {
>                 switch k {
>                 case "name":
> -                       cp.Name = v
> +                       cp.MyName = v
>                 case "type":
> -                       cp.Type = v
> +                       cp.MyType = v
>                 case "connstring":
> -                       cp.ConnString = v
> +                       cp.MyConnString = v
>                 default:
>                         return nil, util.NewNewtError(
>                                 "Invalid key " + k + " with val " + v)
> @@ -141,9 +148,21 @@ func (cpm *CpMgr) GetConnProfile(pName string) (*ConnProfile, error) {
>         return cp, nil
>  }
>
> +func (cp *ConnProfile) Name() string {
> +       return cp.MyName
> +}
> +
> +func (cp *ConnProfile) Type() string {
> +       return cp.MyType
> +}
> +
> +func (cp *ConnProfile) ConnString() string {
> +       return cp.MyConnString
> +}
> +
>  func NewConnProfile(pName string) (*ConnProfile, error) {
>         cp := &ConnProfile{}
> -       cp.Name = pName
> +       cp.MyName = pName
>
>         return cp, nil
>  }
>
> http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fd1b2028/newtmgr/newtmgr.go
> ----------------------------------------------------------------------
> diff --git a/newtmgr/newtmgr.go b/newtmgr/newtmgr.go
> index d2d9992..9aed1db 100644
> --- a/newtmgr/newtmgr.go
> +++ b/newtmgr/newtmgr.go
> @@ -75,11 +75,11 @@ func connProfileAddCmd(cmd *cobra.Command, args []string) {
>                 s := strings.Split(vdef, "=")
>                 switch s[0] {
>                 case "name":
> -                       cp.Name = s[1]
> +                       cp.MyName = s[1]
>                 case "type":
> -                       cp.Type = s[1]
> +                       cp.MyType = s[1]
>                 case "connstring":
> -                       cp.ConnString = s[1]
> +                       cp.MyConnString = s[1]
>                 default:
>                         nmUsage(cmd, util.NewNewtError("Unknown variable "+s[0]))
>                 }
> @@ -112,7 +112,7 @@ func connProfileShowCmd(cmd *cobra.Command, args []string) {
>         for _, cp := range cpList {
>                 // Print out the connection profile, if name is "" or name
>                 // matches cp.Name
> -               if name != "" && cp.Name != name {
> +               if name != "" && cp.Name() != name {
>                         continue
>                 }
>
> @@ -120,8 +120,8 @@ func connProfileShowCmd(cmd *cobra.Command, args []string) {
>                         found = true
>                         fmt.Printf("Connection profiles: \n")
>                 }
> -               fmt.Printf("  %s: type=%s, connstring='%s'\n", cp.Name, cp.Type,
> -                       cp.ConnString)
> +               fmt.Printf("  %s: type=%s, connstring='%s'\n", cp.MyName, cp.MyType,
> +                       cp.MyConnString)
>         }
>
>         if !found {
> @@ -697,7 +697,9 @@ func imageCmd() *cobra.Command {
>         imageCmd := &cobra.Command{
>                 Use:   "image",
>                 Short: "Manage images on remote instance",
> -               Run:   imageListCmd,
> +               Run: func(cmd *cobra.Command, args []string) {
> +                       cmd.Help()
> +               },
>         }
>
>         listCmd := &cobra.Command{
>
> http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fd1b2028/newtmgr/protocol/stats.go
> ----------------------------------------------------------------------
> diff --git a/newtmgr/protocol/stats.go b/newtmgr/protocol/stats.go
> index bb8d6a6..30cb710 100644
> --- a/newtmgr/protocol/stats.go
> +++ b/newtmgr/protocol/stats.go
> @@ -31,13 +31,14 @@ const (
>  )
>
>  type StatsReadReq struct {
> -       Name string `json:"n"`
> +       Name string `json:"name"`
>  }
>
>  type StatsReadRsp struct {
> -       ReturnCode int                    `json:"r"`
> -       Name       string                 `json:"n"`
> -       Fields     map[string]interface{} `json:"f"`
> +       ReturnCode int                    `json:"rc"`
> +       Name       string                 `json:"name"`
> +       Group      string                 `json:"group"`
> +       Fields     map[string]interface{} `json:"fields"`
>  }
>
>  func NewStatsReadReq() (*StatsReadReq, error) {
>
> http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fd1b2028/newtmgr/transport/conn.go
> ----------------------------------------------------------------------
> diff --git a/newtmgr/transport/conn.go b/newtmgr/transport/conn.go
> index e2d2ece..560b349 100644
> --- a/newtmgr/transport/conn.go
> +++ b/newtmgr/transport/conn.go
> @@ -23,7 +23,7 @@ import (
>  )
>
>  type Conn interface {
> -       Open(cp *cli.ConnProfile) error
> +       Open(cp cli.NewtmgrConnProfile) error
>         ReadPacket() (*Packet, error)
>         WritePacket(pkt *Packet) error
>  }
> @@ -56,18 +56,18 @@ func (pkt *Packet) GetBytes() []byte {
>         return pkt.buffer.Bytes()
>  }
>
> -func NewConn(cp *cli.ConnProfile) (Conn, error) {
> +func NewConn(cp cli.NewtmgrConnProfile) (Conn, error) {
>         // Based on ConnProfile, instantiate the right type of conn object, that
>         // implements the conn interface.
>         var c Conn
> -       switch cp.Type {
> +       switch cp.Type() {
>         case "serial":
>                 c = &ConnSerial{}
>                 if err := c.Open(cp); err != nil {
>                         return nil, err
>                 }
>         default:
> -               return nil, util.NewNewtError("Invalid conn profile " + cp.Type +
> +               return nil, util.NewNewtError("Invalid conn profile " + cp.Type() +
>                         " not implemented")
>         }
>
>
> http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/fd1b2028/newtmgr/transport/connserial.go
> ----------------------------------------------------------------------
> diff --git a/newtmgr/transport/connserial.go b/newtmgr/transport/connserial.go
> index b0e51ee..1115fa4 100644
> --- a/newtmgr/transport/connserial.go
> +++ b/newtmgr/transport/connserial.go
> @@ -28,18 +28,18 @@ import (
>  )
>
>  type ConnSerial struct {
> -       cp            *cli.ConnProfile
> +       cp            cli.NewtmgrConnProfile
>         currentPacket *Packet
>
>         scanner       *bufio.Scanner
>         serialChannel *serial.Port
>  }
>
> -func (cs *ConnSerial) Open(cp *cli.ConnProfile) error {
> +func (cs *ConnSerial) Open(cp cli.NewtmgrConnProfile) error {
>         var err error
>
>         c := &serial.Config{
> -               Name: cp.ConnString,
> +               Name: cp.ConnString(),
>                 Baud: 115200,
>         }
>
> @@ -77,8 +77,9 @@ func (cs *ConnSerial) ReadPacket() (*Packet, error) {
>
>                 data, err := base64.StdEncoding.DecodeString(base64Data)
>                 if err != nil {
> -                       return nil, util.NewNewtError(fmt.Sprintf("Couldn't decode base64 string: %b",
> -                               line))
> +                       return nil, util.NewNewtError(
> +                               fmt.Sprintf("Couldn't decode base64 string: %b",
> +                                       line))
>                 }
>
>                 if line[0] == 6 && line[1] == 9 {
>