Skip to content

Commit 76250ca

Browse files
committed
litcli: use cancellable contexts
In this commit, we use the signal.Interceptor to cancel the contexts we use for our CLI calls.
1 parent 0407505 commit 76250ca

File tree

9 files changed

+236
-218
lines changed

9 files changed

+236
-218
lines changed

cmd/litcli/accounts.go

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"context"
54
"encoding/hex"
65
"fmt"
76
"os"
@@ -75,9 +74,9 @@ spend that amount.`,
7574
Action: createAccount,
7675
}
7776

78-
func createAccount(ctx *cli.Context) error {
79-
ctxb := context.Background()
80-
clientConn, cleanup, err := connectClient(ctx, false)
77+
func createAccount(cli *cli.Context) error {
78+
ctx := getContext()
79+
clientConn, cleanup, err := connectClient(cli, false)
8180
if err != nil {
8281
return err
8382
}
@@ -88,11 +87,11 @@ func createAccount(ctx *cli.Context) error {
8887
initialBalance uint64
8988
expirationDate int64
9089
)
91-
args := ctx.Args()
90+
args := cli.Args()
9291

9392
switch {
94-
case ctx.IsSet("balance"):
95-
initialBalance = ctx.Uint64("balance")
93+
case cli.IsSet("balance"):
94+
initialBalance = cli.Uint64("balance")
9695
case args.Present():
9796
initialBalance, err = strconv.ParseUint(args.First(), 10, 64)
9897
if err != nil {
@@ -102,8 +101,8 @@ func createAccount(ctx *cli.Context) error {
102101
}
103102

104103
switch {
105-
case ctx.IsSet("expiration_date"):
106-
expirationDate = ctx.Int64("expiration_date")
104+
case cli.IsSet("expiration_date"):
105+
expirationDate = cli.Int64("expiration_date")
107106
case args.Present():
108107
expirationDate, err = strconv.ParseInt(args.First(), 10, 64)
109108
if err != nil {
@@ -117,9 +116,9 @@ func createAccount(ctx *cli.Context) error {
117116
req := &litrpc.CreateAccountRequest{
118117
AccountBalance: initialBalance,
119118
ExpirationDate: expirationDate,
120-
Label: ctx.String(labelName),
119+
Label: cli.String(labelName),
121120
}
122-
resp, err := client.CreateAccount(ctxb, req)
121+
resp, err := client.CreateAccount(ctx, req)
123122
if err != nil {
124123
return err
125124
}
@@ -128,8 +127,8 @@ func createAccount(ctx *cli.Context) error {
128127

129128
// User requested to store the newly baked account macaroon to a file
130129
// in addition to printing it to the console.
131-
if ctx.IsSet("save_to") {
132-
fileName := lncfg.CleanAndExpandPath(ctx.String("save_to"))
130+
if cli.IsSet("save_to") {
131+
fileName := lncfg.CleanAndExpandPath(cli.String("save_to"))
133132
err := os.WriteFile(fileName, resp.Macaroon, 0644)
134133
if err != nil {
135134
return fmt.Errorf("error writing account macaroon "+
@@ -176,16 +175,16 @@ var updateAccountCommand = cli.Command{
176175
Action: updateAccount,
177176
}
178177

179-
func updateAccount(ctx *cli.Context) error {
180-
ctxb := context.Background()
181-
clientConn, cleanup, err := connectClient(ctx, false)
178+
func updateAccount(cli *cli.Context) error {
179+
ctx := getContext()
180+
clientConn, cleanup, err := connectClient(cli, false)
182181
if err != nil {
183182
return err
184183
}
185184
defer cleanup()
186185
client := litrpc.NewAccountsClient(clientConn)
187186

188-
id, label, args, err := parseIDOrLabel(ctx)
187+
id, label, args, err := parseIDOrLabel(cli)
189188
if err != nil {
190189
return err
191190
}
@@ -195,8 +194,8 @@ func updateAccount(ctx *cli.Context) error {
195194
expirationDate int64
196195
)
197196
switch {
198-
case ctx.IsSet("new_balance"):
199-
newBalance = ctx.Int64("new_balance")
197+
case cli.IsSet("new_balance"):
198+
newBalance = cli.Int64("new_balance")
200199
case args.Present():
201200
newBalance, err = strconv.ParseInt(args.First(), 10, 64)
202201
if err != nil {
@@ -206,8 +205,8 @@ func updateAccount(ctx *cli.Context) error {
206205
}
207206

208207
switch {
209-
case ctx.IsSet("new_expiration_date"):
210-
expirationDate = ctx.Int64("new_expiration_date")
208+
case cli.IsSet("new_expiration_date"):
209+
expirationDate = cli.Int64("new_expiration_date")
211210
case args.Present():
212211
expirationDate, err = strconv.ParseInt(args.First(), 10, 64)
213212
if err != nil {
@@ -224,7 +223,7 @@ func updateAccount(ctx *cli.Context) error {
224223
AccountBalance: newBalance,
225224
ExpirationDate: expirationDate,
226225
}
227-
resp, err := client.UpdateAccount(ctxb, req)
226+
resp, err := client.UpdateAccount(ctx, req)
228227
if err != nil {
229228
return err
230229
}
@@ -242,17 +241,17 @@ var listAccountsCommand = cli.Command{
242241
Action: listAccounts,
243242
}
244243

245-
func listAccounts(ctx *cli.Context) error {
246-
ctxb := context.Background()
247-
clientConn, cleanup, err := connectClient(ctx, false)
244+
func listAccounts(cli *cli.Context) error {
245+
ctx := getContext()
246+
clientConn, cleanup, err := connectClient(cli, false)
248247
if err != nil {
249248
return err
250249
}
251250
defer cleanup()
252251
client := litrpc.NewAccountsClient(clientConn)
253252

254253
req := &litrpc.ListAccountsRequest{}
255-
resp, err := client.ListAccounts(ctxb, req)
254+
resp, err := client.ListAccounts(ctx, req)
256255
if err != nil {
257256
return err
258257
}
@@ -281,16 +280,16 @@ var accountInfoCommand = cli.Command{
281280
Action: accountInfo,
282281
}
283282

284-
func accountInfo(ctx *cli.Context) error {
285-
ctxb := context.Background()
286-
clientConn, cleanup, err := connectClient(ctx, false)
283+
func accountInfo(cli *cli.Context) error {
284+
ctx := getContext()
285+
clientConn, cleanup, err := connectClient(cli, false)
287286
if err != nil {
288287
return err
289288
}
290289
defer cleanup()
291290
client := litrpc.NewAccountsClient(clientConn)
292291

293-
id, label, _, err := parseIDOrLabel(ctx)
292+
id, label, _, err := parseIDOrLabel(cli)
294293
if err != nil {
295294
return err
296295
}
@@ -299,7 +298,7 @@ func accountInfo(ctx *cli.Context) error {
299298
Id: id,
300299
Label: label,
301300
}
302-
resp, err := client.AccountInfo(ctxb, req)
301+
resp, err := client.AccountInfo(ctx, req)
303302
if err != nil {
304303
return err
305304
}
@@ -327,16 +326,16 @@ var removeAccountCommand = cli.Command{
327326
Action: removeAccount,
328327
}
329328

330-
func removeAccount(ctx *cli.Context) error {
331-
ctxb := context.Background()
332-
clientConn, cleanup, err := connectClient(ctx, false)
329+
func removeAccount(cli *cli.Context) error {
330+
ctx := getContext()
331+
clientConn, cleanup, err := connectClient(cli, false)
333332
if err != nil {
334333
return err
335334
}
336335
defer cleanup()
337336
client := litrpc.NewAccountsClient(clientConn)
338337

339-
id, label, _, err := parseIDOrLabel(ctx)
338+
id, label, _, err := parseIDOrLabel(cli)
340339
if err != nil {
341340
return err
342341
}
@@ -345,7 +344,7 @@ func removeAccount(ctx *cli.Context) error {
345344
Id: id,
346345
Label: label,
347346
}
348-
_, err = client.RemoveAccount(ctxb, req)
347+
_, err = client.RemoveAccount(ctx, req)
349348
return err
350349
}
351350

cmd/litcli/actions.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"context"
54
"encoding/hex"
65
"fmt"
76

@@ -98,49 +97,49 @@ var listActionsCommand = cli.Command{
9897
},
9998
}
10099

101-
func listActions(ctx *cli.Context) error {
102-
ctxb := context.Background()
103-
clientConn, cleanup, err := connectClient(ctx, false)
100+
func listActions(cli *cli.Context) error {
101+
ctx := getContext()
102+
clientConn, cleanup, err := connectClient(cli, false)
104103
if err != nil {
105104
return err
106105
}
107106
defer cleanup()
108107
client := litrpc.NewFirewallClient(clientConn)
109108

110-
state, err := parseActionState(ctx.String("state"))
109+
state, err := parseActionState(cli.String("state"))
111110
if err != nil {
112111
return err
113112
}
114113

115114
var sessionID []byte
116-
if ctx.String("session_id") != "" {
117-
sessionID, err = hex.DecodeString(ctx.String("session_id"))
115+
if cli.String("session_id") != "" {
116+
sessionID, err = hex.DecodeString(cli.String("session_id"))
118117
if err != nil {
119118
return err
120119
}
121120
}
122121

123122
var groupID []byte
124-
if ctx.String("group_id") != "" {
125-
groupID, err = hex.DecodeString(ctx.String("group_id"))
123+
if cli.String("group_id") != "" {
124+
groupID, err = hex.DecodeString(cli.String("group_id"))
126125
if err != nil {
127126
return err
128127
}
129128
}
130129

131130
resp, err := client.ListActions(
132-
ctxb, &litrpc.ListActionsRequest{
131+
ctx, &litrpc.ListActionsRequest{
133132
SessionId: sessionID,
134-
FeatureName: ctx.String("feature"),
135-
ActorName: ctx.String("actor"),
136-
MethodName: ctx.String("method"),
133+
FeatureName: cli.String("feature"),
134+
ActorName: cli.String("actor"),
135+
MethodName: cli.String("method"),
137136
State: state,
138-
IndexOffset: ctx.Uint64("index_offset"),
139-
MaxNumActions: ctx.Uint64("max_num_actions"),
140-
Reversed: !ctx.Bool("oldest_first"),
141-
CountTotal: ctx.Bool("count_total"),
142-
StartTimestamp: ctx.Uint64("start_timestamp"),
143-
EndTimestamp: ctx.Uint64("end_timestamp"),
137+
IndexOffset: cli.Uint64("index_offset"),
138+
MaxNumActions: cli.Uint64("max_num_actions"),
139+
Reversed: !cli.Bool("oldest_first"),
140+
CountTotal: cli.Bool("count_total"),
141+
StartTimestamp: cli.Uint64("start_timestamp"),
142+
EndTimestamp: cli.Uint64("end_timestamp"),
144143
GroupId: groupID,
145144
},
146145
)

0 commit comments

Comments
 (0)