-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (128 loc) · 3.31 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"runtime/debug"
"strings"
"time"
"github.com/igolaizola/webcli"
"github.com/igolaizola/webcli/pkg/webcobra"
"github.com/spf13/cobra"
)
// 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 commandwebff
rootCmd := newCommand()
if err := rootCmd.ExecuteContext(ctx); err != nil {
log.Fatal(err)
}
}
func newCommand() *cobra.Command {
rootCmd := &cobra.Command{
Use: "webcobra",
Short: "webcobra [flags] <subcommand>",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
port, _ := cmd.Flags().GetInt("port")
s, err := webcobra.New(cmd.Commands(), webcli.WithAppName(cmd.Name()), webcli.WithAddress(fmt.Sprintf(":%d", port)))
if err != nil {
return err
}
return s.Run(cmd.Context())
}
return cmd.Help()
},
}
// Disable default commands
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
rootCmd.Flags().Int("port", 0, "port number")
rootCmd.AddCommand(newVersionCommand())
rootCmd.AddCommand(newRunCommand())
return rootCmd
}
func newVersionCommand() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print version",
Run: func(cmd *cobra.Command, args []string) {
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, " "))
},
}
}
func newRunCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "webcobra run command",
RunE: func(cmd *cobra.Command, args []string) error {
log.Println("running")
defer log.Println("finished")
for i := 0; i < 5; i++ {
select {
case <-cmd.Context().Done():
case <-time.After(1 * time.Second):
}
fmt.Println("tick", i)
}
return nil
},
}
cmd.Flags().String("config", "", "config file (optional)")
cmd.Flags().Duration("max-duration", 0, "duration")
cmd.Flags().Int("attempts", 0, "int")
cmd.Flags().Bool("debug", false, "bool")
cmd.Flags().Float64("price", 0, "float64")
cmd.Flags().StringArray("tags", []string{"bar", "foo"}, "tags")
cmd.AddCommand(newSubRunCommand(cmd.Name()))
return cmd
}
func newSubRunCommand(parent string) *cobra.Command {
cmd := &cobra.Command{
Use: "subrun",
Short: fmt.Sprintf("webcobra %s subrun command", parent),
RunE: func(cmd *cobra.Command, args []string) error {
log.Println("running")
defer log.Println("finished")
for i := 0; i < 5; i++ {
select {
case <-cmd.Context().Done():
case <-time.After(1 * time.Second):
}
fmt.Println("tick", i)
}
return nil
},
}
cmd.Flags().String("config", "", "config file (optional)")
cmd.Flags().Duration("max-duration", 0, "duration")
cmd.Flags().Int("attempts", 0, "int")
cmd.Flags().Bool("debug", false, "bool")
cmd.Flags().Float64("price", 0, "float64")
cmd.Flags().StringArray("tags", []string{"bar", "foo"}, "tags")
return cmd
}