Refit source files to have a public xs-apiv1 lib package.
[src/xds/xds-server.git] / lib / xdsserver / sdk.go
1 package xdsserver
2
3 import (
4         "fmt"
5         "path/filepath"
6
7         "github.com/iotbzh/xds-server/lib/xsapiv1"
8         uuid "github.com/satori/go.uuid"
9 )
10
11 // CrossSDK Hold SDK config
12 type CrossSDK struct {
13         sdk xsapiv1.SDK
14 }
15
16 // NewCrossSDK creates a new instance of Syncthing
17 func NewCrossSDK(path string) (*CrossSDK, error) {
18         // Assume that we have .../<profile>/<version>/<arch>
19         s := CrossSDK{
20                 sdk: xsapiv1.SDK{Path: path},
21         }
22
23         s.sdk.Arch = filepath.Base(path)
24
25         d := filepath.Dir(path)
26         s.sdk.Version = filepath.Base(d)
27
28         d = filepath.Dir(d)
29         s.sdk.Profile = filepath.Base(d)
30
31         // Use V3 to ensure that we get same uuid on restart
32         s.sdk.ID = uuid.NewV3(uuid.FromStringOrNil("sdks"), s.sdk.Profile+"_"+s.sdk.Arch+"_"+s.sdk.Version).String()
33         s.sdk.Name = s.sdk.Arch + "  (" + s.sdk.Version + ")"
34
35         envFile := filepath.Join(path, "environment-setup*")
36         ef, err := filepath.Glob(envFile)
37         if err != nil {
38                 return nil, fmt.Errorf("Cannot retrieve environment setup file: %v", err)
39         }
40         if len(ef) != 1 {
41                 return nil, fmt.Errorf("No environment setup file found match %s", envFile)
42         }
43         s.sdk.EnvFile = ef[0]
44
45         return &s, nil
46 }
47
48 // Get Return SDK definition
49 func (s *CrossSDK) Get() *xsapiv1.SDK {
50         return &s.sdk
51 }
52
53 // GetEnvCmd returns the command used to initialized the environment
54 func (s *CrossSDK) GetEnvCmd() []string {
55         return []string{"source", s.sdk.EnvFile}
56 }