/
main.go
279 lines (231 loc) · 9.02 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Code generated by truss.
// Rerunning truss will overwrite this file.
// DO NOT EDIT!
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"strconv"
"strings"
"time"
"golang.org/x/net/context"
"google.golang.org/grpc"
"github.com/pkg/errors"
// This Service
pb "github.com/adamryman/ambition-model/ambition-service"
"github.com/adamryman/ambition-model/ambition-service/svc/client/cli/handlers"
grpcclient "github.com/adamryman/ambition-model/ambition-service/svc/client/grpc"
httpclient "github.com/adamryman/ambition-model/ambition-service/svc/client/http"
)
var (
_ = strconv.ParseInt
_ = strings.Split
_ = json.Compact
_ = errors.Wrapf
_ = pb.RegisterAmbitionServer
)
func main() {
os.Exit(submain())
}
// submain exists to act as the functional main, but will return exit codes to
// the actual main instead of calling os.Exit directly. This is done to allow
// the defered functions to be called, since if os.Exit where called directly
// from this function, none of the defered functions set up by this function
// would be called.
func submain() int {
var (
httpAddr = flag.String("http.addr", "", "HTTP address of addsvc")
grpcAddr = flag.String("grpc.addr", ":5040", "gRPC (HTTP) address of addsvc")
)
// The addcli presumes no service discovery system, and expects users to
// provide the direct address of an service. This presumption is reflected in
// the cli binary and the the client packages: the -transport.addr flags
// and various client constructors both expect host:port strings.
fsCreateAction := flag.NewFlagSet("createaction", flag.ExitOnError)
fsCreateOccurrence := flag.NewFlagSet("createoccurrence", flag.ExitOnError)
fsReadAction := flag.NewFlagSet("readaction", flag.ExitOnError)
fsReadActions := flag.NewFlagSet("readactions", flag.ExitOnError)
fsReadOccurrences := flag.NewFlagSet("readoccurrences", flag.ExitOnError)
fsReadOccurrencesByDate := flag.NewFlagSet("readoccurrencesbydate", flag.ExitOnError)
var (
flagUserIDReadActions = fsReadActions.Int64("userid", 0, "")
flagActionIDReadOccurrencesByDate = fsReadOccurrencesByDate.Int64("actionid", 0, "")
flagStartDateReadOccurrencesByDate = fsReadOccurrencesByDate.String("startdate", "", "")
flagEndDateReadOccurrencesByDate = fsReadOccurrencesByDate.String("enddate", "", "")
flagIDReadOccurrences = fsReadOccurrences.Int64("id", 0, "")
flagNameReadOccurrences = fsReadOccurrences.String("name", "", "")
flagUserIDReadOccurrences = fsReadOccurrences.Int64("userid", 0, "")
flagIDCreateAction = fsCreateAction.Int64("id", 0, "")
flagNameCreateAction = fsCreateAction.String("name", "", "")
flagUserIDCreateAction = fsCreateAction.Int64("userid", 0, "")
flagUserIDCreateOccurrence = fsCreateOccurrence.Int64("userid", 0, "")
flagOccurrenceCreateOccurrence = fsCreateOccurrence.String("occurrence", "", "")
flagIDReadAction = fsReadAction.Int64("id", 0, "")
flagNameReadAction = fsReadAction.String("name", "", "")
flagUserIDReadAction = fsReadAction.Int64("userid", 0, "")
)
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "Subcommands:\n")
fmt.Fprintf(os.Stderr, " %s\n", "createaction")
fmt.Fprintf(os.Stderr, " %s\n", "createoccurrence")
fmt.Fprintf(os.Stderr, " %s\n", "readaction")
fmt.Fprintf(os.Stderr, " %s\n", "readactions")
fmt.Fprintf(os.Stderr, " %s\n", "readoccurrences")
fmt.Fprintf(os.Stderr, " %s\n", "readoccurrencesbydate")
}
if len(os.Args) < 2 {
flag.Usage()
return 1
}
flag.Parse()
var (
service pb.AmbitionServer
err error
)
if *httpAddr != "" {
service, err = httpclient.New(*httpAddr)
} else if *grpcAddr != "" {
conn, err := grpc.Dial(*grpcAddr, grpc.WithInsecure(), grpc.WithTimeout(time.Second))
if err != nil {
fmt.Fprintf(os.Stderr, "Error while dialing grpc connection: %v", err)
return 1
}
defer conn.Close()
service, err = grpcclient.New(conn)
} else {
fmt.Fprintf(os.Stderr, "error: no remote address specified\n")
return 1
}
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
return 1
}
switch flag.Args()[0] {
case "createaction":
fsCreateAction.Parse(flag.Args()[1:])
IDCreateAction := *flagIDCreateAction
NameCreateAction := *flagNameCreateAction
UserIDCreateAction := *flagUserIDCreateAction
request, err := handlers.CreateAction(IDCreateAction, NameCreateAction, UserIDCreateAction)
if err != nil {
fmt.Fprintf(os.Stderr, "Error calling handlers.CreateAction: %v\n", err)
return 1
}
v, err := service.CreateAction(context.Background(), request)
if err != nil {
fmt.Fprintf(os.Stderr, "Error calling service.CreateAction: %v\n", err)
return 1
}
fmt.Println("Client Requested with:")
fmt.Println(IDCreateAction, NameCreateAction, UserIDCreateAction)
fmt.Println("Server Responded with:")
fmt.Println(v)
case "createoccurrence":
fsCreateOccurrence.Parse(flag.Args()[1:])
UserIDCreateOccurrence := *flagUserIDCreateOccurrence
var OccurrenceCreateOccurrence pb.Occurrence
if flagOccurrenceCreateOccurrence != nil && len(*flagOccurrenceCreateOccurrence) > 0 {
err = json.Unmarshal([]byte(*flagOccurrenceCreateOccurrence), &OccurrenceCreateOccurrence)
if err != nil {
panic(errors.Wrapf(err, "unmarshalling OccurrenceCreateOccurrence from %v:", flagOccurrenceCreateOccurrence))
}
}
request, err := handlers.CreateOccurrence(UserIDCreateOccurrence, OccurrenceCreateOccurrence)
if err != nil {
fmt.Fprintf(os.Stderr, "Error calling handlers.CreateOccurrence: %v\n", err)
return 1
}
v, err := service.CreateOccurrence(context.Background(), request)
if err != nil {
fmt.Fprintf(os.Stderr, "Error calling service.CreateOccurrence: %v\n", err)
return 1
}
fmt.Println("Client Requested with:")
fmt.Println(UserIDCreateOccurrence, OccurrenceCreateOccurrence)
fmt.Println("Server Responded with:")
fmt.Println(v)
case "readaction":
fsReadAction.Parse(flag.Args()[1:])
IDReadAction := *flagIDReadAction
NameReadAction := *flagNameReadAction
UserIDReadAction := *flagUserIDReadAction
request, err := handlers.ReadAction(IDReadAction, NameReadAction, UserIDReadAction)
if err != nil {
fmt.Fprintf(os.Stderr, "Error calling handlers.ReadAction: %v\n", err)
return 1
}
v, err := service.ReadAction(context.Background(), request)
if err != nil {
fmt.Fprintf(os.Stderr, "Error calling service.ReadAction: %v\n", err)
return 1
}
fmt.Println("Client Requested with:")
fmt.Println(IDReadAction, NameReadAction, UserIDReadAction)
fmt.Println("Server Responded with:")
fmt.Println(v)
case "readactions":
fsReadActions.Parse(flag.Args()[1:])
UserIDReadActions := *flagUserIDReadActions
request, err := handlers.ReadActions(UserIDReadActions)
if err != nil {
fmt.Fprintf(os.Stderr, "Error calling handlers.ReadActions: %v\n", err)
return 1
}
v, err := service.ReadActions(context.Background(), request)
if err != nil {
fmt.Fprintf(os.Stderr, "Error calling service.ReadActions: %v\n", err)
return 1
}
fmt.Println("Client Requested with:")
fmt.Println(UserIDReadActions)
fmt.Println("Server Responded with:")
fmt.Println(v)
case "readoccurrences":
fsReadOccurrences.Parse(flag.Args()[1:])
IDReadOccurrences := *flagIDReadOccurrences
NameReadOccurrences := *flagNameReadOccurrences
UserIDReadOccurrences := *flagUserIDReadOccurrences
request, err := handlers.ReadOccurrences(IDReadOccurrences, NameReadOccurrences, UserIDReadOccurrences)
if err != nil {
fmt.Fprintf(os.Stderr, "Error calling handlers.ReadOccurrences: %v\n", err)
return 1
}
v, err := service.ReadOccurrences(context.Background(), request)
if err != nil {
fmt.Fprintf(os.Stderr, "Error calling service.ReadOccurrences: %v\n", err)
return 1
}
fmt.Println("Client Requested with:")
fmt.Println(IDReadOccurrences, NameReadOccurrences, UserIDReadOccurrences)
fmt.Println("Server Responded with:")
fmt.Println(v)
case "readoccurrencesbydate":
fsReadOccurrencesByDate.Parse(flag.Args()[1:])
ActionIDReadOccurrencesByDate := *flagActionIDReadOccurrencesByDate
StartDateReadOccurrencesByDate := *flagStartDateReadOccurrencesByDate
EndDateReadOccurrencesByDate := *flagEndDateReadOccurrencesByDate
request, err := handlers.ReadOccurrencesByDate(ActionIDReadOccurrencesByDate, StartDateReadOccurrencesByDate, EndDateReadOccurrencesByDate)
if err != nil {
fmt.Fprintf(os.Stderr, "Error calling handlers.ReadOccurrencesByDate: %v\n", err)
return 1
}
v, err := service.ReadOccurrencesByDate(context.Background(), request)
if err != nil {
fmt.Fprintf(os.Stderr, "Error calling service.ReadOccurrencesByDate: %v\n", err)
return 1
}
fmt.Println("Client Requested with:")
fmt.Println(ActionIDReadOccurrencesByDate, StartDateReadOccurrencesByDate, EndDateReadOccurrencesByDate)
fmt.Println("Server Responded with:")
fmt.Println(v)
default:
flag.Usage()
return 1
}
return 0
}