Skip to content

Commit 029f39e

Browse files
committed
feat(label-cmd): add Label cmd
Add new Label command. Labels are general key value string to each app just for administration purpose. See workflow#597
1 parent 8782839 commit 029f39e

5 files changed

Lines changed: 246 additions & 0 deletions

File tree

cmd/apps.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ func (d *DeisCmd) AppInfo(appID string) error {
134134
return err
135135
}
136136

137+
d.Println()
138+
// print the app labels
139+
if err = d.LabelsList(app.ID); err != nil {
140+
return err
141+
}
142+
137143
d.Println()
138144

139145
return nil

cmd/cmd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ type Commander interface {
5252
KeysList(int) error
5353
KeyRemove(string) error
5454
KeyAdd(string, string) error
55+
LabelsList(string) error
56+
LabelsSet(string, []string) error
57+
LabelsUnset(string, []string) error
5558
LimitsList(string) error
5659
LimitsSet(string, []string, string) error
5760
LimitsUnset(string, []string, string) error

cmd/labels.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"github.com/deis/controller-sdk-go/api"
6+
"github.com/deis/controller-sdk-go/appsettings"
7+
"strings"
8+
)
9+
10+
// LabelsList list app's labels
11+
func (d *DeisCmd) LabelsList(appID string) error {
12+
s, appID, err := load(d.ConfigFile, appID)
13+
14+
if err != nil {
15+
return err
16+
}
17+
18+
appSettings, err := appsettings.List(s.Client, appID)
19+
if d.checkAPICompatibility(s.Client, err) != nil {
20+
return err
21+
}
22+
23+
d.Printf("=== %s Label\n", appID)
24+
25+
if appSettings.Label == nil || len(appSettings.Label) == 0 {
26+
d.Println("No labels found.")
27+
} else {
28+
d.Println(appSettings.Label)
29+
}
30+
31+
return nil
32+
}
33+
34+
// LabelsSet sets labels for app
35+
func (d *DeisCmd) LabelsSet(appID string, labels []string) error {
36+
s, appID, err := load(d.ConfigFile, appID)
37+
38+
if err != nil {
39+
return err
40+
}
41+
42+
labelsMap, err := parseLabels(labels)
43+
if err != nil {
44+
return err
45+
}
46+
47+
d.Printf("Applying labels on %s... ", appID)
48+
49+
quit := progress(d.WOut)
50+
51+
_, err = appsettings.Set(s.Client, appID, api.AppSettings{Label: labelsMap})
52+
53+
quit <- true
54+
<-quit
55+
56+
if err != nil {
57+
return err
58+
}
59+
60+
d.Println("done")
61+
return nil
62+
}
63+
64+
// LabelsUnset removes labels for the app.
65+
func (d *DeisCmd) LabelsUnset(appID string, labels []string) error {
66+
s, appID, err := load(d.ConfigFile, appID)
67+
68+
if err != nil {
69+
return err
70+
}
71+
72+
labelsMap := make(map[string]interface{})
73+
74+
for _, label := range labels {
75+
labelsMap[label] = nil
76+
}
77+
78+
d.Printf("Removing labels on %s... ", appID)
79+
80+
quit := progress(d.WOut)
81+
82+
_, err = appsettings.Set(s.Client, appID, api.AppSettings{Label: labelsMap})
83+
84+
quit <- true
85+
<-quit
86+
87+
if err != nil {
88+
return err
89+
}
90+
91+
d.Println("done")
92+
return nil
93+
}
94+
95+
func parseLabels(labels []string) (map[string]interface{}, error) {
96+
labelsMap := make(map[string]interface{})
97+
98+
for _, label := range labels {
99+
key, value, err := parseLabel(label)
100+
101+
if err != nil {
102+
return nil, err
103+
}
104+
105+
labelsMap[key] = value
106+
}
107+
108+
return labelsMap, nil
109+
}
110+
111+
func parseLabel(label string) (string, string, error) {
112+
parts := strings.Split(label, "=")
113+
114+
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
115+
return "", "", fmt.Errorf(`%s is invalid, Must be in format key=value
116+
Examples: git_repo=https://github.com/deis/workflow team=frontend`, label)
117+
}
118+
119+
return parts[0], parts[1], nil
120+
}

