/
add.go
118 lines (101 loc) · 2.36 KB
/
add.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
118
package commands
import (
"fmt"
"log"
"strings"
"github.com/codegangsta/cli"
"github.com/lingo-reviews/lingo/util"
)
var AddCMD = cli.Command{
Name: "add",
Usage: "add a tenet to lingo",
Description: `
"lingo remove github.com/lingo-reviews/unused-args"
`[1:],
Flags: []cli.Flag{
cli.StringFlag{
Name: "driver",
Value: "docker",
Usage: "driver to use for this tenet",
},
cli.StringFlag{
Name: "registry",
Value: "hub.docker.com",
Usage: "the registry this tenet should be pulled from",
},
cli.StringFlag{
Name: "group",
Value: "default",
Usage: "group to add tenet to",
},
cli.StringFlag{
Name: "options",
Value: "",
Usage: "a space separated list of key=value options",
},
},
Action: add,
BashComplete: func(c *cli.Context) {
// This will complete if no args are passed
if len(c.Args()) > 0 {
return
}
tenets, err := util.BinTenets()
if err != nil {
log.Printf("auto complete error %v", err)
return
}
for _, t := range tenets {
fmt.Println(t)
}
},
}
func add(c *cli.Context) {
if l := len(c.Args()); l != 1 {
oserrf("expected 1 argument, got %d", l)
return
}
cfgPath, err := tenetCfgPath(c)
if err != nil {
oserrf("reading config file: %s", err.Error())
return
}
cfg, err := buildConfig(cfgPath, CascadeNone)
if err != nil {
oserrf("reading config file: %s", err.Error())
return
}
imageName := c.Args().First()
groupName := c.String("group")
g, err := cfg.FindTenetGroup(groupName)
if err == nil && hasTenet(g.Tenets, imageName) {
oserrf(`error: tenet "%s" already added`, imageName)
return
}
// TODO(waigani) DEMOWARE. This will panic with wrong input. Matt didn't
// your first PR bring in options?
opts := map[string]interface{}{}
if optStr := c.String("options"); optStr != "" {
// TODO: DEMOWARE. Only set one option at a time to allow spaces in value
//for _, part := range strings.Split(optStr, " ") {
p := strings.Split(optStr, "=")
opts[p[0]] = p[1]
//}
}
var registry string
driver := c.String("driver")
if driver == "docker" {
registry = c.String("registry")
}
cfg.AddTenet(TenetConfig{
Name: imageName,
Driver: driver,
Registry: registry,
Options: opts,
}, groupName)
if err := writeConfigFile(c, cfg); err != nil {
oserrf(err.Error())
return
}
// TODO(waigani) open an interactive shell, prompt to set options.
}