-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_channel.go
125 lines (101 loc) · 2.41 KB
/
cmd_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
package main
import (
"net/http"
"github.com/Urethramancer/signor/opt"
)
type CmdChannel struct {
opt.DefaultHelp
Add CmdChannelAdd `command:"add" help:"Add a channel."`
Remove CmdChannelRemove `command:"remove" aliases:"rm" help:"Remove a channel."`
List CmdChannelList `command:"list" aliases:"ls" help:"List channels."`
}
func (cmd *CmdChannel) Run(in []string) error {
return opt.ErrUsage
}
//
// Add
//
// CmdUserAdd options.
type CmdChannelAdd struct {
opt.DefaultHelp
Name string `placeholder:"NAME" help:"Name of the new channel."`
}
// Run add.
func (cmd *CmdChannelAdd) Run(in []string) error {
if cmd.Help || cmd.Name == "" {
return opt.ErrUsage
}
headers := map[string]string{
"channel": cmd.Name,
}
res, err := Request(http.MethodPost, "channel", headers)
if err != nil {
pr("Error adding channel: %s\n", err.Error())
return err
}
if res.StatusCode == http.StatusNotModified {
pr("Channel '%s' already exists.", cmd.Name)
return nil
}
if res.StatusCode != http.StatusOK {
pr("Error adding channel: %s\n", http.StatusText(res.StatusCode))
return nil
}
println("Channel created.")
return nil
}
//
// Remove
//
// CmdChannelRemove options.
type CmdChannelRemove struct {
opt.DefaultHelp
Name string `placeholder:"NAME" help:"Name of the channel to be removed."`
}
// Run remove.
func (cmd *CmdChannelRemove) Run(in []string) error {
if cmd.Help {
return opt.ErrUsage
}
headers := map[string]string{
"channel": cmd.Name,
}
res, err := Request(http.MethodDelete, "channel", headers)
if err != nil {
pr("Error removing channel: %s\n", err.Error())
return err
}
switch res.StatusCode {
case http.StatusOK:
println("Channel removed.")
case http.StatusNotFound:
pr("Channel '%s' not found.", cmd.Name)
default:
pr("Unknown error removing '%s': %s", cmd.Name, http.StatusText(res.StatusCode))
}
return nil
}
//
// List
//
// CmdChannelList options.
type CmdChannelList struct{}
// Run list.
func (cmd *CmdChannelList) Run(in []string) error {
var list []Channel
headers := make(map[string]string)
res, err := RequestJSON(http.MethodGet, "channels", headers, &list)
if err != nil {
pr("Error listing channels: %s\nStatus: %d\n", err.Error(), res.StatusCode)
return err
}
if res.StatusCode != http.StatusOK || len(list) == 0 {
println("No channels.")
return nil
}
println("Channel:")
for _, c := range list {
println(c.Name)
}
return nil
}