-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (147 loc) · 3.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
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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"runtime/debug"
"strings"
"time"
"github.com/igolaizola/webcli"
"github.com/igolaizola/webcli/pkg/webff"
"github.com/peterbourgon/ff/v3"
"github.com/peterbourgon/ff/v3/ffcli"
)
// Build flags
var version = ""
var commit = ""
var date = ""
func main() {
// Create signal based context
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
// Launch command
cmd := newCommand()
if err := cmd.ParseAndRun(ctx, os.Args[1:]); err != nil {
log.Fatal(err)
}
}
func newCommand() *ffcli.Command {
fs := flag.NewFlagSet("webff", flag.ExitOnError)
cmds := []*ffcli.Command{
newVersionCommand(),
newRunCommand(),
}
port := fs.Int("port", 0, "port number")
return &ffcli.Command{
ShortUsage: "webff [flags] <subcommand>",
FlagSet: fs,
Exec: func(ctx context.Context, args []string) error {
if len(args) == 0 {
s, err := webff.New(cmds, webcli.WithAppName(fs.Name()), webcli.WithAddress(fmt.Sprintf(":%d", *port)))
if err != nil {
return err
}
return s.Run(ctx)
}
return flag.ErrHelp
},
Subcommands: cmds,
}
}
func newVersionCommand() *ffcli.Command {
return &ffcli.Command{
Name: "version",
ShortUsage: "webff version",
ShortHelp: "print version",
Exec: func(ctx context.Context, args []string) error {
v := version
if v == "" {
if buildInfo, ok := debug.ReadBuildInfo(); ok {
v = buildInfo.Main.Version
}
}
if v == "" {
v = "dev"
}
versionFields := []string{v}
if commit != "" {
versionFields = append(versionFields, commit)
}
if date != "" {
versionFields = append(versionFields, date)
}
fmt.Println(strings.Join(versionFields, " "))
return nil
},
}
}
func newRunCommand() *ffcli.Command {
cmd := "run"
fs := flag.NewFlagSet(cmd, flag.ExitOnError)
_ = fs.String("config", "", "config file (optional)")
_ = fs.Duration("max-duration", 0, "duration")
_ = fs.Int("attempts", 0, "int")
_ = fs.Bool("debug", false, "bool")
_ = fs.Float64("price", 0, "float64")
return &ffcli.Command{
Name: cmd,
ShortUsage: fmt.Sprintf("webff %s [flags] <key> <value data...>", cmd),
Options: []ff.Option{
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ff.PlainParser),
ff.WithEnvVarPrefix("webff"),
},
ShortHelp: fmt.Sprintf("webff %s command", cmd),
FlagSet: fs,
Exec: func(ctx context.Context, args []string) error {
log.Println("running")
defer log.Println("finished")
for i := 0; i < 5; i++ {
select {
case <-ctx.Done():
case <-time.After(1 * time.Second):
}
fmt.Println("tick", i)
}
return nil
},
Subcommands: []*ffcli.Command{
newSubRunCommand(cmd),
},
}
}
func newSubRunCommand(parent string) *ffcli.Command {
cmd := "subrun"
fs := flag.NewFlagSet(cmd, flag.ExitOnError)
_ = fs.String("config", "", "config file (optional)")
_ = fs.Duration("max-duration", 0, "duration")
_ = fs.Int("attempts", 0, "int")
_ = fs.Bool("debug", false, "bool")
_ = fs.Float64("price", 0, "float64")
return &ffcli.Command{
Name: cmd,
ShortUsage: fmt.Sprintf("webff %s %s [flags] <key> <value data...>", parent, cmd),
Options: []ff.Option{
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ff.PlainParser),
ff.WithEnvVarPrefix("webff"),
},
ShortHelp: fmt.Sprintf("webff %s %s command", parent, cmd),
FlagSet: fs,
Exec: func(ctx context.Context, args []string) error {
log.Println("running")
defer log.Println("finished")
for i := 0; i < 5; i++ {
select {
case <-ctx.Done():
case <-time.After(1 * time.Second):
}
fmt.Println("tick", i)
}
return nil
},
}
}