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 2017/10/19 22:31:20 UTC

[GitHub] ccollins476ad closed pull request #38: newtmgr - CLI options to specify connprofile flds

ccollins476ad closed pull request #38: newtmgr - CLI options to specify connprofile flds
URL: https://github.com/apache/mynewt-newtmgr/pull/38
 
 
   

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/commands.go b/newtmgr/cli/commands.go
index 0089573..70af24e 100644
--- a/newtmgr/cli/commands.go
+++ b/newtmgr/cli/commands.go
@@ -71,6 +71,16 @@ func Commands() *cobra.Command {
 	nmCmd.PersistentFlags().BoolVar(&nmutil.BleWriteRsp, "write-rsp", false,
 		"Send BLE acked write requests instead of unacked write commands")
 
+	nmCmd.PersistentFlags().StringVar(&nmutil.ConnType, "conntype", "",
+		"Connection type to use instead of using the profile's type")
+
+	nmCmd.PersistentFlags().StringVar(&nmutil.ConnString, "connstring", "",
+		"Connection key-value pairs to use instead of using the profile's "+
+			"connstring")
+
+	nmCmd.PersistentFlags().StringVar(&nmutil.ConnExtra, "connextra", "",
+		"Additional key-value pair to append to the connstring")
+
 	nmCmd.AddCommand(crashCmd())
 	nmCmd.AddCommand(dateTimeCmd())
 	nmCmd.AddCommand(fsCmd())
diff --git a/newtmgr/cli/common.go b/newtmgr/cli/common.go
index 9f0cb82..07f5700 100644
--- a/newtmgr/cli/common.go
+++ b/newtmgr/cli/common.go
@@ -22,6 +22,8 @@ package cli
 import (
 	"fmt"
 
+	log "github.com/Sirupsen/logrus"
+
 	"mynewt.apache.org/newt/util"
 	"mynewt.apache.org/newtmgr/newtmgr/bll"
 	"mynewt.apache.org/newtmgr/newtmgr/config"
@@ -35,13 +37,67 @@ import (
 
 var globalSesn sesn.Sesn
 var globalXport xport.Xport
+var globalP *config.ConnProfile
 
-// These keep track of whether the global interfaces have been assigned.  These
-// are necessary to accommodate golang's nil-interface semantics.
+// This keeps track of whether the global interface has been assigned.  This
+// is necessary to accommodate golang's nil-interface semantics.
 var globalXportSet bool
 
+func initConnProfile() error {
+	var p *config.ConnProfile
+
+	if nmutil.ConnProfile == "" {
+		p = config.NewConnProfile()
+		p.Name = "unnamed"
+	} else {
+		var err error
+
+		cpm := config.GlobalConnProfileMgr()
+		p, err = cpm.GetConnProfile(nmutil.ConnProfile)
+		if err != nil {
+			return err
+		}
+	}
+
+	if nmutil.ConnType != "" {
+		t, err := config.ConnTypeFromString(nmutil.ConnType)
+		if err != nil {
+			return util.FmtNewtError("invalid conntype: \"%s\"",
+				nmutil.ConnType)
+		}
+
+		p.Type = t
+	}
+
+	if nmutil.ConnString != "" {
+		p.ConnString = nmutil.ConnString
+	}
+
+	if nmutil.ConnExtra != "" {
+		if p.ConnString != "" {
+			p.ConnString += ","
+		}
+		p.ConnString += nmutil.ConnExtra
+	}
+
+	if p.Type == config.CONN_TYPE_NONE {
+		return util.FmtNewtError("No connection type specified")
+	}
+
+	log.Debugf("Using connection profile: %v", p)
+	globalP = p
+
+	return nil
+}
+
 func getConnProfile() (*config.ConnProfile, error) {
-	return config.GlobalConnProfileMgr().GetConnProfile(nmutil.ConnProfile)
+	if globalP == nil {
+		if err := initConnProfile(); err != nil {
+			return nil, err
+		}
+	}
+
+	return globalP, nil
 }
 
 func GetXport() (xport.Xport, error) {
diff --git a/newtmgr/config/connprofile.go b/newtmgr/config/connprofile.go
index 4000774..c6a34a5 100644
--- a/newtmgr/config/connprofile.go
+++ b/newtmgr/config/connprofile.go
@@ -24,6 +24,7 @@ import (
 	"github.com/mitchellh/go-homedir"
 
 	"encoding/json"
+	"fmt"
 	"io/ioutil"
 	"os"
 	"sort"
@@ -43,6 +44,11 @@ type ConnProfile struct {
 	ConnString string   `json:"MyConnString"`
 }
 
+func (p *ConnProfile) String() string {
+	return fmt.Sprintf("name=%s type=%s connstring=%s",
+		p.Name, ConnTypeToString(p.Type), p.ConnString)
+}
+
 const (
 	CONN_TYPE_NONE ConnType = iota
 	CONN_TYPE_SERIAL_PLAIN
@@ -234,10 +240,6 @@ func (cpm *ConnProfileMgr) AddConnProfile(cp *ConnProfile) error {
 func (cpm *ConnProfileMgr) GetConnProfile(pName string) (*ConnProfile, error) {
 	// Each section is a connection profile, key values are the contents
 	// of that section.
-	if pName == "" {
-		return nil, util.NewNewtError("Need to specify connection profile")
-	}
-
 	p := cpm.profiles[pName]
 	if p == nil {
 		return nil, util.FmtNewtError("connection profile \"%s\" doesn't "+
diff --git a/newtmgr/nmutil/nmutil.go b/newtmgr/nmutil/nmutil.go
index 79e7ef6..9d7fe42 100644
--- a/newtmgr/nmutil/nmutil.go
+++ b/newtmgr/nmutil/nmutil.go
@@ -32,6 +32,9 @@ var Tries int
 var ConnProfile string
 var DeviceName string
 var BleWriteRsp bool
+var ConnType string
+var ConnString string
+var ConnExtra string
 
 func TxOptions() sesn.TxOptions {
 	return sesn.TxOptions{


 

----------------------------------------------------------------
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