-
Notifications
You must be signed in to change notification settings - Fork 13
/
platforms.go
112 lines (92 loc) · 3.04 KB
/
platforms.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package platforms
import (
"sort"
"strconv"
"strings"
"time"
"github.com/ActiveState/cli/pkg/sysinfo"
"github.com/go-openapi/strfmt"
"github.com/ActiveState/cli/internal/captain"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/pkg/platform/model"
)
type PlatformVersion struct {
captain.NameVersionValue
}
func (pv *PlatformVersion) Set(arg string) error {
err := pv.NameVersionValue.Set(arg)
if err != nil {
return locale.WrapInputError(err, "err_platform_format", "The platform and version provided is not formatting correctly, must be in the form of <platform>@<version>")
}
return nil
}
// Platform represents the output data of a platform.
type Platform struct {
Name string `json:"name"`
Version string `json:"version"`
BitWidth string `json:"bitWidth"`
}
func makePlatformsFromModelPlatforms(platforms []*model.Platform) []*Platform {
var ps []*Platform
for _, platform := range platforms {
if platform.EndOfSupportDate != nil && time.Since(time.Time(*platform.EndOfSupportDate)) > 0 {
continue // ignore EOL platforms; the Platform will fail to resolve dependencies on them
}
var p Platform
if platform.Kernel != nil && platform.Kernel.Name != nil {
p.Name = *platform.Kernel.Name
}
if platform.KernelVersion != nil && platform.KernelVersion.Version != nil {
p.Version = *platform.KernelVersion.Version
}
if platform.CPUArchitecture != nil && platform.CPUArchitecture.BitWidth != nil {
p.BitWidth = *platform.CPUArchitecture.BitWidth
}
ps = append(ps, &p)
}
sort.Slice(ps, func(i, j int) bool {
tmpI := strings.ToLower(ps[i].Name) + ps[i].BitWidth + ps[i].Version
tmpJ := strings.ToLower(ps[j].Name) + ps[j].BitWidth + ps[j].Version
return tmpI < tmpJ
})
return ps
}
// Params represents the minimal defining details of a platform.
type Params struct {
Platform PlatformVersion
BitWidth int
name string
version string
}
func prepareParams(ps Params) (Params, error) {
ps.name = ps.Platform.Name()
if ps.name == "" {
ps.name = sysinfo.OS().String()
}
ps.version = ps.Platform.Version()
if ps.version == "" {
return prepareLatestVersion(ps)
}
if ps.BitWidth == 0 {
ps.BitWidth = 32 << (^uint(0) >> 63) // gets host word size
}
return ps, nil
}
func prepareLatestVersion(params Params) (Params, error) {
platformUUID, err := model.PlatformNameToPlatformID(params.Platform.Name())
if err != nil {
return params, locale.WrapExternalError(err, "err_resolve_platform_id", "Could not resolve platform ID from name: {{.V0}}", params.Platform.Name())
}
platform, err := model.FetchPlatformByUID(strfmt.UUID(platformUUID))
if err != nil {
return params, locale.WrapError(err, "err_fetch_platform", "Could not get platform details")
}
params.name = *platform.Kernel.Name
params.version = *platform.KernelVersion.Version
bitWidth, err := strconv.Atoi(*platform.CPUArchitecture.BitWidth)
if err != nil {
return params, locale.WrapError(err, "err_platform_bitwidth", "Unable to determine platform bit width")
}
params.BitWidth = bitWidth
return params, nil
}