2 * Copyright (C) 2017 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
27 common "github.com/iotbzh/xds-common/golib"
28 "github.com/iotbzh/xds-server/lib/xsapiv1"
31 // SDKs List of installed SDK
34 Sdks map[string]*CrossSDK
39 // NewSDKs creates a new instance of SDKs
40 func NewSDKs(ctx *Context) (*SDKs, error) {
43 Sdks: make(map[string]*CrossSDK),
46 // Retrieve installed sdks
47 sdkRD := ctx.Config.FileConf.SdkRootDir
49 if common.Exists(sdkRD) {
51 // Assume that SDK install tree is <rootdir>/<profile>/<version>/<arch>
52 dirs, err := filepath.Glob(path.Join(sdkRD, "*", "*", "*"))
54 ctx.Log.Debugf("Error while retrieving SDKs: dir=%s, error=%s", sdkRD, err.Error())
58 defer s.mutex.Unlock()
60 for _, d := range dirs {
64 cSdk, err := NewCrossSDK(d)
66 ctx.Log.Debugf("Error while processing SDK dir=%s, err=%s", d, err.Error())
69 s.Sdks[cSdk.sdk.ID] = cSdk
73 ctx.Log.Debugf("SDKs: %d cross sdks found", len(s.Sdks))
78 // ResolveID Complete an SDK ID (helper for user that can use partial ID value)
79 func (s *SDKs) ResolveID(id string) (string, error) {
85 for iid := range s.Sdks {
86 if strings.HasPrefix(iid, id) {
87 match = append(match, iid)
93 } else if len(match) == 0 {
94 return id, fmt.Errorf("Unknown sdk id")
96 return id, fmt.Errorf("Multiple sdk IDs found with provided prefix: " + id)
99 // Get returns an SDK from id
100 func (s *SDKs) Get(id string) *xsapiv1.SDK {
102 defer s.mutex.Unlock()
104 sc, exist := s.Sdks[id]
111 // GetAll returns all existing SDKs
112 func (s *SDKs) GetAll() []xsapiv1.SDK {
114 defer s.mutex.Unlock()
115 res := []xsapiv1.SDK{}
116 for _, v := range s.Sdks {
117 res = append(res, *(*v).Get())
122 // GetEnvCmd returns the command used to initialized the environment for an SDK
123 func (s *SDKs) GetEnvCmd(id string, defaultID string) []string {
124 if id == "" && defaultID == "" {
130 defer s.mutex.Unlock()
132 if iid, err := s.ResolveID(id); err == nil {
133 if sdk, exist := s.Sdks[iid]; exist {
134 return sdk.GetEnvCmd()
138 if sdk, exist := s.Sdks[defaultID]; defaultID != "" && exist {
139 return sdk.GetEnvCmd()
142 // Return default env that may be empty