10 common "github.com/iotbzh/xds-common/golib"
11 "github.com/iotbzh/xds-server/lib/xsapiv1"
14 // SDKs List of installed SDK
17 Sdks map[string]*CrossSDK
22 // NewSDKs creates a new instance of SDKs
23 func NewSDKs(ctx *Context) (*SDKs, error) {
26 Sdks: make(map[string]*CrossSDK),
29 // Retrieve installed sdks
30 sdkRD := ctx.Config.FileConf.SdkRootDir
32 if common.Exists(sdkRD) {
34 // Assume that SDK install tree is <rootdir>/<profile>/<version>/<arch>
35 dirs, err := filepath.Glob(path.Join(sdkRD, "*", "*", "*"))
37 ctx.Log.Debugf("Error while retrieving SDKs: dir=%s, error=%s", sdkRD, err.Error())
41 defer s.mutex.Unlock()
43 for _, d := range dirs {
47 cSdk, err := NewCrossSDK(d)
49 ctx.Log.Debugf("Error while processing SDK dir=%s, err=%s", d, err.Error())
52 s.Sdks[cSdk.sdk.ID] = cSdk
56 ctx.Log.Debugf("SDKs: %d cross sdks found", len(s.Sdks))
61 // ResolveID Complete an SDK ID (helper for user that can use partial ID value)
62 func (s *SDKs) ResolveID(id string) (string, error) {
68 for iid := range s.Sdks {
69 if strings.HasPrefix(iid, id) {
70 match = append(match, iid)
76 } else if len(match) == 0 {
77 return id, fmt.Errorf("Unknown sdk id")
79 return id, fmt.Errorf("Multiple sdk IDs found with provided prefix: " + id)
82 // Get returns an SDK from id
83 func (s *SDKs) Get(id string) *xsapiv1.SDK {
85 defer s.mutex.Unlock()
87 sc, exist := s.Sdks[id]
94 // GetAll returns all existing SDKs
95 func (s *SDKs) GetAll() []xsapiv1.SDK {
97 defer s.mutex.Unlock()
98 res := []xsapiv1.SDK{}
99 for _, v := range s.Sdks {
100 res = append(res, *(*v).Get())
105 // GetEnvCmd returns the command used to initialized the environment for an SDK
106 func (s *SDKs) GetEnvCmd(id string, defaultID string) []string {
107 if id == "" && defaultID == "" {
113 defer s.mutex.Unlock()
115 if iid, err := s.ResolveID(id); err == nil {
116 if sdk, exist := s.Sdks[iid]; exist {
117 return sdk.GetEnvCmd()
121 if sdk, exist := s.Sdks[defaultID]; defaultID != "" && exist {
122 return sdk.GetEnvCmd()
125 // Return default env that may be empty