-
Notifications
You must be signed in to change notification settings - Fork 38
/
commands.go
239 lines (188 loc) · 3.82 KB
/
commands.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/boltdb/bolt"
)
// Command ...
type Command interface {
Name() string
Desc() string
Exec(w http.ResponseWriter, args []string) error
}
var commands map[string]Command
func init() {
commands = make(map[string]Command)
RegisterCommand("list", List{})
RegisterCommand("ping", Ping{})
RegisterCommand("help", Help{})
RegisterCommand("date", Date{})
RegisterCommand("time", Time{})
RegisterCommand("add", Add{})
}
// RegisterCommand ...
func RegisterCommand(name string, command Command) {
commands[name] = command
}
// LookupCommand ...
func LookupCommand(name string) Command {
command, ok := commands[name]
if ok {
return command
}
return nil
}
// Ping ...
type Ping struct{}
// Name ...
func (p Ping) Name() string {
return "ping"
}
// Desc ...
func (p Ping) Desc() string {
return `ping
Responds with "pong <ts>" where ts is the current UNIX timestamp.
`
}
// Exec ...
func (p Ping) Exec(w http.ResponseWriter, args []string) error {
w.Write([]byte(fmt.Sprintf("pong %d", time.Now().Unix())))
return nil
}
// List ...
type List struct{}
// Name ...
func (p List) Name() string {
return "list"
}
// Desc ...
func (p List) Desc() string {
return `list
Lists all available commands.
`
}
// Exec ...
func (p List) Exec(w http.ResponseWriter, args []string) error {
var bs, cs []string
err := db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("bookmarks"))
if b == nil {
return nil
}
err := b.ForEach(func(k, v []byte) error {
bs = append(bs, string(k))
return nil
})
return err
})
if err != nil {
log.Printf("error reading list of bookmarks: %s", err)
}
for k := range commands {
cs = append(cs, k)
}
w.Write(
[]byte(
fmt.Sprintf(
"Bookmarks:\n\n%s\n\nCommands:\n\n%s",
strings.Join(bs, "\n"),
strings.Join(cs, "\n"),
),
),
)
return nil
}
// Help ...
type Help struct{}
// Name ...
func (p Help) Name() string {
return "help"
}
// Desc ...
func (p Help) Desc() string {
return `help
Display general helpful information
`
}
// Exec ...
func (p Help) Exec(w http.ResponseWriter, args []string) error {
render(w, "help", nil)
return nil
}
// Date ...
type Date struct{}
// Name ...
func (p Date) Name() string {
return "date"
}
// Desc ...
func (p Date) Desc() string {
return `date
Display the current date and time
`
}
// Exec ...
func (p Date) Exec(w http.ResponseWriter, args []string) error {
w.Write([]byte(time.Now().Format(http.TimeFormat)))
return nil
}
// Time ...
type Time struct{}
// Name ...
func (p Time) Name() string {
return "time"
}
// Desc ...
func (p Time) Desc() string {
return `time
Display the current time
`
}
// Exec ...
func (p Time) Exec(w http.ResponseWriter, args []string) error {
w.Write([]byte(time.Now().Format("15:04:05")))
return nil
}
// Add ...
type Add struct{}
// Name ...
func (p Add) Name() string {
return "add"
}
// Desc ...
func (p Add) Desc() string {
return `add [name] [url]
Adds a new bookmark with the given name that will redierct to the given
url passing arguments as %s. For example:
add g http://google.com/search?btnK&q=%s
Will add a new command called 'g' which will redirect to Google's search
passing in arguments as '%s'
`
}
// Exec ...
func (p Add) Exec(w http.ResponseWriter, args []string) error {
var name, url string
if len(args) == 2 {
name, url = args[0], args[1]
} else {
return fmt.Errorf("expected 2 arguments got %d", len(args))
}
err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("bookmarks"))
if err != nil {
log.Printf("create bucket failed: %s", err)
return err
}
err = b.Put([]byte(name), []byte(url))
if err != nil {
log.Printf("put key failed: %s", err)
return err
}
w.Write([]byte("OK"))
return nil
})
return err
}