-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
172 lines (145 loc) · 3.43 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
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"encoding/json"
"io"
"net/http"
"os"
"time"
"github.com/jsteenb2/errors"
"github.com/spf13/cobra"
"github.com/jsteenb2/allsrvc"
"github.com/jsteenb2/mess/allsrv"
)
func main() {
cmd := newCmd()
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}
func newCmd() *cobra.Command {
c := new(cli)
return c.cmd()
}
const name = "allsrvc"
type cli struct {
// base flags
addr string
pass string
user string
// foo flags
id string
name string
note string
}
func (c *cli) cmd() *cobra.Command {
cmd := cobra.Command{
Use: name,
SilenceUsage: true,
}
cmd.AddCommand(
c.cmdCreateFoo(),
c.cmdReadFoo(),
c.cmdUpdateFoo(),
c.cmdRmFoo(),
)
return &cmd
}
func (c *cli) cmdCreateFoo() *cobra.Command {
cmd := cobra.Command{
Use: "add",
Aliases: []string{"create"},
Short: "creates a new foo",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
client := c.newClient()
f, err := client.CreateFoo(cmd.Context(), allsrv.Foo{
Name: c.name,
Note: c.note,
})
if err != nil {
return err
}
return json.NewEncoder(cmd.OutOrStderr()).Encode(f)
},
}
c.registerCommonFlags(&cmd)
cmd.Flags().StringVar(&c.name, "name", "", "name of the new foo")
cmd.Flags().StringVar(&c.note, "note", "", "optional foo note")
return &cmd
}
func (c *cli) cmdReadFoo() *cobra.Command {
cmd := cobra.Command{
Use: "read $FOO_ID",
Short: "read a food by id",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client := c.newClient()
f, err := client.ReadFoo(cmd.Context(), args[0])
if err != nil {
return err
}
return errors.Wrap(writeFoo(cmd.OutOrStdout(), f))
},
}
c.registerCommonFlags(&cmd)
return &cmd
}
func (c *cli) cmdUpdateFoo() *cobra.Command {
cmd := cobra.Command{
Use: "update",
Short: "updates an existing foo",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
client := c.newClient()
upd := allsrv.FooUpd{
ID: c.id,
}
if c.name != "" {
upd.Name = &c.name
}
if c.note != "" {
upd.Note = &c.note
}
f, err := client.UpdateFoo(cmd.Context(), upd)
if err != nil {
return err
}
return errors.Wrap(writeFoo(cmd.OutOrStdout(), f))
},
}
c.registerCommonFlags(&cmd)
cmd.Flags().StringVar(&c.id, "id", "", "id of the foo resource")
cmd.Flags().StringVar(&c.name, "name", "", "optional foo name")
cmd.Flags().StringVar(&c.note, "note", "", "optional foo note")
return &cmd
}
func (c *cli) cmdRmFoo() *cobra.Command {
cmd := cobra.Command{
Use: "rm $FOO_ID",
Short: "delete a foo by id",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client := c.newClient()
return client.DelFoo(cmd.Context(), args[0])
},
}
c.registerCommonFlags(&cmd)
return &cmd
}
func (c *cli) newClient() *allsrv.ClientHTTP {
return allsrv.NewClientHTTP(
c.addr,
name,
&http.Client{Timeout: 5 * time.Second},
allsrvc.WithBasicAuth(c.user, c.pass),
)
}
func (c *cli) registerCommonFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&c.addr, "addr", "http://localhost:8091", "addr for foo svc")
cmd.Flags().StringVar(&c.user, "user", "admin", "user for basic auth")
cmd.Flags().StringVar(&c.pass, "password", "pass", "password for basic auth")
}
func writeFoo(w io.Writer, f allsrv.Foo) error {
err := json.NewEncoder(w).Encode(allsrv.FooToData(f))
return errors.Wrap(err)
}