-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathparse.go
More file actions
116 lines (93 loc) · 2.81 KB
/
parse.go
File metadata and controls
116 lines (93 loc) · 2.81 KB
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
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
//go:build go1.24
// Package genericresource is a local fork of SwarmKit's [genericresource] package,
// without protobuf dependencies.
//
// [genericresource]: https://github.com/moby/swarmkit/blob/v2.1.1/api/genericresource/parse.go
package genericresource
import (
"encoding/csv"
"fmt"
"strconv"
"strings"
api "github.com/moby/moby/api/types/swarm"
)
func newParseError(format string, args ...any) error {
return fmt.Errorf("could not parse GenericResource: "+format, args...)
}
// discreteResourceVal returns an int64 if the string is a discreteResource
// and an error if it isn't
func discreteResourceVal(res string) (int64, error) {
return strconv.ParseInt(res, 10, 64)
}
// allNamedResources returns true if the array of resources are all namedResources
// e.g: res = [red, orange, green]
func allNamedResources(res []string) bool {
for _, v := range res {
if _, err := discreteResourceVal(v); err == nil {
return false
}
}
return true
}
// ParseCmd parses the Generic Resource command line argument
// and returns a list of api.GenericResource
func ParseCmd(cmd string) ([]api.GenericResource, error) {
if strings.Contains(cmd, "\n") {
return nil, newParseError("unexpected '\\n' character")
}
r := csv.NewReader(strings.NewReader(cmd))
records, err := r.ReadAll()
if err != nil {
return nil, newParseError("%v", err)
}
if len(records) != 1 {
return nil, newParseError("found multiple records while parsing cmd %v", records)
}
return Parse(records[0])
}
// Parse parses a table of GenericResource resources
func Parse(cmds []string) ([]api.GenericResource, error) {
tokens := make(map[string][]string)
for _, term := range cmds {
kva := strings.Split(term, "=")
if len(kva) != 2 {
return nil, newParseError("incorrect term %s, missing"+
" '=' or malformed expression", term)
}
key := strings.TrimSpace(kva[0])
val := strings.TrimSpace(kva[1])
tokens[key] = append(tokens[key], val)
}
var rs []api.GenericResource
for k, v := range tokens {
if u, ok := isDiscreteResource(v); ok {
if u < 0 {
return nil, newParseError("cannot ask for"+
" negative resource %s", k)
}
rs = append(rs, NewDiscrete(k, u))
continue
}
if allNamedResources(v) {
rs = append(rs, NewSet(k, v...)...)
continue
}
return nil, newParseError("mixed discrete and named resources"+
" in expression '%s=%s'", k, v)
}
return rs, nil
}
// isDiscreteResource returns true if the array of resources is a
// Discrete Resource.
// e.g: res = [1]
func isDiscreteResource(values []string) (int64, bool) {
if len(values) != 1 {
return int64(0), false
}
u, err := discreteResourceVal(values[0])
if err != nil {
return int64(0), false
}
return u, true
}