-
Notifications
You must be signed in to change notification settings - Fork 18
/
parallels.go
117 lines (90 loc) · 3.01 KB
/
parallels.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
113
114
115
116
117
package isolation
import (
"fmt"
"github.com/cirruslabs/cirrus-ci-agent/api"
"github.com/cirruslabs/cirrus-cli/pkg/parser/nameable"
"github.com/cirruslabs/cirrus-cli/pkg/parser/node"
"github.com/cirruslabs/cirrus-cli/pkg/parser/parseable"
"github.com/cirruslabs/cirrus-cli/pkg/parser/parsererror"
"github.com/cirruslabs/cirrus-cli/pkg/parser/schema"
jsschema "github.com/lestrrat-go/jsschema"
"sort"
"strings"
)
type Parallels struct {
proto *api.Isolation_Parallels_
parseable.DefaultParser
}
func NewParallels(mergedEnv map[string]string) *Parallels {
parallels := &Parallels{
proto: &api.Isolation_Parallels_{
Parallels: &api.Isolation_Parallels{},
},
}
imageSchema := schema.String("Image name.")
parallels.OptionalField(nameable.NewSimpleNameable("image"), imageSchema, func(node *node.Node) error {
image, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
parallels.proto.Parallels.Image = image
return nil
})
userSchema := schema.String("SSH username")
parallels.OptionalField(nameable.NewSimpleNameable("user"), userSchema, func(node *node.Node) error {
user, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
parallels.proto.Parallels.User = user
return nil
})
passwordSchema := schema.String("SSH password")
parallels.OptionalField(nameable.NewSimpleNameable("password"), passwordSchema, func(node *node.Node) error {
password, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
parallels.proto.Parallels.Password = password
return nil
})
// Prepare a list of platforms
var platformsLowercased []string
for _, platformName := range api.Platform_name {
platformsLowercased = append(platformsLowercased, strings.ToLower(platformName))
}
sort.Strings(platformsLowercased)
var platformsInterfaced []interface{}
for _, lowercasePlatform := range platformsLowercased {
platformsInterfaced = append(platformsInterfaced, lowercasePlatform)
}
platformSchema := schema.Enum(platformsInterfaced, "Image Platform.")
parallels.OptionalField(nameable.NewSimpleNameable("platform"), platformSchema, func(node *node.Node) error {
platform, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
resolvedPlatform, ok := api.Platform_value[strings.ToUpper(platform)]
if !ok {
return fmt.Errorf("%w: unsupported platform name: %q", parsererror.ErrParsing, platform)
}
parallels.proto.Parallels.Platform = api.Platform(resolvedPlatform)
return nil
})
return parallels
}
func (parallels *Parallels) Parse(node *node.Node) error {
if err := parallels.DefaultParser.Parse(node); err != nil {
return err
}
return nil
}
func (parallels *Parallels) Proto() *api.Isolation_Parallels_ {
return parallels.proto
}
func (parallels *Parallels) Schema() *jsschema.Schema {
modifiedSchema := parallels.DefaultParser.Schema()
modifiedSchema.Type = jsschema.PrimitiveTypes{jsschema.ObjectType}
modifiedSchema.Description = "Parallels VM isolation."
return modifiedSchema
}