Skip to content

Commit 84a8f80

Browse files
committed
Update deps and embed a standard Contxt in cmd.Context
1 parent b1cc80b commit 84a8f80

File tree

5 files changed

+103
-23
lines changed

5 files changed

+103
-23
lines changed

cmd.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package cmd
55

66
import (
77
"bytes"
8+
"context"
89
"errors"
910
"fmt"
1011
"io"
@@ -108,6 +109,7 @@ func (c *CommandBase) AllowInterspersedFlags() bool {
108109
// should interpret file names relative to Dir (see AbsPath below), and print
109110
// output and errors to Stdout and Stderr respectively.
110111
type Context struct {
112+
context.Context
111113
Dir string
112114
Env map[string]string
113115
Stdin io.Reader
@@ -119,6 +121,13 @@ type Context struct {
119121
serialisable bool
120122
}
121123

124+
// With returns a command context with the specified context.Context.
125+
func (ctx *Context) With(c context.Context) *Context {
126+
newCtx := *ctx
127+
newCtx.Context = c
128+
return &newCtx
129+
}
130+
122131
// Quiet reports whether the command is in "quiet" mode. When
123132
// this is true, informational output should be suppressed (logger
124133
// messages can be used instead).
@@ -407,12 +416,14 @@ func DefaultContext() (*Context, error) {
407416
if err != nil {
408417
return nil, err
409418
}
410-
return &Context{
419+
ctx := &Context{
411420
Dir: abs,
412421
Stdin: os.Stdin,
413422
Stdout: os.Stdout,
414423
Stderr: os.Stderr,
415-
}, nil
424+
}
425+
ctx.Context = context.Background()
426+
return ctx, nil
416427
}
417428

418429
// CheckEmpty is a utility function that returns an error if args is not empty.

cmd_test.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@ package cmd_test
55

66
import (
77
"bytes"
8+
"context"
89
"fmt"
910
"os"
1011
"path/filepath"
1112
"strings"
1213

14+
jc "github.com/juju/testing/checkers"
1315
gc "gopkg.in/check.v1"
1416

15-
"github.com/juju/cmd/v3"
16-
"github.com/juju/cmd/v3/cmdtesting"
1717
"github.com/juju/gnuflag"
1818
"github.com/juju/testing"
19+
20+
"github.com/juju/cmd/v3"
21+
"github.com/juju/cmd/v3/cmdtesting"
1922
)
2023

2124
var _ = gc.Suite(&CmdSuite{})
@@ -27,13 +30,22 @@ type CmdSuite struct {
2730

2831
func (s *CmdSuite) TestContext(c *gc.C) {
2932
ctx := cmdtesting.Context(c)
33+
c.Check(ctx.Context, jc.DeepEquals, context.Background())
3034
c.Check(ctx.AbsPath("/foo/bar"), gc.Equals, "/foo/bar")
3135
c.Check(ctx.AbsPath("/foo/../bar"), gc.Equals, "/bar")
3236
c.Check(ctx.AbsPath("foo/bar"), gc.Equals, filepath.Join(ctx.Dir, "foo/bar"))
3337
homeDir := os.Getenv("HOME")
3438
c.Check(ctx.AbsPath("~/foo/bar"), gc.Equals, filepath.Join(homeDir, "foo/bar"))
3539
}
3640

41+
func (s *CmdSuite) TestWith(c *gc.C) {
42+
ctx := cmdtesting.Context(c)
43+
cancelCtx, cancel := context.WithCancel(context.Background())
44+
defer cancel()
45+
ctx = ctx.With(cancelCtx)
46+
c.Assert(ctx.Context, jc.DeepEquals, cancelCtx)
47+
}
48+
3749
func (s *CmdSuite) TestContextGetenv(c *gc.C) {
3850
ctx := cmdtesting.Context(c)
3951
ctx.Env = make(map[string]string)

cmdtesting/cmd.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package cmdtesting
55

66
import (
77
"bytes"
8+
"context"
89
"io/ioutil"
910

1011
"github.com/juju/gnuflag"
@@ -36,23 +37,27 @@ func InitCommand(c cmd.Command, args []string) error {
3637
// Context creates a simple command execution context with the current
3738
// dir set to a newly created directory within the test directory.
3839
func Context(c *gc.C) *cmd.Context {
39-
return &cmd.Context{
40+
ctx := &cmd.Context{
4041
Dir: c.MkDir(),
4142
Stdin: &bytes.Buffer{},
4243
Stdout: &bytes.Buffer{},
4344
Stderr: &bytes.Buffer{},
4445
}
46+
ctx.Context = context.TODO()
47+
return ctx
4548
}
4649

4750
// ContextForDir creates a simple command execution context with the current
4851
// dir set to the specified directory.
4952
func ContextForDir(c *gc.C, dir string) *cmd.Context {
50-
return &cmd.Context{
53+
ctx := &cmd.Context{
5154
Dir: dir,
5255
Stdin: &bytes.Buffer{},
5356
Stdout: &bytes.Buffer{},
5457
Stderr: &bytes.Buffer{},
5558
}
59+
ctx.Context = context.TODO()
60+
return ctx
5661
}
5762

5863
// Stdout takes a command Context that we assume has been created in this

go.mod

+8-9
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,27 @@ go 1.17
44

55
require (
66
github.com/juju/ansiterm v0.0.0-20210706145210-9283cdf370b5
7-
github.com/juju/errors v0.0.0-20200330140219-3fe23663418f
7+
github.com/juju/errors v0.0.0-20220203013757-bd733f3c86b9
88
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d
99
github.com/juju/loggo v0.0.0-20210728185423-eebad3a902c4
10-
github.com/juju/testing v0.0.0-20220202055744-1ad0816210a6
11-
github.com/juju/utils/v3 v3.0.0-20220130232349-cd7ecef0e94a
10+
github.com/juju/testing v0.0.0-20220203020004-a0ff61f03494
11+
github.com/juju/utils/v3 v3.0.0-20220203023959-c3fbc78a33b0
1212
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
1313
gopkg.in/yaml.v2 v2.4.0
1414
)
1515

1616
require (
17-
github.com/juju/clock v0.0.0-20190205081909-9c5c9712527c // indirect
18-
github.com/juju/collections v0.0.0-20200605021417-0d0ec82b7271 // indirect
17+
github.com/juju/clock v0.0.0-20220203021603-d9deb868a28a // indirect
18+
github.com/juju/collections v0.0.0-20220203020748-febd7cad8a7a // indirect
1919
github.com/juju/mgo/v2 v2.0.0-20210302023703-70d5d206e208 // indirect
2020
github.com/juju/retry v0.0.0-20180821225755-9058e192b216 // indirect
21-
github.com/juju/version v0.0.0-20191219164919-81c1be00b9a6 // indirect
21+
github.com/juju/version/v2 v2.0.0-20211007103408-2e8da085dc23 // indirect
2222
github.com/kr/pretty v0.2.1 // indirect
2323
github.com/kr/text v0.2.0 // indirect
2424
github.com/lunixbochs/vtclean v1.0.0 // indirect
2525
github.com/mattn/go-colorable v0.1.8 // indirect
2626
github.com/mattn/go-isatty v0.0.13 // indirect
27-
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
28-
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect
27+
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
28+
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
2929
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect
30-
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
3130
)

0 commit comments

Comments
 (0)