X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=src%2Fxds%2Fxds-server.git;a=blobdiff_plain;f=lib%2Fxdsserver%2Fsdks.go;fp=lib%2Fxdsserver%2Fsdks.go;h=1a40ab58c003e906941f67680d5d195faeb7f5b6;hp=0000000000000000000000000000000000000000;hb=2f7828d01f4c4ca2909f95f098627cd5475ed225;hpb=5caebfb4b7c3b73988f067082b219ce5b7f409ba diff --git a/lib/xdsserver/sdks.go b/lib/xdsserver/sdks.go new file mode 100644 index 0000000..1a40ab5 --- /dev/null +++ b/lib/xdsserver/sdks.go @@ -0,0 +1,127 @@ +package xdsserver + +import ( + "fmt" + "path" + "path/filepath" + "strings" + "sync" + + common "github.com/iotbzh/xds-common/golib" + "github.com/iotbzh/xds-server/lib/xsapiv1" +) + +// SDKs List of installed SDK +type SDKs struct { + *Context + Sdks map[string]*CrossSDK + + mutex sync.Mutex +} + +// NewSDKs creates a new instance of SDKs +func NewSDKs(ctx *Context) (*SDKs, error) { + s := SDKs{ + Context: ctx, + Sdks: make(map[string]*CrossSDK), + } + + // Retrieve installed sdks + sdkRD := ctx.Config.FileConf.SdkRootDir + + if common.Exists(sdkRD) { + + // Assume that SDK install tree is /// + dirs, err := filepath.Glob(path.Join(sdkRD, "*", "*", "*")) + if err != nil { + ctx.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 { + if !common.IsDir(d) { + continue + } + cSdk, err := NewCrossSDK(d) + if err != nil { + ctx.Log.Debugf("Error while processing SDK dir=%s, err=%s", d, err.Error()) + continue + } + s.Sdks[cSdk.sdk.ID] = cSdk + } + } + + ctx.Log.Debugf("SDKs: %d cross sdks found", len(s.Sdks)) + + return &s, nil +} + +// 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 { + if strings.HasPrefix(iid, id) { + match = append(match, iid) + } + } + + if len(match) == 1 { + return match[0], nil + } else if len(match) == 0 { + return id, fmt.Errorf("Unknown sdk id") + } + return id, fmt.Errorf("Multiple sdk IDs found with provided prefix: " + id) +} + +// Get returns an SDK from id +func (s *SDKs) Get(id string) *xsapiv1.SDK { + s.mutex.Lock() + defer s.mutex.Unlock() + + sc, exist := s.Sdks[id] + if !exist { + return nil + } + return (*sc).Get() +} + +// GetAll returns all existing SDKs +func (s *SDKs) GetAll() []xsapiv1.SDK { + s.mutex.Lock() + defer s.mutex.Unlock() + res := []xsapiv1.SDK{} + for _, v := range s.Sdks { + res = append(res, *(*v).Get()) + } + 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 []string{} + } + + s.mutex.Lock() + defer s.mutex.Unlock() + + if iid, err := s.ResolveID(id); err == nil { + if sdk, exist := s.Sdks[iid]; exist { + return sdk.GetEnvCmd() + } + } + + if sdk, exist := s.Sdks[defaultID]; defaultID != "" && exist { + return sdk.GetEnvCmd() + } + + // Return default env that may be empty + return []string{} +}