Used uuid of SDK ID and support short ID name if not ambiguous.
authorSebastien Douheret <sebastien.douheret@iot.bzh>
Mon, 6 Nov 2017 13:56:39 +0000 (14:56 +0100)
committerSebastien Douheret <sebastien.douheret@iot.bzh>
Mon, 6 Nov 2017 15:00:04 +0000 (16:00 +0100)
lib/apiv1/sdks.go
lib/crosssdk/sdk.go
lib/crosssdk/sdks.go

index 52af506..f67a0ef 100644 (file)
@@ -2,7 +2,6 @@ package apiv1
 
 import (
        "net/http"
-       "strconv"
 
        "github.com/gin-gonic/gin"
        common "github.com/iotbzh/xds-common/golib"
@@ -15,12 +14,11 @@ func (s *APIService) getSdks(c *gin.Context) {
 
 // getSdk returns a specific Sdk configuration
 func (s *APIService) getSdk(c *gin.Context) {
-       id, err := strconv.Atoi(c.Param("id"))
+       id, err := s.sdks.ResolveID(c.Param("id"))
        if err != nil {
-               common.APIError(c, "Invalid id")
+               common.APIError(c, err.Error())
                return
        }
-
        sdk := s.sdks.Get(id)
        if sdk.Profile == "" {
                common.APIError(c, "Invalid id")
index 5a5770d..5be8954 100644 (file)
@@ -3,6 +3,8 @@ package crosssdk
 import (
        "fmt"
        "path/filepath"
+
+       uuid "github.com/satori/go.uuid"
 )
 
 // SDK Define a cross tool chain used to build application
@@ -31,8 +33,9 @@ func NewCrossSDK(path string) (*SDK, error) {
        d = filepath.Dir(d)
        s.Profile = filepath.Base(d)
 
-       s.ID = s.Profile + "_" + s.Arch + "_" + s.Version
-       s.Name = s.Arch + "   (" + s.Version + ")"
+       // Use V3 to ensure that we get same uuid on restart
+       s.ID = uuid.NewV3(uuid.FromStringOrNil("sdks"), s.Profile+"_"+s.Arch+"_"+s.Version).String()
+       s.Name = s.Arch + "  (" + s.Version + ")"
 
        envFile := filepath.Join(path, "environment-setup*")
        ef, err := filepath.Glob(envFile)
index 0da0d1b..e3d6607 100644 (file)
@@ -1,8 +1,10 @@
 package crosssdk
 
 import (
+       "fmt"
        "path"
        "path/filepath"
+       "strings"
        "sync"
 
        "github.com/Sirupsen/logrus"
@@ -12,14 +14,16 @@ import (
 
 // SDKs List of installed SDK
 type SDKs struct {
-       Sdks []SDK
+       Sdks map[string]*SDK
 
        mutex sync.Mutex
 }
 
 // Init creates a new instance of Syncthing
 func Init(cfg *xdsconfig.Config, log *logrus.Logger) (*SDKs, error) {
-       s := SDKs{}
+       s := SDKs{
+               Sdks: make(map[string]*SDK),
+       }
 
        // Retrieve installed sdks
        sdkRD := cfg.FileConf.SdkRootDir
@@ -44,7 +48,7 @@ func Init(cfg *xdsconfig.Config, log *logrus.Logger) (*SDKs, error) {
                                log.Debugf("Error while processing SDK dir=%s, err=%s", d, err.Error())
                                continue
                        }
-                       s.Sdks = append(s.Sdks, *sdk)
+                       s.Sdks[sdk.ID] = sdk
                }
        }
 
@@ -53,23 +57,50 @@ func Init(cfg *xdsconfig.Config, log *logrus.Logger) (*SDKs, error) {
        return &s, nil
 }
 
-// GetAll returns all existing SDKs
-func (s *SDKs) GetAll() []SDK {
-       s.mutex.Lock()
-       defer s.mutex.Unlock()
-       res := s.Sdks
-       return res
+// ResolveID Complete an SDK ID (helper for user that can use partial ID value)
+func (s *SDKs) ResolveID(id string) (string, error) {
+       if id == "" {
+               return "", nil
+       }
+
+       match := []string{}
+       for iid := range s.Sdks {
+               fmt.Printf("SEB prefix iid=%v id=%v\n", iid, id)
+               if strings.HasPrefix(iid, id) {
+                       match = append(match, iid)
+                       fmt.Printf("  SEB match (%d): %v\n", len(match), match)
+               }
+       }
+       fmt.Printf("SEB match (%d): %v\n", len(match), match)
+
+       if len(match) == 1 {
+               return match[0], nil
+       } else if len(match) == 0 {
+               return id, fmt.Errorf("Unknown id")
+       }
+       return id, fmt.Errorf("Multiple IDs found with provided prefix: " + id)
 }
 
 // Get returns an SDK from id
-func (s *SDKs) Get(id int) SDK {
+func (s *SDKs) Get(id string) *SDK {
        s.mutex.Lock()
        defer s.mutex.Unlock()
 
-       if id < 0 || id > len(s.Sdks) {
-               return SDK{}
+       sc, exist := s.Sdks[id]
+       if !exist {
+               return nil
+       }
+       return sc
+}
+
+// GetAll returns all existing SDKs
+func (s *SDKs) GetAll() []SDK {
+       s.mutex.Lock()
+       defer s.mutex.Unlock()
+       res := []SDK{}
+       for _, v := range s.Sdks {
+               res = append(res, *v)
        }
-       res := s.Sdks[id]
        return res
 }
 
@@ -82,15 +113,15 @@ func (s *SDKs) GetEnvCmd(id string, defaultID string) []string {
 
        s.mutex.Lock()
        defer s.mutex.Unlock()
-       defaultEnv := []string{}
-       for _, sdk := range s.Sdks {
-               if sdk.ID == id {
-                       return sdk.GetEnvCmd()
-               }
-               if sdk.ID == defaultID {
-                       defaultEnv = sdk.GetEnvCmd()
-               }
+
+       if sdk, exist := s.Sdks[id]; exist {
+               return sdk.GetEnvCmd()
        }
+
+       if sdk, exist := s.Sdks[defaultID]; defaultID != "" && exist {
+               return sdk.GetEnvCmd()
+       }
+
        // Return default env that may be empty
-       return defaultEnv
+       return []string{}
 }