Skip to content

Commit c2ee480

Browse files
Fix handling of context
1 parent bd93c67 commit c2ee480

File tree

12 files changed

+50
-132
lines changed

12 files changed

+50
-132
lines changed

dependency.png

122 Bytes
Loading

facade/completion.go

Lines changed: 0 additions & 94 deletions
This file was deleted.

facade/facade.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package facade
22

33
import (
4+
cntxt "context"
45
"io"
56
"os"
7+
"os/signal"
68
"runtime"
79
"strings"
810

@@ -34,7 +36,7 @@ var (
3436
filePath string
3537
)
3638

37-
//newRootCmd returns cobra.Command instance for root command
39+
// newRootCmd returns cobra.Command instance for root command
3840
func newRootCmd(ui *rwi.RWI, args []string) *cobra.Command {
3941
rootCmd := &cobra.Command{
4042
Use: Name,
@@ -104,6 +106,7 @@ func newRootCmd(ui *rwi.RWI, args []string) *cobra.Command {
104106
rootCmd.PersistentFlags().BoolP(context.UTC.String(), "u", false, "output with UTC time")
105107

106108
rootCmd.SilenceUsage = true
109+
rootCmd.CompletionOptions.DisableDefaultCmd = true
107110
rootCmd.SetArgs(args)
108111
rootCmd.SetIn(ui.Reader()) //Stdin
109112
rootCmd.SetOut(ui.ErrorWriter()) //Stdout -> Stderr
@@ -113,7 +116,6 @@ func newRootCmd(ui *rwi.RWI, args []string) *cobra.Command {
113116
newHkpCmd(ui),
114117
newGitHubCmd(ui),
115118
newFetchCmd(ui),
116-
newCompletionCmd(ui, rootCmd),
117119
)
118120

119121
return rootCmd
@@ -154,7 +156,7 @@ func parseContext(cmd *cobra.Command) *context.Context {
154156
return cxt
155157
}
156158

157-
//Execute is called from main function
159+
// Execute is called from main function
158160
func Execute(ui *rwi.RWI, args []string) (exit exitcode.ExitCode) {
159161
defer func() {
160162
//panic hundling
@@ -171,15 +173,19 @@ func Execute(ui *rwi.RWI, args []string) (exit exitcode.ExitCode) {
171173
}
172174
}()
173175

176+
// create interrupt SIGNAL
177+
ctx, cancel := signal.NotifyContext(cntxt.Background(), os.Interrupt)
178+
defer cancel()
179+
174180
//execution
175181
exit = exitcode.Normal
176-
if err := newRootCmd(ui, args).Execute(); err != nil {
182+
if err := newRootCmd(ui, args).ExecuteContext(ctx); err != nil {
177183
exit = exitcode.Abnormal
178184
}
179185
return
180186
}
181187

182-
/* Copyright 2017-2021 Spiegel
188+
/* Copyright 2017-2023 Spiegel
183189
*
184190
* Licensed under the Apache License, Version 2.0 (the "License");
185191
* you may not use this file except in compliance with the License.

facade/fetch.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package facade
22

33
import (
4-
"context"
54
"os"
65

76
"github.com/goark/errs"
87

98
"github.com/goark/fetch"
109
"github.com/goark/gocli/rwi"
11-
"github.com/goark/gocli/signal"
1210
"github.com/goark/gpgpdump/parse"
1311
"github.com/spf13/cobra"
1412
)
1513

16-
//newHkpCmd returns cobra.Command instance for show sub-command
14+
// newHkpCmd returns cobra.Command instance for show sub-command
1715
func newFetchCmd(ui *rwi.RWI) *cobra.Command {
1816
fetchCmd := &cobra.Command{
1917
Use: "fetch [flags] URL",
@@ -38,9 +36,9 @@ func newFetchCmd(ui *rwi.RWI) *cobra.Command {
3836
}
3937

4038
//Fetch OpenPGP packets
41-
resp, err := fetch.New().Get(
39+
resp, err := fetch.New().GetWithContext(
40+
cmd.Context(),
4241
u,
43-
fetch.WithContext(signal.Context(context.Background(), os.Interrupt)),
4442
)
4543
if err != nil {
4644
return debugPrint(ui, err)
@@ -71,7 +69,7 @@ func newFetchCmd(ui *rwi.RWI) *cobra.Command {
7169
return fetchCmd
7270
}
7371

74-
/* Copyright 2019-2021 Spiegel
72+
/* Copyright 2019-2023 Spiegel
7573
*
7674
* Licensed under the Apache License, Version 2.0 (the "License");
7775
* you may not use this file except in compliance with the License.

facade/github.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ package facade
22

33
import (
44
"bytes"
5-
"context"
65
"os"
76

87
"github.com/goark/errs"
98

109
"github.com/goark/fetch"
1110
"github.com/goark/gocli/rwi"
12-
"github.com/goark/gocli/signal"
1311
"github.com/goark/gpgpdump/github"
1412
"github.com/goark/gpgpdump/parse"
1513
contxt "github.com/goark/gpgpdump/parse/context"
1614
"github.com/spf13/cobra"
1715
)
1816

19-
//newHkpCmd returns cobra.Command instance for show sub-command
17+
// newHkpCmd returns cobra.Command instance for show sub-command
2018
func newGitHubCmd(ui *rwi.RWI) *cobra.Command {
2119
githubCmd := &cobra.Command{
2220
Use: "github [flags] GitHubUserID",
@@ -44,7 +42,7 @@ func newGitHubCmd(ui *rwi.RWI) *cobra.Command {
4442

4543
//Fetch OpenPGP packets
4644
resp, err := github.GetKey(
47-
signal.Context(context.Background(), os.Interrupt),
45+
cmd.Context(),
4846
fetch.New(),
4947
userID,
5048
keyid,
@@ -78,7 +76,7 @@ func newGitHubCmd(ui *rwi.RWI) *cobra.Command {
7876
return githubCmd
7977
}
8078

81-
/* Copyright 2019-2021 Spiegel
79+
/* Copyright 2019-2023 Spiegel
8280
*
8381
* Licensed under the Apache License, Version 2.0 (the "License");
8482
* you may not use this file except in compliance with the License.

facade/hkp.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
package facade
22

33
import (
4-
"context"
54
"os"
65

76
"github.com/goark/errs"
87

98
"github.com/goark/fetch"
109
"github.com/goark/gocli/rwi"
11-
"github.com/goark/gocli/signal"
1210
"github.com/goark/gpgpdump/ecode"
1311
"github.com/goark/gpgpdump/hkp"
1412
"github.com/goark/gpgpdump/parse"
1513
contxt "github.com/goark/gpgpdump/parse/context"
1614
"github.com/spf13/cobra"
1715
)
1816

19-
//newHkpCmd returns cobra.Command instance for show sub-command
17+
// newHkpCmd returns cobra.Command instance for show sub-command
2018
func newHkpCmd(ui *rwi.RWI) *cobra.Command {
2119
hkpCmd := &cobra.Command{
2220
Use: "hkp [flags] {userID | keyID}",
@@ -63,7 +61,7 @@ func newHkpCmd(ui *rwi.RWI) *cobra.Command {
6361
hkp.WithProtocol(prt),
6462
hkp.WithPort(port),
6563
).Fetch(
66-
signal.Context(context.Background(), os.Interrupt),
64+
cmd.Context(),
6765
fetch.New(),
6866
userID,
6967
)
@@ -99,7 +97,7 @@ func newHkpCmd(ui *rwi.RWI) *cobra.Command {
9997
return hkpCmd
10098
}
10199

102-
/* Copyright 2019-2021 Spiegel
100+
/* Copyright 2019-2023 Spiegel
103101
*
104102
* Licensed under the Apache License, Version 2.0 (the "License");
105103
* you may not use this file except in compliance with the License.

github/github.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ const maxKeys = 100
1818

1919
var maxKeysStr = strconv.Itoa(maxKeys)
2020

21-
//Get returns OpenPGP ASCII armor text from GitHub user profile
21+
// Get returns OpenPGP ASCII armor text from GitHub user profile
2222
func Get(ctx context.Context, cli fetch.Client, username string) ([]byte, error) {
23-
resp, err := cli.Get(makeURL(username), fetch.WithContext(ctx))
23+
resp, err := cli.GetWithContext(ctx, makeURL(username))
2424
if err != nil {
2525
return nil, errs.Wrap(err, errs.WithContext("username", username))
2626
}
@@ -36,7 +36,7 @@ func makeURL(username string) *url.URL {
3636
return u
3737
}
3838

39-
//GetKey returns JSON text for GitHub OpenPGP API
39+
// GetKey returns JSON text for GitHub OpenPGP API
4040
func GetKey(ctx context.Context, cli fetch.Client, username string, keyID string) ([]byte, error) {
4141
if len(keyID) == 0 {
4242
return Get(ctx, cli, username)
@@ -66,9 +66,9 @@ func getInPage(ctx context.Context, cli fetch.Client, username string, page int)
6666
if err != nil {
6767
return nil, errs.Wrap(ecode.ErrInvalidRequest, errs.WithCause(err), errs.WithContext("username", username), errs.WithContext("page", page))
6868
}
69-
resp, err := cli.Get(
69+
resp, err := cli.GetWithContext(
70+
ctx,
7071
u,
71-
fetch.WithContext(ctx),
7272
fetch.WithRequestHeaderSet("Accept", "application/vnd.github.v3+json"),
7373
)
7474
if err != nil {
@@ -98,7 +98,7 @@ func makeURLAPI(username string, page int) (*url.URL, error) {
9898
return u, nil
9999
}
100100

101-
/* Copyright 2020-2021 Spiegel
101+
/* Copyright 2020-2023 Spiegel
102102
*
103103
* Licensed under the Apache License, Version 2.0 (the "License");
104104
* you may not use this file except in compliance with the License.

github/github_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,15 @@ func newtestClient2() *testClient {
144144
func (c *testClient) Get(u *url.URL, opts ...fetch.RequestOpts) (fetch.Response, error) {
145145
return c.resp, nil
146146
}
147+
func (c *testClient) GetWithContext(ctx context.Context, u *url.URL, opts ...fetch.RequestOpts) (fetch.Response, error) {
148+
return c.resp, nil
149+
}
147150
func (c *testClient) Post(u *url.URL, payload io.Reader, opts ...fetch.RequestOpts) (fetch.Response, error) {
148151
return c.resp, nil
149152
}
153+
func (c *testClient) PostWithContext(ctx context.Context, u *url.URL, payload io.Reader, opts ...fetch.RequestOpts) (fetch.Response, error) {
154+
return c.resp, nil
155+
}
150156

151157
var _ fetch.Client = (*testClient)(nil)
152158

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/ProtonMail/go-crypto v0.0.0-20230426101702-58e86b294756
77
github.com/atotto/clipboard v0.1.4
88
github.com/goark/errs v1.2.2
9-
github.com/goark/fetch v0.3.0
9+
github.com/goark/fetch v0.4.1
1010
github.com/goark/gocli v0.13.0
1111
github.com/spf13/cobra v1.7.0
1212
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUK
99
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
1010
github.com/goark/errs v1.2.2 h1:UrMZZJL0WaOzaO+ErSV+nz/k/+bmW2wUiFe5V7pUeEo=
1111
github.com/goark/errs v1.2.2/go.mod h1:ZsQucxaDFVfSB8I99j4bxkDRfNOrlKINwg72QMuRWKw=
12-
github.com/goark/fetch v0.3.0 h1:2m32EGOLBi99RzI5urFfmv5++CMqfenVw7NH8z/lbX8=
13-
github.com/goark/fetch v0.3.0/go.mod h1:sqDdPbbHeIjDVeHrgvzhHpkUr8X9pVC9DgJoVwU02x0=
12+
github.com/goark/fetch v0.4.1 h1:Y59g9sAdgqjPS7UADdLIoQGRxJE1WMo5ixlJi/ZcCfc=
13+
github.com/goark/fetch v0.4.1/go.mod h1:umZxLIJHa15J8EQSp5zft1dHDv0VGwmQL6pOfaJ60FY=
1414
github.com/goark/gocli v0.13.0 h1:hR/5E4JGMEcbQxkSqR7K/0XnYY2Hd6GDpuazXGC3jn4=
1515
github.com/goark/gocli v0.13.0/go.mod h1:pFYWXAXZ5G4QqPcXsDTSFbCuVg0qO40NYkp2XKthc18=
1616
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=

0 commit comments

Comments
 (0)