This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
channel.go
161 lines (134 loc) · 4.92 KB
/
channel.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
154
155
156
157
158
159
160
161
package main
import (
"fmt"
"log"
"text/tabwriter"
"github.com/coreos/updateservicectl/client/update/v1"
)
var (
channelFlags struct {
appId StringFlag
channel StringFlag
version StringFlag
publish bool
}
cmdChannel = &Command{
Name: "channel",
Summary: "Manage channels for an application",
Subcommands: []*Command{
cmdChannelList,
cmdChannelUpdate,
cmdChannelCreate,
cmdChannelDelete,
},
}
cmdChannelList = &Command{
Name: "channel list",
Usage: "[OPTION]...",
Description: `List all channels for an application.`,
Run: channelList,
}
cmdChannelCreate = &Command{
Name: "channel create",
Usage: "[OPTION]...",
Description: `Given an application ID (--app-id) and channel (--channel),
you can create a new channel.`,
Run: channelCreate,
}
cmdChannelUpdate = &Command{
Name: "channel update",
Usage: "[OPTION]...",
Summary: `Update the version and publish state for an application channel.`,
Description: `Given an application ID (--app-id) and channel (--channel),
you can change the channel to a new version (--version), or set the publish state (--publish).`,
Run: channelUpdate,
}
cmdChannelDelete = &Command{
Name: "channel delete",
Usage: "[OPTION]...",
Summary: `Delete an application channel.`,
Description: `Deletes the channel with matching application ID (--app-id) and channel (--channel).`,
Run: channelDelete,
}
)
func init() {
cmdChannelList.Flags.Var(&channelFlags.appId, "app-id", "The application ID to list the channels of.")
cmdChannelCreate.Flags.Var(&channelFlags.appId, "app-id", "The application ID that the channel belongs to.")
cmdChannelCreate.Flags.Var(&channelFlags.channel, "channel", "The channel to create.")
cmdChannelCreate.Flags.BoolVar(&channelFlags.publish, "publish", false, "Publish or unpublish the channel.")
cmdChannelCreate.Flags.Var(&channelFlags.version, "version", "The version to create the channel to.")
cmdChannelUpdate.Flags.Var(&channelFlags.appId, "app-id", "The application ID that the channel belongs to.")
cmdChannelUpdate.Flags.Var(&channelFlags.channel, "channel", "The channel to update.")
cmdChannelUpdate.Flags.BoolVar(&channelFlags.publish, "publish", false, "Publish or unpublish the channel.")
cmdChannelUpdate.Flags.Var(&channelFlags.version, "version", "The version to update the channel to.")
cmdChannelDelete.Flags.Var(&channelFlags.appId, "app-id", "The application ID that the channel belongs to.")
cmdChannelDelete.Flags.Var(&channelFlags.channel, "channel", "The channel to update.")
}
const channelHeader = "Label\tVersion\tPublish\tUpstream\n"
func formatChannel(channel *update.AppChannel) string {
return fmt.Sprintf("%s\t%s\t%t\t%s\n", channel.Label, channel.Version, channel.Publish, channel.Upstream)
}
func channelList(args []string, service *update.Service, out *tabwriter.Writer) int {
if channelFlags.appId.Get() == nil {
return ERROR_USAGE
}
listCall := service.Channel.List(channelFlags.appId.String())
list, err := listCall.Do()
if err != nil {
log.Fatal(err)
}
fmt.Fprint(out, channelHeader)
for _, channel := range list.Items {
fmt.Fprintf(out, "%s", formatChannel(channel))
}
out.Flush()
return OK
}
func channelCreate(args []string, service *update.Service, out *tabwriter.Writer) int {
if channelFlags.version.Get() == nil || channelFlags.appId.Get() == nil || channelFlags.channel.Get() == nil {
return ERROR_USAGE
}
channelReq := &update.ChannelRequest{
Version: *channelFlags.version.Get(),
Publish: channelFlags.publish,
Label: *channelFlags.channel.Get(),
AppId: *channelFlags.appId.Get(),
}
call := service.Channel.Insert(*channelFlags.channel.Get(), channelReq)
channel, err := call.Do()
if err != nil {
log.Fatal(err)
}
fmt.Fprint(out, channelHeader)
fmt.Fprintf(out, "%s", formatChannel(channel))
out.Flush()
return OK
}
func channelUpdate(args []string, service *update.Service, out *tabwriter.Writer) int {
if channelFlags.version.Get() == nil || channelFlags.appId.Get() == nil || channelFlags.channel.Get() == nil {
return ERROR_USAGE
}
channelReq := &update.ChannelRequest{Version: *channelFlags.version.Get(), Publish: channelFlags.publish}
call := service.Channel.Update(channelFlags.appId.String(), channelFlags.channel.String(), channelReq)
channel, err := call.Do()
if err != nil {
log.Fatal(err)
}
fmt.Fprint(out, channelHeader)
fmt.Fprintf(out, "%s", formatChannel(channel))
out.Flush()
return OK
}
func channelDelete(args []string, service *update.Service, out *tabwriter.Writer) int {
if channelFlags.appId.Get() == nil || channelFlags.channel.Get() == nil {
return ERROR_USAGE
}
call := service.Channel.Delete(channelFlags.appId.String(), channelFlags.channel.String())
_, err := call.Do()
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(out, "deleted channel: %s, for application: %s\n", channelFlags.channel.String(), channelFlags.appId.String())
out.Flush()
return OK
}