-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
57 lines (51 loc) · 1.46 KB
/
main.go
File metadata and controls
57 lines (51 loc) · 1.46 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
51
52
53
54
55
56
57
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/evilmonkeyinc/golang-cli/commands"
"github.com/evilmonkeyinc/golang-cli/errors"
"github.com/evilmonkeyinc/golang-cli/middleware"
"github.com/evilmonkeyinc/golang-cli/shell"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
newShell := new(shell.Shell)
newShell.Options(
shell.OptionShellPrompt("example>"),
shell.OptionExitOnError(true),
shell.OptionHelpHandler(&commands.HelpCommand{Usage: "help"}),
)
newShell.Use(middleware.Recoverer())
newShell.HandleFunction("help", func(shell.ResponseWriter, *shell.Request) error {
return errors.HelpRequested("command")
})
newShell.Handle("ping", &commands.Command{
Name: "Ping",
Summary: "Simple ping pong command",
Description: "Simple command that will output the word pong",
Function: func(rw shell.ResponseWriter, r *shell.Request) error {
fmt.Fprintln(rw, "pong")
return nil
},
})
newShell.HandleFunction("secret", func(rw shell.ResponseWriter, r *shell.Request) error {
panic("this command should not be called.")
})
newShell.HandleFunction("exit", func(rw shell.ResponseWriter, r *shell.Request) error {
return fmt.Errorf("exit error")
})
go newShell.Start(ctx)
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
select {
case <-signals:
cancel()
<-newShell.Closed()
case <-newShell.Closed():
cancel()
}
}