Skip to content

Commit 14715b2

Browse files
committed
cmd/go: add Context parameter to base.command.Run
One small step to start propagating the context in cmd/go for tracing purposes. Updates #38714 Change-Id: Ibb6debeb9233f84d55f0e81244487355cbe7b82c Reviewed-on: https://go-review.googlesource.com/c/go/+/237684 Run-TryBot: Michael Matloob <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 52b0ea2 commit 14715b2

File tree

26 files changed

+52
-28
lines changed

26 files changed

+52
-28
lines changed

src/cmd/go/internal/base/base.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package base
88

99
import (
10+
"context"
1011
"flag"
1112
"fmt"
1213
"log"
@@ -24,7 +25,7 @@ import (
2425
type Command struct {
2526
// Run runs the command.
2627
// The args are the arguments after the command name.
27-
Run func(cmd *Command, args []string)
28+
Run func(ctx context.Context, cmd *Command, args []string)
2829

2930
// UsageLine is the one-line usage message.
3031
// The words between "go" and the first flag or argument in the line are taken to be the command name.

src/cmd/go/internal/bug/bug.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package bug
77

88
import (
99
"bytes"
10+
"context"
1011
"fmt"
1112
"io"
1213
"io/ioutil"
@@ -37,7 +38,7 @@ func init() {
3738
CmdBug.Flag.BoolVar(&cfg.BuildV, "v", false, "")
3839
}
3940

40-
func runBug(cmd *base.Command, args []string) {
41+
func runBug(ctx context.Context, cmd *base.Command, args []string) {
4142
if len(args) > 0 {
4243
base.Fatalf("go bug: bug takes no arguments")
4344
}

src/cmd/go/internal/clean/clean.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package clean
77

88
import (
9+
"context"
910
"fmt"
1011
"io/ioutil"
1112
"os"
@@ -105,7 +106,7 @@ func init() {
105106
work.AddBuildFlags(CmdClean, work.DefaultBuildFlags)
106107
}
107108

108-
func runClean(cmd *base.Command, args []string) {
109+
func runClean(ctx context.Context, cmd *base.Command, args []string) {
109110
// golang.org/issue/29925: only load packages before cleaning if
110111
// either the flags and arguments explicitly imply a package,
111112
// or no other target (such as a cache) was requested to be cleaned.

src/cmd/go/internal/doc/doc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package doc
88
import (
99
"cmd/go/internal/base"
1010
"cmd/go/internal/cfg"
11+
"context"
1112
)
1213

1314
var CmdDoc = &base.Command{
@@ -129,6 +130,6 @@ Flags:
129130
`,
130131
}
131132

132-
func runDoc(cmd *base.Command, args []string) {
133+
func runDoc(ctx context.Context, cmd *base.Command, args []string) {
133134
base.Run(cfg.BuildToolexec, base.Tool("doc"), args)
134135
}

src/cmd/go/internal/envcmd/env.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package envcmd
77

88
import (
9+
"context"
910
"encoding/json"
1011
"fmt"
1112
"go/build"
@@ -186,7 +187,7 @@ func argKey(arg string) string {
186187
return arg[:i]
187188
}
188189

189-
func runEnv(cmd *base.Command, args []string) {
190+
func runEnv(ctx context.Context, cmd *base.Command, args []string) {
190191
if *envJson && *envU {
191192
base.Fatalf("go env: cannot use -json with -u")
192193
}

src/cmd/go/internal/fix/fix.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"cmd/go/internal/load"
1212
"cmd/go/internal/modload"
1313
"cmd/go/internal/str"
14+
"context"
1415
"fmt"
1516
"os"
1617
)
@@ -31,7 +32,7 @@ See also: go fmt, go vet.
3132
`,
3233
}
3334

34-
func runFix(cmd *base.Command, args []string) {
35+
func runFix(ctx context.Context, cmd *base.Command, args []string) {
3536
printed := false
3637
for _, pkg := range load.Packages(args) {
3738
if modload.Enabled() && pkg.Module != nil && !pkg.Module.Main {

src/cmd/go/internal/fmtcmd/fmt.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package fmtcmd
77

88
import (
9+
"context"
910
"errors"
1011
"fmt"
1112
"os"
@@ -48,7 +49,7 @@ See also: go fix, go vet.
4849
`,
4950
}
5051

51-
func runFmt(cmd *base.Command, args []string) {
52+
func runFmt(ctx context.Context, cmd *base.Command, args []string) {
5253
printed := false
5354
gofmt := gofmtPath()
5455
procs := runtime.GOMAXPROCS(0)

src/cmd/go/internal/generate/generate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package generate
88
import (
99
"bufio"
1010
"bytes"
11+
"context"
1112
"fmt"
1213
"go/parser"
1314
"go/token"
@@ -160,7 +161,7 @@ func init() {
160161
CmdGenerate.Flag.StringVar(&generateRunFlag, "run", "", "")
161162
}
162163

163-
func runGenerate(cmd *base.Command, args []string) {
164+
func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
164165
load.IgnoreImports = true
165166

166167
if generateRunFlag != "" {

src/cmd/go/internal/get/get.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package get
77

88
import (
9+
"context"
910
"fmt"
1011
"os"
1112
"path/filepath"
@@ -112,7 +113,7 @@ func init() {
112113
CmdGet.Flag.BoolVar(&Insecure, "insecure", Insecure, "")
113114
}
114115

115-
func runGet(cmd *base.Command, args []string) {
116+
func runGet(ctx context.Context, cmd *base.Command, args []string) {
116117
if cfg.ModulesEnabled {
117118
// Should not happen: main.go should install the separate module-enabled get code.
118119
base.Fatalf("go get: modules not implemented")

src/cmd/go/internal/list/list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package list
88
import (
99
"bufio"
1010
"bytes"
11+
"context"
1112
"encoding/json"
1213
"io"
1314
"os"
@@ -309,7 +310,7 @@ var (
309310

310311
var nl = []byte{'\n'}
311312

312-
func runList(cmd *base.Command, args []string) {
313+
func runList(ctx context.Context, cmd *base.Command, args []string) {
313314
modload.LoadTests = *listTest
314315
work.BuildInit()
315316
out := newTrackingWriter(os.Stdout)

src/cmd/go/internal/modcmd/download.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package modcmd
66

77
import (
8+
"context"
89
"encoding/json"
910
"os"
1011

@@ -78,7 +79,7 @@ type moduleJSON struct {
7879
GoModSum string `json:",omitempty"`
7980
}
8081

81-
func runDownload(cmd *base.Command, args []string) {
82+
func runDownload(ctx context.Context, cmd *base.Command, args []string) {
8283
// Check whether modules are enabled and whether we're in a module.
8384
if cfg.Getenv("GO111MODULE") == "off" {
8485
base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")

src/cmd/go/internal/modcmd/edit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package modcmd
88

99
import (
1010
"bytes"
11+
"context"
1112
"encoding/json"
1213
"errors"
1314
"fmt"
@@ -141,7 +142,7 @@ func init() {
141142
base.AddBuildFlagsNX(&cmdEdit.Flag)
142143
}
143144

144-
func runEdit(cmd *base.Command, args []string) {
145+
func runEdit(ctx context.Context, cmd *base.Command, args []string) {
145146
anyFlags :=
146147
*editModule != "" ||
147148
*editGo != "" ||

src/cmd/go/internal/modcmd/graph.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package modcmd
88

99
import (
1010
"bufio"
11+
"context"
1112
"os"
1213
"sort"
1314

@@ -36,7 +37,7 @@ func init() {
3637
work.AddModCommonFlags(cmdGraph)
3738
}
3839

39-
func runGraph(cmd *base.Command, args []string) {
40+
func runGraph(ctx context.Context, cmd *base.Command, args []string) {
4041
if len(args) > 0 {
4142
base.Fatalf("go mod graph: graph takes no arguments")
4243
}

src/cmd/go/internal/modcmd/init.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"cmd/go/internal/base"
1111
"cmd/go/internal/modload"
1212
"cmd/go/internal/work"
13+
"context"
1314
"os"
1415
"strings"
1516
)
@@ -32,7 +33,7 @@ func init() {
3233
work.AddModCommonFlags(cmdInit)
3334
}
3435

35-
func runInit(cmd *base.Command, args []string) {
36+
func runInit(ctx context.Context, cmd *base.Command, args []string) {
3637
modload.CmdModInit = true
3738
if len(args) > 1 {
3839
base.Fatalf("go mod init: too many arguments")

src/cmd/go/internal/modcmd/tidy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"cmd/go/internal/modfetch"
1313
"cmd/go/internal/modload"
1414
"cmd/go/internal/work"
15+
"context"
1516

1617
"golang.org/x/mod/module"
1718
)
@@ -37,7 +38,7 @@ func init() {
3738
work.AddModCommonFlags(cmdTidy)
3839
}
3940

40-
func runTidy(cmd *base.Command, args []string) {
41+
func runTidy(ctx context.Context, cmd *base.Command, args []string) {
4142
if len(args) > 0 {
4243
base.Fatalf("go mod tidy: no arguments allowed")
4344
}

src/cmd/go/internal/modcmd/vendor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package modcmd
66

77
import (
88
"bytes"
9+
"context"
910
"fmt"
1011
"io"
1112
"io/ioutil"
@@ -43,7 +44,7 @@ func init() {
4344
work.AddModCommonFlags(cmdVendor)
4445
}
4546

46-
func runVendor(cmd *base.Command, args []string) {
47+
func runVendor(ctx context.Context, cmd *base.Command, args []string) {
4748
if len(args) != 0 {
4849
base.Fatalf("go mod vendor: vendor takes no arguments")
4950
}

src/cmd/go/internal/modcmd/verify.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package modcmd
66

77
import (
88
"bytes"
9+
"context"
910
"errors"
1011
"fmt"
1112
"io/ioutil"
@@ -40,7 +41,7 @@ func init() {
4041
work.AddModCommonFlags(cmdVerify)
4142
}
4243

43-
func runVerify(cmd *base.Command, args []string) {
44+
func runVerify(ctx context.Context, cmd *base.Command, args []string) {
4445
if len(args) != 0 {
4546
// NOTE(rsc): Could take a module pattern.
4647
base.Fatalf("go mod verify: verify takes no arguments")

src/cmd/go/internal/modcmd/why.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package modcmd
66

77
import (
8+
"context"
89
"fmt"
910
"strings"
1011

@@ -60,7 +61,7 @@ func init() {
6061
work.AddModCommonFlags(cmdWhy)
6162
}
6263

63-
func runWhy(cmd *base.Command, args []string) {
64+
func runWhy(ctx context.Context, cmd *base.Command, args []string) {
6465
loadALL := modload.LoadALL
6566
if *whyVendor {
6667
loadALL = modload.LoadVendor

src/cmd/go/internal/modget/get.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package modget
77

88
import (
9+
"context"
910
"errors"
1011
"fmt"
1112
"os"
@@ -259,7 +260,7 @@ type query struct {
259260
m module.Version
260261
}
261262

262-
func runGet(cmd *base.Command, args []string) {
263+
func runGet(ctx context.Context, cmd *base.Command, args []string) {
263264
switch getU {
264265
case "", "upgrade", "patch":
265266
// ok

src/cmd/go/internal/run/run.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package run
77

88
import (
9+
"context"
910
"fmt"
1011
"os"
1112
"path"
@@ -57,7 +58,7 @@ func printStderr(args ...interface{}) (int, error) {
5758
return fmt.Fprint(os.Stderr, args...)
5859
}
5960

60-
func runRun(cmd *base.Command, args []string) {
61+
func runRun(ctx context.Context, cmd *base.Command, args []string) {
6162
work.BuildInit()
6263
var b work.Builder
6364
b.Init()

src/cmd/go/internal/test/test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package test
66

77
import (
88
"bytes"
9+
"context"
910
"crypto/sha256"
1011
"errors"
1112
"fmt"
@@ -565,7 +566,7 @@ var defaultVetFlags = []string{
565566
// "-unusedresult",
566567
}
567568

568-
func runTest(cmd *base.Command, args []string) {
569+
func runTest(ctx context.Context, cmd *base.Command, args []string) {
569570
modload.LoadTests = true
570571

571572
pkgArgs, testArgs = testFlags(args)

src/cmd/go/internal/tool/tool.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package tool
77

88
import (
9+
"context"
910
"fmt"
1011
"os"
1112
"os/exec"
@@ -48,7 +49,7 @@ func init() {
4849
CmdTool.Flag.BoolVar(&toolN, "n", false, "")
4950
}
5051

51-
func runTool(cmd *base.Command, args []string) {
52+
func runTool(ctx context.Context, cmd *base.Command, args []string) {
5253
if len(args) == 0 {
5354
listTools()
5455
return

src/cmd/go/internal/version/version.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package version
77

88
import (
99
"bytes"
10+
"context"
1011
"encoding/binary"
1112
"fmt"
1213
"os"
@@ -51,7 +52,7 @@ var (
5152
versionV = CmdVersion.Flag.Bool("v", false, "")
5253
)
5354

54-
func runVersion(cmd *base.Command, args []string) {
55+
func runVersion(ctx context.Context, cmd *base.Command, args []string) {
5556
if len(args) == 0 {
5657
if *versionM || *versionV {
5758
fmt.Fprintf(os.Stderr, "go version: flags can only be used with arguments\n")

0 commit comments

Comments
 (0)