File: parse.go

package info (click to toggle)
docker.io 26.1.5%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,576 kB
  • sloc: sh: 5,748; makefile: 912; ansic: 664; asm: 228; python: 162
file content (111 lines) | stat: -rw-r--r-- 2,517 bytes parent folder | download | duplicates (2)
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
package genericresource

import (
	"encoding/csv"
	"fmt"
	"strconv"
	"strings"

	"github.com/moby/swarmkit/v2/api"
)

func newParseError(format string, args ...interface{}) 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

}