From 2c9ae6a5a27ae2f2e23495c613e7a53aed8e786c Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Thu, 18 May 2017 11:01:13 +0200 Subject: Add Cross SDKs support (part 2) --- lib/crosssdk/sdk.go | 22 ++++++++++++-------- lib/crosssdk/sdks.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 10 deletions(-) (limited to 'lib/crosssdk') diff --git a/lib/crosssdk/sdk.go b/lib/crosssdk/sdk.go index 2f22f22..9aeec90 100644 --- a/lib/crosssdk/sdk.go +++ b/lib/crosssdk/sdk.go @@ -2,17 +2,20 @@ package crosssdk import ( "fmt" - "path" "path/filepath" ) // SDK Define a cross tool chain used to build application type SDK struct { - Profile string - Version string - Arch string - Path string - EnvFile string + ID string `json:"id" binding:"required"` + Name string `json:"name"` + Profile string `json:"profile"` + Version string `json:"version"` + Arch string `json:"arch"` + Path string `json:"path"` + + // Not exported fields + EnvFile string `json:"-"` } // NewCrossSDK creates a new instance of Syncthing @@ -28,6 +31,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 + ")" + envFile := filepath.Join(path, "environment-setup*") ef, err := filepath.Glob(envFile) if err != nil { @@ -41,7 +47,7 @@ func NewCrossSDK(path string) (*SDK, error) { return &s, nil } -// GetEnvCmd returns the command to initialized the environment to use a cross SDK +// GetEnvCmd returns the command used to initialized the environment func (s *SDK) GetEnvCmd() string { - return ". " + path.Join(s.Path, s.EnvFile) + return ". " + s.EnvFile } diff --git a/lib/crosssdk/sdks.go b/lib/crosssdk/sdks.go index 435aae6..87bb85d 100644 --- a/lib/crosssdk/sdks.go +++ b/lib/crosssdk/sdks.go @@ -3,6 +3,7 @@ package crosssdk import ( "path" "path/filepath" + "sync" "github.com/Sirupsen/logrus" "github.com/iotbzh/xds-server/lib/common" @@ -10,7 +11,11 @@ import ( ) // SDKs List of installed SDK -type SDKs []*SDK +type SDKs struct { + Sdks []SDK + + mutex sync.Mutex +} // Init creates a new instance of Syncthing func Init(cfg *xdsconfig.Config, log *logrus.Logger) (*SDKs, error) { @@ -27,13 +32,61 @@ func Init(cfg *xdsconfig.Config, log *logrus.Logger) (*SDKs, error) { log.Debugf("Error while retrieving SDKs: dir=%s, error=%s", sdkRD, err.Error()) return &s, err } + s.mutex.Lock() + defer s.mutex.Unlock() + for _, d := range dirs { sdk, err := NewCrossSDK(d) if err != nil { log.Debugf("Error while processing SDK dir=%s, err=%s", d, err.Error()) } - s = append(s, sdk) + s.Sdks = append(s.Sdks, *sdk) } } + + log.Debugf("SDKs: %d cross sdks found", len(s.Sdks)) + 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 +} + +// Get returns an SDK from id +func (s *SDKs) Get(id int) SDK { + s.mutex.Lock() + defer s.mutex.Unlock() + + if id < 0 || id > len(s.Sdks) { + return SDK{} + } + res := s.Sdks[id] + return res +} + +// GetEnvCmd returns the command used to initialized the environment for an SDK +func (s *SDKs) GetEnvCmd(id string, defaultID string) string { + if id == "" && defaultID == "" { + // no env cmd + return "" + } + + s.mutex.Lock() + defer s.mutex.Unlock() + defaultEnv := "" + for _, sdk := range s.Sdks { + if sdk.ID == id { + return sdk.GetEnvCmd() + } + if sdk.ID == defaultID { + defaultEnv = sdk.GetEnvCmd() + } + } + // Return default env that may be empty + return defaultEnv +} -- cgit