Skip to content

Commit f0c5dfa

Browse files
authored
Merge pull request #2184 from laurazard/cli-signal-handling
Use Cobra's `command.Context()`, cancel execution when CLI is signalled
2 parents 7e4021a + 147c713 commit f0c5dfa

12 files changed

Lines changed: 36 additions & 60 deletions

File tree

commands/bake.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/docker/buildx/util/tracing"
2525
"github.com/docker/cli/cli/command"
2626
"github.com/moby/buildkit/identity"
27-
"github.com/moby/buildkit/util/appcontext"
2827
"github.com/moby/buildkit/util/progress/progressui"
2928
"github.com/pkg/errors"
3029
"github.com/spf13/cobra"
@@ -43,9 +42,7 @@ type bakeOptions struct {
4342
exportLoad bool
4443
}
4544

46-
func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags commonFlags) (err error) {
47-
ctx := appcontext.Context()
48-
45+
func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in bakeOptions, cFlags commonFlags) (err error) {
4946
mp, report, err := metrics.MeterProvider(dockerCli)
5047
if err != nil {
5148
return err
@@ -275,7 +272,7 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
275272
options.builder = rootOpts.builder
276273
options.metadataFile = cFlags.metadataFile
277274
// Other common flags (noCache, pull and progress) are processed in runBake function.
278-
return runBake(dockerCli, args, options, cFlags)
275+
return runBake(cmd.Context(), dockerCli, args, options, cFlags)
279276
},
280277
ValidArgsFunction: completion.BakeTargets(options.files),
281278
}

