-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.go
80 lines (72 loc) · 1.52 KB
/
parse.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
package discovery
import (
"fmt"
"net/url"
"runtime"
"strings"
"github.com/coreos/rocket/app-container/schema/types"
)
const (
defaultVersion = "latest"
defaultOS = runtime.GOOS
defaultArch = runtime.GOARCH
)
type App struct {
Name types.ACName
Labels map[string]string
}
func NewApp(name string, labels map[string]string) (*App, error) {
if labels == nil {
labels = make(map[string]string, 0)
}
acn, err := types.NewACName(name)
if err != nil {
return nil, err
}
return &App{
Name: *acn,
Labels: labels,
}, nil
}
// NewAppFromString takes a command line app parameter and returns a map of labels.
//
// Example app parameters:
// example.com/reduce-worker:1.0.0
// example.com/reduce-worker,channel=alpha,label=value
func NewAppFromString(app string) (*App, error) {
var (
name string
labels map[string]string
)
app = strings.Replace(app, ":", ",version=", -1)
app = "name=" + app
v, err := url.ParseQuery(strings.Replace(app, ",", "&", -1))
if err != nil {
return nil, err
}
labels = make(map[string]string, 0)
for key, val := range v {
if len(val) > 1 {
return nil, fmt.Errorf("label %s with multiple values %q", key, val)
}
if key == "name" {
name = val[0]
continue
}
labels[key] = val[0]
}
if labels["version"] == "" {
labels["version"] = defaultVersion
}
if labels["os"] == "" {
labels["os"] = defaultOS
}
if labels["arch"] == "" {
labels["arch"] = defaultArch
}
a, err := NewApp(name, labels)
if err != nil {
return nil, err
}
return a, nil
}