aboutsummaryrefslogtreecommitdiffstats
path: root/lib/xdsserver/sdk.go
blob: 7a90f6b538fc6703d07da748c0a5b5d4b20fd0a1 (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
/*
 * Copyright (C) 2017 "IoT.bzh"
 * Author Sebastien Douheret <sebastien@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package xdsserver

import (
	"fmt"
	"path/filepath"

	"github.com/iotbzh/xds-server/lib/xsapiv1"
	uuid "github.com/satori/go.uuid"
)

// CrossSDK Hold SDK config
type CrossSDK struct {
	sdk xsapiv1.SDK
}

// NewCrossSDK creates a new instance of Syncthing
func NewCrossSDK(path string) (*CrossSDK, error) {
	// Assume that we have .../<profile>/<version>/<arch>
	s := CrossSDK{
		sdk: xsapiv1.SDK{Path: path},
	}

	s.sdk.Arch = filepath.Base(path)

	d := filepath.Dir(path)
	s.sdk.Version = filepath.Base(d)

	d = filepath.Dir(d)
	s.sdk.Profile = filepath.Base(d)

	// Use V3 to ensure that we get same uuid on restart
	s.sdk.ID = uuid.NewV3(uuid.FromStringOrNil("sdks"), s.sdk.Profile+"_"+s.sdk.Arch+"_"+s.sdk.Version).String()
	s.sdk.Name = s.sdk.Arch + "  (" + s.sdk.Version + ")"

	envFile := filepath.Join(path, "environment-setup*")
	ef, err := filepath.Glob(envFile)
	if err != nil {
		return nil, fmt.Errorf("Cannot retrieve environment setup file: %v", err)
	}
	if len(ef) != 1 {
		return nil, fmt.Errorf("No environment setup file found match %s", envFile)
	}
	s.sdk.EnvFile = ef[0]

	return &s, nil
}

// Get Return SDK definition
func (s *CrossSDK) Get() *xsapiv1.SDK {
	return &s.sdk
}

// GetEnvCmd returns the command used to initialized the environment
func (s *CrossSDK) GetEnvCmd() []string {
	return []string{"source", s.sdk.EnvFile}
}