-
Notifications
You must be signed in to change notification settings - Fork 18
/
piperesources.go
64 lines (53 loc) · 1.66 KB
/
piperesources.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
package task
import (
"github.com/cirruslabs/cirrus-cli/pkg/parser/instance/resources"
"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/parserkit"
"github.com/cirruslabs/cirrus-cli/pkg/parser/schema"
jsschema "github.com/lestrrat-go/jsschema"
"strconv"
)
type PipeResources struct {
cpu float32
memory uint32
parseable.DefaultParser
}
func NewPipeResources(mergedEnv map[string]string) *PipeResources {
res := &PipeResources{}
res.OptionalField(nameable.NewSimpleNameable("cpu"), schema.Number(""), func(node *node.Node) error {
cpu, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
cpuFloat, err := strconv.ParseFloat(cpu, 32)
if err != nil {
return err
}
res.cpu = float32(cpuFloat)
return nil
})
res.OptionalField(nameable.NewSimpleNameable("memory"), schema.Memory(), func(node *node.Node) error {
memory, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
memoryParsed, err := resources.ParseMegaBytes(memory)
if err != nil {
return node.ParserError("%s", err.Error())
}
res.memory = uint32(memoryParsed)
return nil
})
return res
}
func (res *PipeResources) Parse(node *node.Node, parserKit *parserkit.ParserKit) error {
return res.DefaultParser.Parse(node, parserKit)
}
func (res *PipeResources) Schema() *jsschema.Schema {
modifiedSchema := res.DefaultParser.Schema()
modifiedSchema.Type = jsschema.PrimitiveTypes{jsschema.ObjectType}
modifiedSchema.Description = "Pipe resources"
return modifiedSchema
}