-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
245 lines (205 loc) · 6.22 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
package main
import (
"flag"
"fmt"
"io"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/antklim/go-invoice/cli"
"github.com/antklim/go-invoice/invoice"
"github.com/antklim/go-invoice/storage"
)
var (
storageType string
tableName string
awsEndpoint string
)
func initFlags() {
flag.StringVar(&storageType, "storage", "memory", "Storage to where to save invoices [memory|dynamo]")
flag.StringVar(&tableName, "table", "invoices", "Storage table name")
flag.StringVar(&awsEndpoint, "endpoint", "", "Custom AWS endpoint to connect to DynamoDB")
flag.Parse()
}
func initCli(exit chan<- struct{}, svc *invoice.Service) *cli.Cli {
if exit == nil {
panic("cli: nil exit channel")
}
if svc == nil {
panic("cli: nil invoice service")
}
c := cli.NewCli(os.Stdin, os.Stdout, exit)
c.Handle("create", "Create new invoice", createHandler(svc))
c.Handle("view", "View invoice.", viewHandler(svc))
c.Handle("issue", "Issue invoice.", issueHandler(svc))
c.Handle("pay", "Pay invoice.", payHandler(svc))
c.Handle("cancel", "Cancel invoice.", cancelHandler(svc))
c.Handle("add-item", "Add invoice item.", addItemHandler(svc))
c.Handle("delete-item", "Delete invoice item.", deleteItemHandler(svc))
c.Handle("update-customer", "Update invoice customer.", updateCustomerHandler(svc))
return c
}
func initService() *invoice.Service {
var f invoice.StorageFactory
switch storageType {
case "memory":
f = new(storage.Memory)
case "dynamo":
f = storage.NewDynamo(tableName, storage.WithEndpoint(awsEndpoint))
default:
panic("svc: unknown storage " + storageType)
}
strg := f.MakeStorage()
return invoice.New(strg)
}
func main() {
initFlags()
fmt.Println("Welcome to go-invoice.")
exit := make(chan struct{}, 1)
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, syscall.SIGINT, syscall.SIGTERM)
svc := initService()
c := initCli(exit, svc)
go c.Run()
select {
case <-osSignals:
fmt.Println()
case <-exit:
}
fmt.Println("\nBye!")
}
func createHandler(svc *invoice.Service) cli.RunnerFunc {
return func(out io.Writer, args ...string) {
if len(args) == 0 || args[0] == "" {
fmt.Fprint(out, "create invoice failed: missing customer name\n")
return
}
inv, err := svc.CreateInvoice(strings.TrimSpace(args[0]))
if err != nil {
fmt.Fprintf(out, "create invoice failed: %v\n", err)
return
}
fmt.Fprintf(out, "%q invoice successfully created\n", inv.ID)
}
}
func viewHandler(svc *invoice.Service) cli.RunnerFunc {
return func(out io.Writer, args ...string) {
if len(args) == 0 || args[0] == "" {
fmt.Fprint(out, "view invoice failed: missing invoice ID\n")
return
}
invID := strings.TrimSpace(args[0])
inv, err := svc.ViewInvoice(invID)
if err != nil {
fmt.Fprintf(out, "view invoice failed: %v\n", err)
return
}
if inv == nil {
fmt.Fprintf(out, "%q invoice not found\n", invID)
return
}
fmt.Fprintf(out, "%#v\n", inv)
}
}
func issueHandler(svc *invoice.Service) cli.RunnerFunc {
return func(out io.Writer, args ...string) {
if len(args) == 0 || args[0] == "" {
fmt.Fprint(out, "issue invoice failed: missing invoice ID\n")
return
}
invID := strings.TrimSpace(args[0])
err := svc.IssueInvoice(invID)
if err != nil {
fmt.Fprintf(out, "issue invoice failed: %v\n", err)
return
}
fmt.Fprintf(out, "%q invoice successfully issued\n", invID)
}
}
func payHandler(svc *invoice.Service) cli.RunnerFunc {
return func(out io.Writer, args ...string) {
if len(args) == 0 || args[0] == "" {
fmt.Fprint(out, "pay invoice failed: missing invoice ID\n")
return
}
invID := strings.TrimSpace(args[0])
err := svc.PayInvoice(invID)
if err != nil {
fmt.Fprintf(out, "pay invoice failed: %v\n", err)
return
}
fmt.Fprintf(out, "%q invoice successfully paid\n", invID)
}
}
func cancelHandler(svc *invoice.Service) cli.RunnerFunc {
return func(out io.Writer, args ...string) {
if len(args) == 0 || args[0] == "" {
fmt.Fprint(out, "cancel invoice failed: missing invoice ID\n")
return
}
invID := strings.TrimSpace(args[0])
err := svc.CancelInvoice(invID)
if err != nil {
fmt.Fprintf(out, "cancel invoice failed: %v\n", err)
return
}
fmt.Fprintf(out, "%q invoice successfully canceled\n", invID)
}
}
func addItemHandler(svc *invoice.Service) cli.RunnerFunc {
return func(out io.Writer, args ...string) {
if len(args) < 4 || args[0] == "" || args[1] == "" || args[2] == "" || args[3] == "" {
fmt.Fprint(out, "add invoice item failed: missing arguments\n")
return
}
invID, productName := strings.TrimSpace(args[0]), strings.TrimSpace(args[1])
price, err := strconv.Atoi(strings.TrimSpace(args[2]))
if err != nil {
fmt.Fprintf(out, "add invoice item failed: invalid price argument: %v\n", err)
return
}
qty, err := strconv.Atoi(strings.TrimSpace(args[3]))
if err != nil {
fmt.Fprintf(out, "add invoice item failed: invalid qty argument: %v\n", err)
return
}
item, err := svc.AddInvoiceItem(invID, productName, price, qty)
if err != nil {
fmt.Fprintf(out, "add invoice item failed: %v\n", err)
return
}
fmt.Fprintf(out, "item %q successfully added to invoice %q\n", item.ID, invID)
}
}
func deleteItemHandler(svc *invoice.Service) cli.RunnerFunc {
return func(out io.Writer, args ...string) {
if len(args) < 2 || args[0] == "" || args[1] == "" {
fmt.Fprint(out, "delete invoice item failed: missing arguments\n")
return
}
invID, itemID := strings.TrimSpace(args[0]), strings.TrimSpace(args[1])
err := svc.DeleteInvoiceItem(invID, itemID)
if err != nil {
fmt.Fprintf(out, "delete invoice item failed: %v\n", err)
return
}
fmt.Fprintf(out, "item %q successfully deleted from invoice %q\n", itemID, invID)
}
}
func updateCustomerHandler(svc *invoice.Service) cli.RunnerFunc {
return func(out io.Writer, args ...string) {
if len(args) < 2 || args[0] == "" || args[1] == "" {
fmt.Fprint(out, "update invoice customer failed: missing invoice ID and/or customer name\n")
return
}
invID, name := strings.TrimSpace(args[0]), strings.TrimSpace(args[1])
err := svc.UpdateInvoiceCustomer(invID, name)
if err != nil {
fmt.Fprintf(out, "update invoice customer failed: %v\n", err)
return
}
fmt.Fprintf(out, "%q invoice customer successfully updated\n", invID)
}
}