This repository was archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathapp.go
153 lines (122 loc) · 3.51 KB
/
app.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"fmt"
"log"
"text/tabwriter"
"github.com/pborman/uuid"
"github.com/coreos/updateservicectl/client/update/v1"
)
var (
appFlags struct {
appId StringFlag
label StringFlag
description StringFlag
}
cmdApp = &Command{
Name: "app",
Summary: "Manage applications.",
Subcommands: []*Command{
cmdAppCreate,
cmdAppList,
cmdAppUpdate,
cmdAppDelete,
},
}
cmdAppCreate = &Command{
Name: "app create",
Usage: "[OPTION]...",
Description: `Create a new application.`,
Run: appCreate,
}
cmdAppList = &Command{
Name: "app list",
Description: `List all of the apps that exist including their label, token and update state.`,
Run: appList,
}
cmdAppUpdate = &Command{
Name: "app update",
Usage: "[OPTION]...",
Description: `Update an app's label or description.`,
Run: appUpdate,
}
cmdAppDelete = &Command{
Name: "app delete",
Usage: "[OPTION]...",
Description: `Delete an app.`,
Run: appDelete,
}
)
func init() {
cmdAppCreate.Flags.Var(&appFlags.appId, "app-id", "Application UUID. If not provided, one will be randomly generated.")
cmdAppCreate.Flags.Var(&appFlags.label, "label", "New application label.")
cmdAppCreate.Flags.Var(&appFlags.description, "description", "New application description.")
cmdAppUpdate.Flags.Var(&appFlags.appId, "app-id", "Application ID to update.")
cmdAppUpdate.Flags.Var(&appFlags.label, "label", "Set application label.")
cmdAppUpdate.Flags.Var(&appFlags.description, "description", "Set application description.")
cmdAppDelete.Flags.Var(&appFlags.appId, "app-id", "Application ID to delete.")
}
const appHeader = "Id\tLabel\tDescription\n"
func formatApp(app *update.App) string {
return fmt.Sprintf("%s\t%s\t%s\n", app.Id, app.Label, app.Description)
}
func appList(args []string, service *update.Service, out *tabwriter.Writer) int {
listCall := service.App.List()
list, err := listCall.Do()
if err != nil {
log.Fatal(err)
}
fmt.Fprint(out, appHeader)
for _, app := range list.Items {
fmt.Fprintf(out, "%s", formatApp(app))
}
out.Flush()
return OK
}
func appCreate(args []string, service *update.Service, out *tabwriter.Writer) int {
if appFlags.appId.Get() == nil {
appFlags.appId.Set(uuid.New())
}
appReq := &update.AppInsertReq{
Id: appFlags.appId.String(),
Label: appFlags.label.String(),
Description: appFlags.description.String(),
}
call := service.App.Insert(appReq)
app, err := call.Do()
if err != nil {
log.Fatal(err)
}
fmt.Fprint(out, appHeader)
fmt.Fprintf(out, "%s", formatApp(app))
out.Flush()
return OK
}
func appUpdate(args []string, service *update.Service, out *tabwriter.Writer) int {
if appFlags.appId.Get() == nil || appFlags.label.Get() == nil || appFlags.description.Get() == nil {
return ERROR_USAGE
}
appReq := &update.AppUpdateReq{Label: appFlags.label.String(), Description: appFlags.description.String()}
call := service.App.Update(appFlags.appId.String(), appReq)
app, err := call.Do()
if err != nil {
log.Fatal(err)
}
fmt.Fprint(out, appHeader)
fmt.Fprintf(out, "%s", formatApp(app))
out.Flush()
return OK
}
func appDelete(args []string, service *update.Service, out *tabwriter.Writer) int {
if appFlags.appId.Get() == nil {
return ERROR_USAGE
}
call := service.App.Delete(appFlags.appId.String())
app, err := call.Do()
if err != nil {
log.Fatal(err)
}
fmt.Fprint(out, appHeader)
fmt.Fprintf(out, "%s", formatApp(app))
out.Flush()
return OK
}