-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.go
More file actions
50 lines (40 loc) · 1.24 KB
/
cmd.go
File metadata and controls
50 lines (40 loc) · 1.24 KB
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
package main
import (
"context"
"fmt"
)
type command struct {
name string
args []string
}
type commands struct {
handlers map[string]func(context.Context, *state, command) error
}
func (c *commands) register(name string, f func(context.Context, *state, command) error) {
c.handlers[name] = f
}
func (c *commands) run(ctx context.Context, s *state, cmd command) error {
f, exists := c.handlers[cmd.name]
if !exists {
return fmt.Errorf("no %s command found", cmd.name)
}
return f(ctx, s, cmd)
}
func getCliCommands() *commands {
// set up and register commands
cmds := commands{
handlers: make(map[string]func(context.Context, *state, command) error),
}
cmds.register("login", handlerLogin)
cmds.register("register", handlerRegister)
cmds.register("reset", handlerReset)
cmds.register("users", handlerGetUsers)
cmds.register("agg", handlerFetchFeed)
cmds.register("addfeed", middlewareLoggedIn(handlerAddFeed))
cmds.register("feeds", handlerListFeeds)
cmds.register("follow", middlewareLoggedIn(handlerFollowFeed))
cmds.register("following", middlewareLoggedIn(handlerFeedFollowsForUser))
cmds.register("unfollow", middlewareLoggedIn(handlerDeleteFeedFollow))
cmds.register("browse", middlewareLoggedIn(handlerListPosts))
return &cmds
}