Skip to content

Commit

Permalink
Fix handling of context
Browse files Browse the repository at this point in the history
  • Loading branch information
spiegel-im-spiegel committed May 12, 2023
1 parent bd93c67 commit c2ee480
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 132 deletions.
Binary file modified dependency.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
94 changes: 0 additions & 94 deletions facade/completion.go

This file was deleted.

16 changes: 11 additions & 5 deletions facade/facade.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package facade

import (
cntxt "context"
"io"
"os"
"os/signal"
"runtime"
"strings"

Expand Down Expand Up @@ -34,7 +36,7 @@ var (
filePath string
)

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

rootCmd.SilenceUsage = true
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.SetArgs(args)
rootCmd.SetIn(ui.Reader()) //Stdin
rootCmd.SetOut(ui.ErrorWriter()) //Stdout -> Stderr
Expand All @@ -113,7 +116,6 @@ func newRootCmd(ui *rwi.RWI, args []string) *cobra.Command {
newHkpCmd(ui),
newGitHubCmd(ui),
newFetchCmd(ui),
newCompletionCmd(ui, rootCmd),
)

return rootCmd
Expand Down Expand Up @@ -154,7 +156,7 @@ func parseContext(cmd *cobra.Command) *context.Context {
return cxt
}

//Execute is called from main function
// Execute is called from main function
func Execute(ui *rwi.RWI, args []string) (exit exitcode.ExitCode) {
defer func() {
//panic hundling
Expand All @@ -171,15 +173,19 @@ func Execute(ui *rwi.RWI, args []string) (exit exitcode.ExitCode) {
}
}()

// create interrupt SIGNAL
ctx, cancel := signal.NotifyContext(cntxt.Background(), os.Interrupt)
defer cancel()

//execution
exit = exitcode.Normal
if err := newRootCmd(ui, args).Execute(); err != nil {
if err := newRootCmd(ui, args).ExecuteContext(ctx); err != nil {
exit = exitcode.Abnormal
}
return
}

/* Copyright 2017-2021 Spiegel
/* Copyright 2017-2023 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
10 changes: 4 additions & 6 deletions facade/fetch.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package facade

import (
"context"
"os"

"github.com/goark/errs"

"github.com/goark/fetch"
"github.com/goark/gocli/rwi"
"github.com/goark/gocli/signal"
"github.com/goark/gpgpdump/parse"
"github.com/spf13/cobra"
)

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

//Fetch OpenPGP packets
resp, err := fetch.New().Get(
resp, err := fetch.New().GetWithContext(
cmd.Context(),
u,
fetch.WithContext(signal.Context(context.Background(), os.Interrupt)),
)
if err != nil {
return debugPrint(ui, err)
Expand Down Expand Up @@ -71,7 +69,7 @@ func newFetchCmd(ui *rwi.RWI) *cobra.Command {
return fetchCmd
}

/* Copyright 2019-2021 Spiegel
/* Copyright 2019-2023 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
8 changes: 3 additions & 5 deletions facade/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ package facade

import (
"bytes"
"context"
"os"

"github.com/goark/errs"

"github.com/goark/fetch"
"github.com/goark/gocli/rwi"
"github.com/goark/gocli/signal"
"github.com/goark/gpgpdump/github"
"github.com/goark/gpgpdump/parse"
contxt "github.com/goark/gpgpdump/parse/context"
"github.com/spf13/cobra"
)

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

//Fetch OpenPGP packets
resp, err := github.GetKey(
signal.Context(context.Background(), os.Interrupt),
cmd.Context(),
fetch.New(),
userID,
keyid,
Expand Down Expand Up @@ -78,7 +76,7 @@ func newGitHubCmd(ui *rwi.RWI) *cobra.Command {
return githubCmd
}

/* Copyright 2019-2021 Spiegel
/* Copyright 2019-2023 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
8 changes: 3 additions & 5 deletions facade/hkp.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package facade

import (
"context"
"os"

"github.com/goark/errs"

"github.com/goark/fetch"
"github.com/goark/gocli/rwi"
"github.com/goark/gocli/signal"
"github.com/goark/gpgpdump/ecode"
"github.com/goark/gpgpdump/hkp"
"github.com/goark/gpgpdump/parse"
contxt "github.com/goark/gpgpdump/parse/context"
"github.com/spf13/cobra"
)

//newHkpCmd returns cobra.Command instance for show sub-command
// newHkpCmd returns cobra.Command instance for show sub-command
func newHkpCmd(ui *rwi.RWI) *cobra.Command {
hkpCmd := &cobra.Command{
Use: "hkp [flags] {userID | keyID}",
Expand Down Expand Up @@ -63,7 +61,7 @@ func newHkpCmd(ui *rwi.RWI) *cobra.Command {
hkp.WithProtocol(prt),
hkp.WithPort(port),
).Fetch(
signal.Context(context.Background(), os.Interrupt),
cmd.Context(),
fetch.New(),
userID,
)
Expand Down Expand Up @@ -99,7 +97,7 @@ func newHkpCmd(ui *rwi.RWI) *cobra.Command {
return hkpCmd
}

/* Copyright 2019-2021 Spiegel
/* Copyright 2019-2023 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
12 changes: 6 additions & 6 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const maxKeys = 100

var maxKeysStr = strconv.Itoa(maxKeys)

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

//GetKey returns JSON text for GitHub OpenPGP API
// GetKey returns JSON text for GitHub OpenPGP API
func GetKey(ctx context.Context, cli fetch.Client, username string, keyID string) ([]byte, error) {
if len(keyID) == 0 {
return Get(ctx, cli, username)
Expand Down Expand Up @@ -66,9 +66,9 @@ func getInPage(ctx context.Context, cli fetch.Client, username string, page int)
if err != nil {
return nil, errs.Wrap(ecode.ErrInvalidRequest, errs.WithCause(err), errs.WithContext("username", username), errs.WithContext("page", page))
}
resp, err := cli.Get(
resp, err := cli.GetWithContext(
ctx,
u,
fetch.WithContext(ctx),
fetch.WithRequestHeaderSet("Accept", "application/vnd.github.v3+json"),
)
if err != nil {
Expand Down Expand Up @@ -98,7 +98,7 @@ func makeURLAPI(username string, page int) (*url.URL, error) {
return u, nil
}

/* Copyright 2020-2021 Spiegel
/* Copyright 2020-2023 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
6 changes: 6 additions & 0 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,15 @@ func newtestClient2() *testClient {
func (c *testClient) Get(u *url.URL, opts ...fetch.RequestOpts) (fetch.Response, error) {
return c.resp, nil
}
func (c *testClient) GetWithContext(ctx context.Context, u *url.URL, opts ...fetch.RequestOpts) (fetch.Response, error) {
return c.resp, nil
}
func (c *testClient) Post(u *url.URL, payload io.Reader, opts ...fetch.RequestOpts) (fetch.Response, error) {
return c.resp, nil
}
func (c *testClient) PostWithContext(ctx context.Context, u *url.URL, payload io.Reader, opts ...fetch.RequestOpts) (fetch.Response, error) {
return c.resp, nil
}

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

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/ProtonMail/go-crypto v0.0.0-20230426101702-58e86b294756
github.com/atotto/clipboard v0.1.4
github.com/goark/errs v1.2.2
github.com/goark/fetch v0.3.0
github.com/goark/fetch v0.4.1
github.com/goark/gocli v0.13.0
github.com/spf13/cobra v1.7.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUK
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/goark/errs v1.2.2 h1:UrMZZJL0WaOzaO+ErSV+nz/k/+bmW2wUiFe5V7pUeEo=
github.com/goark/errs v1.2.2/go.mod h1:ZsQucxaDFVfSB8I99j4bxkDRfNOrlKINwg72QMuRWKw=
github.com/goark/fetch v0.3.0 h1:2m32EGOLBi99RzI5urFfmv5++CMqfenVw7NH8z/lbX8=
github.com/goark/fetch v0.3.0/go.mod h1:sqDdPbbHeIjDVeHrgvzhHpkUr8X9pVC9DgJoVwU02x0=
github.com/goark/fetch v0.4.1 h1:Y59g9sAdgqjPS7UADdLIoQGRxJE1WMo5ixlJi/ZcCfc=
github.com/goark/fetch v0.4.1/go.mod h1:umZxLIJHa15J8EQSp5zft1dHDv0VGwmQL6pOfaJ60FY=
github.com/goark/gocli v0.13.0 h1:hR/5E4JGMEcbQxkSqR7K/0XnYY2Hd6GDpuazXGC3jn4=
github.com/goark/gocli v0.13.0/go.mod h1:pFYWXAXZ5G4QqPcXsDTSFbCuVg0qO40NYkp2XKthc18=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down
6 changes: 6 additions & 0 deletions hkp/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,15 @@ var _ fetch.Client = (*testClient)(nil)
func (c *testClient) Get(u *url.URL, opts ...fetch.RequestOpts) (fetch.Response, error) {
return newtestResponse(), nil
}
func (c *testClient) GetWithContext(ctx context.Context, u *url.URL, opts ...fetch.RequestOpts) (fetch.Response, error) {
return newtestResponse(), nil
}
func (c *testClient) Post(u *url.URL, payload io.Reader, opts ...fetch.RequestOpts) (fetch.Response, error) {
return newtestResponse(), nil
}
func (c *testClient) PostWithContext(ctx context.Context, u *url.URL, payload io.Reader, opts ...fetch.RequestOpts) (fetch.Response, error) {
return newtestResponse(), nil
}

func TestFetch(t *testing.T) {
testCases := []struct {
Expand Down
Loading

0 comments on commit c2ee480

Please sign in to comment.