This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
cli.go
59 lines (50 loc) · 1.59 KB
/
cli.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
package pi
import (
"fmt"
"io"
"log"
flags "github.com/jessevdk/go-flags"
)
const (
exitCodeOK = iota
exitCodeErr
)
// CLI is struct for command line tool
type CLI struct {
OutStream, ErrStream io.Writer
}
// Run the pi
func (cli *CLI) Run(argv []string) int {
log.SetOutput(cli.ErrStream)
log.SetFlags(0)
err := parseArgs(argv)
if err != nil {
if ferr, ok := err.(*flags.Error); ok {
if ferr.Type == flags.ErrHelp {
return exitCodeOK
}
return exitCodeErr
}
return exitCodeErr
}
return exitCodeOK
}
type piOpts struct {
Users usersCommand `description:"operate Users" command:"users" subcommands-optional:"true"`
Channels channelsCommand `description:"operate Channels" command:"channels" subcommands-optional:"true"`
Graphs graphsCommand `description:"operate Graphs" command:"graphs" subcommands-optional:"true"`
Pixel pixelCommand `description:"operate Pixel in Graph" command:"pixel" subcommands-optional:"true"`
Webhooks webhooksCommand `description:"operate Webhooks" command:"webhooks" subcommands-optional:"true"`
Ver verCommand `description:"display version" command:"version" subcommands-optional:"true"`
Notifications notificationsCommand `description:"operate Notifications" command:"ntf" subcommands-optional:"true"`
}
type verCommand struct{}
func (b *verCommand) Execute(args []string) error {
fmt.Printf("pi version: %s (rev: %s)\n", version, revision)
return nil
}
func parseArgs(args []string) error {
opts := &piOpts{}
_, err := flags.ParseArgs(opts, args)
return err
}