-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (77 loc) · 2.67 KB
/
main.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
package main
import (
"context"
"fmt"
"strings"
"github.com/evilmonkeyinc/golang-cli/commands"
"github.com/evilmonkeyinc/golang-cli/errors"
"github.com/evilmonkeyinc/golang-cli/flags"
"github.com/evilmonkeyinc/golang-cli/middleware"
"github.com/evilmonkeyinc/golang-cli/shell"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
pingCommand := &commands.Command{
Name: "Ping",
Summary: "Simple ping pong command",
Description: "Simple command that will output the word pong",
Flags: func(fd flags.FlagDefiner) {
fd.String("suffix", "", "add a suffix to the response")
},
Function: func(rw shell.ResponseWriter, r *shell.Request) error {
message := "pong"
if suffix, ok := r.FlagValues().GetString("suffix"); ok {
message = fmt.Sprintf("%s%s", message, suffix)
}
if toUpper, ok := r.FlagValues().GetBool("toUpper"); ok && toUpper {
message = strings.ToUpper(message)
}
fmt.Fprintln(rw, message)
return nil
},
}
newShell := new(shell.Shell)
newShell.Options(shell.OptionHelpHandler(&commands.HelpCommand{Usage: "help"}))
newShell.Flags(flags.FlagHandlerFunction(func(fd flags.FlagDefiner) {
fd.Bool("toUpper", false, "make the response uppercase")
}))
newShell.Use(middleware.Recoverer())
newShell.Handle("ping", pingCommand)
newShell.Handle("users", commands.NewCommandRouter("Users", "Commands for user management", "A series of commands to aid in user management", "users add|delete|list", func(r shell.Router) {
r.Handle("list", &commands.Command{
Name: "List",
Summary: "List users",
Description: "Will list all valid users",
Usage: "list",
Function: func(rw shell.ResponseWriter, r *shell.Request) error {
return fmt.Errorf("list function called")
},
})
r.Handle("add", &commands.Command{
Name: "Add",
Summary: "Add user",
Description: "Will add a new user",
Usage: "add email@example.com",
Function: func(rw shell.ResponseWriter, r *shell.Request) error {
return fmt.Errorf("add function called")
},
})
r.Handle("delete", &commands.Command{
Name: "Delete",
Summary: "Delete user",
Description: "Will delete an existing user",
Usage: "delete email@example.com",
Function: func(rw shell.ResponseWriter, r *shell.Request) error {
return fmt.Errorf("delete function called")
},
})
}))
newShell.HandleFunction("secret", func(rw shell.ResponseWriter, r *shell.Request) error {
panic("this command should not be called.")
})
newShell.HandleFunction("help", func(shell.ResponseWriter, *shell.Request) error {
return errors.HelpRequested("help command")
})
newShell.Execute(ctx)
}