Skip to content

Commit

Permalink
cmd/go: add Context parameter to base.command.Run
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
matloob committed Aug 12, 2020
1 parent 52b0ea2 commit 14715b2
Show file tree
Hide file tree
Showing 26 changed files with 52 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/cmd/go/internal/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package base

import (
"context"
"flag"
"fmt"
"log"
Expand All @@ -24,7 +25,7 @@ import (
type Command struct {
// Run runs the command.
// The args are the arguments after the command name.
Run func(cmd *Command, args []string)
Run func(ctx context.Context, cmd *Command, args []string)

// UsageLine is the one-line usage message.
// The words between "go" and the first flag or argument in the line are taken to be the command name.
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/bug/bug.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package bug

import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -37,7 +38,7 @@ func init() {
CmdBug.Flag.BoolVar(&cfg.BuildV, "v", false, "")
}

func runBug(cmd *base.Command, args []string) {
func runBug(ctx context.Context, cmd *base.Command, args []string) {
if len(args) > 0 {
base.Fatalf("go bug: bug takes no arguments")
}
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/clean/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package clean

import (
"context"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -105,7 +106,7 @@ func init() {
work.AddBuildFlags(CmdClean, work.DefaultBuildFlags)
}

func runClean(cmd *base.Command, args []string) {
func runClean(ctx context.Context, cmd *base.Command, args []string) {
// golang.org/issue/29925: only load packages before cleaning if
// either the flags and arguments explicitly imply a package,
// or no other target (such as a cache) was requested to be cleaned.
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/doc/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package doc
import (
"cmd/go/internal/base"
"cmd/go/internal/cfg"
"context"
)

var CmdDoc = &base.Command{
Expand Down Expand Up @@ -129,6 +130,6 @@ Flags:
`,
}

func runDoc(cmd *base.Command, args []string) {
func runDoc(ctx context.Context, cmd *base.Command, args []string) {
base.Run(cfg.BuildToolexec, base.Tool("doc"), args)
}
3 changes: 2 additions & 1 deletion src/cmd/go/internal/envcmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package envcmd

import (
"context"
"encoding/json"
"fmt"
"go/build"
Expand Down Expand Up @@ -186,7 +187,7 @@ func argKey(arg string) string {
return arg[:i]
}

func runEnv(cmd *base.Command, args []string) {
func runEnv(ctx context.Context, cmd *base.Command, args []string) {
if *envJson && *envU {
base.Fatalf("go env: cannot use -json with -u")
}
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/fix/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"cmd/go/internal/load"
"cmd/go/internal/modload"
"cmd/go/internal/str"
"context"
"fmt"
"os"
)
Expand All @@ -31,7 +32,7 @@ See also: go fmt, go vet.
`,
}

func runFix(cmd *base.Command, args []string) {
func runFix(ctx context.Context, cmd *base.Command, args []string) {
printed := false
for _, pkg := range load.Packages(args) {
if modload.Enabled() && pkg.Module != nil && !pkg.Module.Main {
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/fmtcmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package fmtcmd

import (
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -48,7 +49,7 @@ See also: go fix, go vet.
`,
}

func runFmt(cmd *base.Command, args []string) {
func runFmt(ctx context.Context, cmd *base.Command, args []string) {
printed := false
gofmt := gofmtPath()
procs := runtime.GOMAXPROCS(0)
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package generate
import (
"bufio"
"bytes"
"context"
"fmt"
"go/parser"
"go/token"
Expand Down Expand Up @@ -160,7 +161,7 @@ func init() {
CmdGenerate.Flag.StringVar(&generateRunFlag, "run", "", "")
}

func runGenerate(cmd *base.Command, args []string) {
func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
load.IgnoreImports = true

if generateRunFlag != "" {
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package get

import (
"context"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -112,7 +113,7 @@ func init() {
CmdGet.Flag.BoolVar(&Insecure, "insecure", Insecure, "")
}

func runGet(cmd *base.Command, args []string) {
func runGet(ctx context.Context, cmd *base.Command, args []string) {
if cfg.ModulesEnabled {
// Should not happen: main.go should install the separate module-enabled get code.
base.Fatalf("go get: modules not implemented")
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package list
import (
"bufio"
"bytes"
"context"
"encoding/json"
"io"
"os"
Expand Down Expand Up @@ -309,7 +310,7 @@ var (

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

func runList(cmd *base.Command, args []string) {
func runList(ctx context.Context, cmd *base.Command, args []string) {
modload.LoadTests = *listTest
work.BuildInit()
out := newTrackingWriter(os.Stdout)
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/modcmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package modcmd

import (
"context"
"encoding/json"
"os"

Expand Down Expand Up @@ -78,7 +79,7 @@ type moduleJSON struct {
GoModSum string `json:",omitempty"`
}

func runDownload(cmd *base.Command, args []string) {
func runDownload(ctx context.Context, cmd *base.Command, args []string) {
// Check whether modules are enabled and whether we're in a module.
if cfg.Getenv("GO111MODULE") == "off" {
base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/modcmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package modcmd

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -141,7 +142,7 @@ func init() {
base.AddBuildFlagsNX(&cmdEdit.Flag)
}

func runEdit(cmd *base.Command, args []string) {
func runEdit(ctx context.Context, cmd *base.Command, args []string) {
anyFlags :=
*editModule != "" ||
*editGo != "" ||
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/modcmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package modcmd

import (
"bufio"
"context"
"os"
"sort"

Expand Down Expand Up @@ -36,7 +37,7 @@ func init() {
work.AddModCommonFlags(cmdGraph)
}

func runGraph(cmd *base.Command, args []string) {
func runGraph(ctx context.Context, cmd *base.Command, args []string) {
if len(args) > 0 {
base.Fatalf("go mod graph: graph takes no arguments")
}
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/modcmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"cmd/go/internal/base"
"cmd/go/internal/modload"
"cmd/go/internal/work"
"context"
"os"
"strings"
)
Expand All @@ -32,7 +33,7 @@ func init() {
work.AddModCommonFlags(cmdInit)
}

func runInit(cmd *base.Command, args []string) {
func runInit(ctx context.Context, cmd *base.Command, args []string) {
modload.CmdModInit = true
if len(args) > 1 {
base.Fatalf("go mod init: too many arguments")
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/modcmd/tidy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"cmd/go/internal/modfetch"
"cmd/go/internal/modload"
"cmd/go/internal/work"
"context"

"golang.org/x/mod/module"
)
Expand All @@ -37,7 +38,7 @@ func init() {
work.AddModCommonFlags(cmdTidy)
}

func runTidy(cmd *base.Command, args []string) {
func runTidy(ctx context.Context, cmd *base.Command, args []string) {
if len(args) > 0 {
base.Fatalf("go mod tidy: no arguments allowed")
}
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/modcmd/vendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package modcmd

import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -43,7 +44,7 @@ func init() {
work.AddModCommonFlags(cmdVendor)
}

func runVendor(cmd *base.Command, args []string) {
func runVendor(ctx context.Context, cmd *base.Command, args []string) {
if len(args) != 0 {
base.Fatalf("go mod vendor: vendor takes no arguments")
}
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/modcmd/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package modcmd

import (
"bytes"
"context"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -40,7 +41,7 @@ func init() {
work.AddModCommonFlags(cmdVerify)
}

func runVerify(cmd *base.Command, args []string) {
func runVerify(ctx context.Context, cmd *base.Command, args []string) {
if len(args) != 0 {
// NOTE(rsc): Could take a module pattern.
base.Fatalf("go mod verify: verify takes no arguments")
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/modcmd/why.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package modcmd

import (
"context"
"fmt"
"strings"

Expand Down Expand Up @@ -60,7 +61,7 @@ func init() {
work.AddModCommonFlags(cmdWhy)
}

func runWhy(cmd *base.Command, args []string) {
func runWhy(ctx context.Context, cmd *base.Command, args []string) {
loadALL := modload.LoadALL
if *whyVendor {
loadALL = modload.LoadVendor
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/modget/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package modget

import (
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -259,7 +260,7 @@ type query struct {
m module.Version
}

func runGet(cmd *base.Command, args []string) {
func runGet(ctx context.Context, cmd *base.Command, args []string) {
switch getU {
case "", "upgrade", "patch":
// ok
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package run

import (
"context"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -57,7 +58,7 @@ func printStderr(args ...interface{}) (int, error) {
return fmt.Fprint(os.Stderr, args...)
}

func runRun(cmd *base.Command, args []string) {
func runRun(ctx context.Context, cmd *base.Command, args []string) {
work.BuildInit()
var b work.Builder
b.Init()
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package test

import (
"bytes"
"context"
"crypto/sha256"
"errors"
"fmt"
Expand Down Expand Up @@ -565,7 +566,7 @@ var defaultVetFlags = []string{
// "-unusedresult",
}

func runTest(cmd *base.Command, args []string) {
func runTest(ctx context.Context, cmd *base.Command, args []string) {
modload.LoadTests = true

pkgArgs, testArgs = testFlags(args)
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/tool/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package tool

import (
"context"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -48,7 +49,7 @@ func init() {
CmdTool.Flag.BoolVar(&toolN, "n", false, "")
}

func runTool(cmd *base.Command, args []string) {
func runTool(ctx context.Context, cmd *base.Command, args []string) {
if len(args) == 0 {
listTools()
return
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package version

import (
"bytes"
"context"
"encoding/binary"
"fmt"
"os"
Expand Down Expand Up @@ -51,7 +52,7 @@ var (
versionV = CmdVersion.Flag.Bool("v", false, "")
)

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

0 comments on commit 14715b2

Please sign in to comment.