deis.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Subcommands, use 'deis help [subcommand]' to learn more::
5656
git manage git for applications
5757
healthchecks manage healthchecks for applications
5858
keys manage ssh keys used for 'git push' deployments
59+
labels manage labels of application
5960
limits manage resource limits for your application
6061
perms manage permissions for applications
6162
ps manage processes inside an app container
@@ -128,6 +129,8 @@ Use 'git push deis master' to deploy to an application.
128129
return 0
129130
case "keys":
130131
err = parser.Keys(argv, &cmdr)
132+
case "labels":
133+
err = parser.Labels(argv, &cmdr)
131134
case "limits":
132135
err = parser.Limits(argv, &cmdr)
133136
case "perms":

parser/labels.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package parser
2+
3+
import (
4+
"github.com/deis/workflow-cli/cmd"
5+
docopt "github.com/docopt/docopt-go"
6+
)
7+
8+
// Labels displays all relevant commands for `deis label`.
9+
func Labels(argv []string, cmdr cmd.Commander) error {
10+
usage := `
11+
Valid commands for labels:
12+
13+
labels:list list application's labels
14+
labels:set add new application's label
15+
labels:unset remove application's label
16+
17+
Use 'deis help [command]' to learn more.
18+
`
19+
20+
switch argv[0] {
21+
case "labels:list":
22+
return labelsList(argv, cmdr)
23+
case "labels:set":
24+
return labelsSet(argv, cmdr)
25+
case "labels:unset":
26+
return labelsUnset(argv, cmdr)
27+
default:
28+
if printHelp(argv, usage) {
29+
return nil
30+
}
31+
32+
if argv[0] == "labels" {
33+
argv[0] = "labels:list"
34+
return labelsList(argv, cmdr)
35+
}
36+
37+
PrintUsage(cmdr)
38+
return nil
39+
}
40+
}
41+
42+
func labelsList(argv []string, cmdr cmd.Commander) error {
43+
usage := `
44+
Prints a list of labels of the application.
45+
46+
Usage: deis labels:list [options]
47+
48+
Options:
49+
-a --app=<app>
50+
the uniquely identifiable name for the application.
51+
`
52+
53+
args, err := docopt.Parse(usage, argv, true, "", false, true)
54+
55+
if err != nil {
56+
return err
57+
}
58+
59+
return cmdr.LabelsList(safeGetValue(args, "--app"))
60+
}
61+
62+
func labelsSet(argv []string, cmdr cmd.Commander) error {
63+
usage := `
64+
Sets labels for an application.
65+
66+
A label is a key/value pair used to label an application. This label is a general information for deis user.
67+
Mostly used for administraton/maintenance information, note for application. This information isn't send to scheduler.
68+
69+
Usage: deis labels:set [options] <key>=<value>...
70+
71+
Arguments:
72+
<key> the label key, for example: "git_repo" or "team"
73+
<value> the label value, for example: "https://github.com/deis/workflow" or "frontend"
74+
75+
Options:
76+
-a --app=<app>
77+
the uniquely identifiable name for the application.
78+
`
79+
80+
args, err := docopt.Parse(usage, argv, true, "", false, true)
81+
if err != nil {
82+
return err
83+
}
84+
85+
app := safeGetValue(args, "--app")
86+
tags := args["<key>=<value>"].([]string)
87+
88+
return cmdr.LabelsSet(app, tags)
89+
}
90+
91+
func labelsUnset(argv []string, cmdr cmd.Commander) error {
92+
usage := `
93+
Unsets labels for an application.
94+
95+
Usage: deis labels:unset [options] <key>...
96+
97+
Arguments:
98+
<key> the label key to unset, for example: "git_repo" or "team"
99+
100+
Options:
101+
-a --app=<app>
102+
the uniquely identifiable name for the application.
103+
`
104+
105+
args, err := docopt.Parse(usage, argv, true, "", false, true)
106+
if err != nil {
107+
return err
108+
}
109+
110+
app := safeGetValue(args, "--app")
111+
tags := args["<key>"].([]string)
112+
113+
return cmdr.LabelsUnset(app, tags)
114+
}

0 commit comments

Comments
 (0)