Fixed target and terminal id arg parsing
authorSebastien Douheret <sebastien.douheret@iot.bzh>
Mon, 5 Mar 2018 16:48:39 +0000 (17:48 +0100)
committerSebastien Douheret <sebastien.douheret@iot.bzh>
Mon, 5 Mar 2018 16:48:39 +0000 (17:48 +0100)
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
cmd-target.go
utils.go

index 688aef6..db97e91 100644 (file)
@@ -471,50 +471,50 @@ func TerminalSendSignal(tgt *xaapiv1.TargetConfig, term *xaapiv1.TerminalConfig,
 // GetTargetAndTerminalIDs Retrieve Target and Terminal definition from IDs
 func GetTargetAndTerminalIDs(ctx *cli.Context, useFirstFree bool) (*xaapiv1.TargetConfig, *xaapiv1.TerminalConfig, error) {
 
-       idArg := ctx.String("id")
-       tidArg := GetIDName(ctx, "termId")
-       if tidArg == "" {
-               tidArg = GetIDName(ctx, "tid")
-       }
-       if idArg == "" && tidArg == "" {
-               return nil, nil, fmt.Errorf("id or termId argument must be set")
-       }
-
        tgts := []xaapiv1.TargetConfig{}
        if err := TargetsListGet(&tgts); err != nil {
                return nil, nil, err
        }
 
-       matching := 0
-       ti := 0
-       tj := 0
-       for ii, tt := range tgts {
-               for jj, ttm := range tt.Terms {
-                       if idArg == "" && compareID(ttm.ID, tidArg) {
-                               ti = ii
-                               tj = jj
-                               matching++
-                       }
-                       if idArg != "" && compareID(tt.ID, idArg) && compareID(ttm.ID, tidArg) {
-                               ti = ii
-                               tj = jj
-                               matching++
+       idArg := ctx.String("id")
+       tidArg := ctx.String("termId")
+       if tidArg == "" {
+               tidArg = ctx.String("tid")
+       }
+       if idArg != "" || tidArg != "" {
+               matching := 0
+               ti := 0
+               tj := 0
+               for ii, tt := range tgts {
+                       for jj, ttm := range tt.Terms {
+                               if idArg == "" && compareID(ttm.ID, tidArg) {
+                                       ti = ii
+                                       tj = jj
+                                       matching++
+                               }
+                               if idArg != "" && compareID(tt.ID, idArg) && compareID(ttm.ID, tidArg) {
+                                       ti = ii
+                                       tj = jj
+                                       matching++
+                               }
                        }
                }
-       }
-       if matching > 1 {
-               return nil, nil, fmt.Errorf("Multiple IDs found, please set -id and -tid with full ID notation")
-       } else if matching == 1 {
-               return &tgts[ti], &tgts[ti].Terms[tj], nil
+               if matching > 1 {
+                       return nil, nil, fmt.Errorf("Multiple IDs found, please set -id and -tid with full ID notation")
+               } else if matching == 1 {
+                       return &tgts[ti], &tgts[ti].Terms[tj], nil
+               }
        }
 
        // Allow to create a new terminal when only target id is set
        idArg = GetIDName(ctx, "id")
-       if idArg != "" {
-               for _, tt := range tgts {
-                       if compareID(tt.ID, idArg) {
-                               return &tt, nil, nil
-                       }
+       if idArg == "" {
+               return nil, nil, fmt.Errorf("id or termId argument must be set")
+       }
+
+       for _, tt := range tgts {
+               if compareID(tt.ID, idArg) {
+                       return &tt, nil, nil
                }
        }
 
index 0ca6471..393ee2a 100644 (file)
--- a/utils.go
+++ b/utils.go
@@ -150,5 +150,5 @@ func Confirm(question string) bool {
 
 // compareID Compare an ID to a reference ID
 func compareID(refID, ID string) bool {
-       return refID != "" && ID != "" && strings.Contains(refID, ID)
+       return refID != "" && ID != "" && strings.HasPrefix(refID, ID)
 }