commands/build.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import (
4646
"github.com/moby/buildkit/frontend/subrequests/outline"
4747
"github.com/moby/buildkit/frontend/subrequests/targets"
4848
"github.com/moby/buildkit/solver/errdefs"
49-
"github.com/moby/buildkit/util/appcontext"
5049
"github.com/moby/buildkit/util/grpcerrors"
5150
"github.com/moby/buildkit/util/progress/progressui"
5251
"github.com/morikuni/aec"
@@ -216,9 +215,7 @@ func (o *buildOptions) toDisplayMode() (progressui.DisplayMode, error) {
216215
return progress, nil
217216
}
218217

219-
func runBuild(dockerCli command.Cli, options buildOptions) (err error) {
220-
ctx := appcontext.Context()
221-
218+
func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions) (err error) {
222219
mp, report, err := metrics.MeterProvider(dockerCli)
223220
if err != nil {
224221
return err
@@ -487,7 +484,7 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions, debugConfig *debug.D
487484
options.invokeConfig = iConfig
488485
}
489486

490-
return runBuild(dockerCli, *options)
487+
return runBuild(cmd.Context(), dockerCli, *options)
491488
},
492489
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
493490
return nil, cobra.ShellCompDirectiveFilterDirs

commands/create.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package commands
22

33
import (
44
"bytes"
5+
"context"
56
"fmt"
67

78
"github.com/docker/buildx/builder"
@@ -11,7 +12,6 @@ import (
1112
"github.com/docker/buildx/util/cobrautil/completion"
1213
"github.com/docker/cli/cli"
1314
"github.com/docker/cli/cli/command"
14-
"github.com/moby/buildkit/util/appcontext"
1515
"github.com/spf13/cobra"
1616
)
1717

@@ -30,9 +30,7 @@ type createOptions struct {
3030
// upgrade bool // perform upgrade of the driver
3131
}
3232

33-
func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
34-
ctx := appcontext.Context()
35-
33+
func runCreate(ctx context.Context, dockerCli command.Cli, in createOptions, args []string) error {
3634
txn, release, err := storeutil.GetStore(dockerCli)
3735
if err != nil {
3836
return err
@@ -98,7 +96,7 @@ func createCmd(dockerCli command.Cli) *cobra.Command {
9896
Short: "Create a new builder instance",
9997
Args: cli.RequiresMaxArgs(1),
10098
RunE: func(cmd *cobra.Command, args []string) error {
101-
return runCreate(dockerCli, options, args)
99+
return runCreate(cmd.Context(), dockerCli, options, args)
102100
},
103101
ValidArgsFunction: completion.Disable,
104102
}

commands/diskusage.go

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

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"os"
@@ -15,7 +16,6 @@ import (
1516
"github.com/docker/cli/opts"
1617
"github.com/docker/go-units"
1718
"github.com/moby/buildkit/client"
18-
"github.com/moby/buildkit/util/appcontext"
1919
"github.com/spf13/cobra"
2020
"golang.org/x/sync/errgroup"
2121
)
@@ -26,9 +26,7 @@ type duOptions struct {
2626
verbose bool
2727
}
2828

29-
func runDiskUsage(dockerCli command.Cli, opts duOptions) error {
30-
ctx := appcontext.Context()
31-
29+
func runDiskUsage(ctx context.Context, dockerCli command.Cli, opts duOptions) error {
3230
pi, err := toBuildkitPruneInfo(opts.filter.Value())
3331
if err != nil {
3432
return err
@@ -114,7 +112,7 @@ func duCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
114112
Args: cli.NoArgs,
115113
RunE: func(cmd *cobra.Command, args []string) error {
116114
options.builder = rootOpts.builder
117-
return runDiskUsage(dockerCli, options)
115+
return runDiskUsage(cmd.Context(), dockerCli, options)
118116
},
119117
ValidArgsFunction: completion.Disable,
120118
}

commands/imagetools/create.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/docker/buildx/util/imagetools"
1414
"github.com/docker/buildx/util/progress"
1515
"github.com/docker/cli/cli/command"
16-
"github.com/moby/buildkit/util/appcontext"
1716
"github.com/moby/buildkit/util/progress/progressui"
1817
"github.com/opencontainers/go-digest"
1918
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -32,7 +31,7 @@ type createOptions struct {
3231
progress string
3332
}
3433

35-
func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
34+
func runCreate(ctx context.Context, dockerCli command.Cli, in createOptions, args []string) error {
3635
if len(args) == 0 && len(in.files) == 0 {
3736
return errors.Errorf("no sources specified")
3837
}
@@ -113,8 +112,6 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
113112
}
114113
}
115114

116-
ctx := appcontext.Context()
117-
118115
b, err := builder.New(dockerCli, builder.WithName(in.builder))
119116
if err != nil {
120117
return err
@@ -274,7 +271,7 @@ func createCmd(dockerCli command.Cli, opts RootOptions) *cobra.Command {
274271
Short: "Create a new image based on source images",
275272
RunE: func(cmd *cobra.Command, args []string) error {
276273
options.builder = *opts.Builder
277-
return runCreate(dockerCli, options, args)
274+
return runCreate(cmd.Context(), dockerCli, options, args)
278275
},
279276
ValidArgsFunction: completion.Disable,
280277
}

commands/imagetools/inspect.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package commands
22

33
import (
4+
"context"
5+
46
"github.com/docker/buildx/builder"
57
"github.com/docker/buildx/util/cobrautil/completion"
68
"github.com/docker/buildx/util/imagetools"
79
"github.com/docker/cli-docs-tool/annotation"
810
"github.com/docker/cli/cli"
911
"github.com/docker/cli/cli/command"
10-
"github.com/moby/buildkit/util/appcontext"
1112
"github.com/pkg/errors"
1213
"github.com/spf13/cobra"
1314
)
@@ -18,9 +19,7 @@ type inspectOptions struct {
1819
raw bool
1920
}
2021

21-
func runInspect(dockerCli command.Cli, in inspectOptions, name string) error {
22-
ctx := appcontext.Context()
23-
22+
func runInspect(ctx context.Context, dockerCli command.Cli, in inspectOptions, name string) error {
2423
if in.format != "" && in.raw {
2524
return errors.Errorf("format and raw cannot be used together")
2625
}
@@ -51,7 +50,7 @@ func inspectCmd(dockerCli command.Cli, rootOpts RootOptions) *cobra.Command {
5150
Args: cli.ExactArgs(1),
5251
RunE: func(cmd *cobra.Command, args []string) error {
5352
options.builder = *rootOpts.Builder
54-
return runInspect(dockerCli, options, args[0])
53+
return runInspect(cmd.Context(), dockerCli, options, args[0])
5554
},
5655
ValidArgsFunction: completion.Disable,
5756
}

commands/inspect.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/docker/cli/cli/command"
1818
"github.com/docker/cli/cli/debug"
1919
"github.com/docker/go-units"
20-
"github.com/moby/buildkit/util/appcontext"
2120
"github.com/spf13/cobra"
2221
)
2322

@@ -26,9 +25,7 @@ type inspectOptions struct {
2625
builder string
2726
}
2827

29-
func runInspect(dockerCli command.Cli, in inspectOptions) error {
30-
ctx := appcontext.Context()
31-
28+
func runInspect(ctx context.Context, dockerCli command.Cli, in inspectOptions) error {
3229
b, err := builder.New(dockerCli,
3330
builder.WithName(in.builder),
3431
builder.WithSkippedValidation(),
@@ -150,7 +147,7 @@ func inspectCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
150147
if len(args) > 0 {
151148
options.builder = args[0]
152149
}
153-
return runInspect(dockerCli, options)
150+
return runInspect(cmd.Context(), dockerCli, options)
154151
},
155152
ValidArgsFunction: completion.BuilderNames(dockerCli),
156153
}

commands/ls.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/docker/cli/cli"
1818
"github.com/docker/cli/cli/command"
1919
"github.com/docker/cli/cli/command/formatter"
20-
"github.com/moby/buildkit/util/appcontext"
2120
"github.com/spf13/cobra"
2221
"golang.org/x/sync/errgroup"
2322
)
@@ -39,9 +38,7 @@ type lsOptions struct {
3938
format string
4039
}
4140

42-
func runLs(dockerCli command.Cli, in lsOptions) error {
43-
ctx := appcontext.Context()
44-
41+
func runLs(ctx context.Context, dockerCli command.Cli, in lsOptions) error {
4542
txn, release, err := storeutil.GetStore(dockerCli)
4643
if err != nil {
4744
return err
@@ -103,7 +100,7 @@ func lsCmd(dockerCli command.Cli) *cobra.Command {
103100
Short: "List builder instances",
104101
Args: cli.ExactArgs(0),
105102
RunE: func(cmd *cobra.Command, args []string) error {
106-
return runLs(dockerCli, options)
103+
return runLs(cmd.Context(), dockerCli, options)
107104
},
108105
ValidArgsFunction: completion.Disable,
109106
}

commands/prune.go

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

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"strings"
@@ -15,7 +16,6 @@ import (
1516
"github.com/docker/docker/api/types/filters"
1617
"github.com/docker/go-units"
1718
"github.com/moby/buildkit/client"
18-
"github.com/moby/buildkit/util/appcontext"
1919
"github.com/pkg/errors"
2020
"github.com/spf13/cobra"
2121
"golang.org/x/sync/errgroup"
@@ -35,9 +35,7 @@ const (
3535
allCacheWarning = `WARNING! This will remove all build cache. Are you sure you want to continue?`
3636
)
3737

38-
func runPrune(dockerCli command.Cli, opts pruneOptions) error {
39-
ctx := appcontext.Context()
40-
38+
func runPrune(ctx context.Context, dockerCli command.Cli, opts pruneOptions) error {
4139
pruneFilters := opts.filter.Value()
4240
pruneFilters = command.PruneFilters(dockerCli, pruneFilters)
4341

@@ -138,7 +136,7 @@ func pruneCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
138136
Args: cli.NoArgs,
139137
RunE: func(cmd *cobra.Command, args []string) error {
140138
options.builder = rootOpts.builder
141-
return runPrune(dockerCli, options)
139+
return runPrune(cmd.Context(), dockerCli, options)
142140
},
143141
ValidArgsFunction: completion.Disable,
144142
}

commands/rm.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/docker/buildx/store/storeutil"
1111
"github.com/docker/buildx/util/cobrautil/completion"
1212
"github.com/docker/cli/cli/command"
13-
"github.com/moby/buildkit/util/appcontext"
1413
"github.com/pkg/errors"
1514
"github.com/spf13/cobra"
1615
"golang.org/x/sync/errgroup"
@@ -28,9 +27,7 @@ const (
2827
rmInactiveWarning = `WARNING! This will remove all builders that are not in running state. Are you sure you want to continue?`
2928
)
3029

31-
func runRm(dockerCli command.Cli, in rmOptions) error {
32-
ctx := appcontext.Context()
33-
30+
func runRm(ctx context.Context, dockerCli command.Cli, in rmOptions) error {
3431
if in.allInactive && !in.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), rmInactiveWarning) {
3532
return nil
3633
}
@@ -108,7 +105,7 @@ func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
108105
}
109106
options.builders = args
110107
}
111-
return runRm(dockerCli, options)
108+
return runRm(cmd.Context(), dockerCli, options)
112109
},
113110
ValidArgsFunction: completion.BuilderNames(dockerCli),
114111
}

0 commit comments

Comments
 (0)