summaryrefslogtreecommitdiffstats
path: root/lib/crosssdk/sdks.go
blob: 0da0d1b859274027a34dbc8af924571ff51d10a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package crosssdk

import (
	"path"
	"path/filepath"
	"sync"

	"github.com/Sirupsen/logrus"
	common "github.com/iotbzh/xds-common/golib"
	"github.com/iotbzh/xds-server/lib/xdsconfig"
)

// SDKs List of installed 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) {
	s := SDKs{}

	// Retrieve installed sdks
	sdkRD := cfg.FileConf.SdkRootDir

	if common.Exists(sdkRD) {

		// Assume that SDK install tree is <rootdir>/<profile>/<version>/<arch>
		dirs, err := filepath.Glob(path.Join(sdkRD, "*", "*", "*"))
		if err != nil {
			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
			}
			sdk, err := NewCrossSDK(d)
			if err != nil {
				log.Debugf("Error while processing SDK dir=%s, err=%s", d, err.Error())
				continue
			}
			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 []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()
		}
	}
	// Return default env that may be empty
	return defaultEnv
}