]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add Context parameter to base.command.Run
authorMichael Matloob <matloob@golang.org>
Fri, 12 Jun 2020 18:33:23 +0000 (14:33 -0400)
committerMichael Matloob <matloob@golang.org>
Wed, 12 Aug 2020 18:35:38 +0000 (18:35 +0000)
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 <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
26 files changed:
src/cmd/go/internal/base/base.go
src/cmd/go/internal/bug/bug.go
src/cmd/go/internal/clean/clean.go
src/cmd/go/internal/doc/doc.go
src/cmd/go/internal/envcmd/env.go
src/cmd/go/internal/fix/fix.go
src/cmd/go/internal/fmtcmd/fmt.go
src/cmd/go/internal/generate/generate.go
src/cmd/go/internal/get/get.go
src/cmd/go/internal/list/list.go
src/cmd/go/internal/modcmd/download.go
src/cmd/go/internal/modcmd/edit.go
src/cmd/go/internal/modcmd/graph.go
src/cmd/go/internal/modcmd/init.go
src/cmd/go/internal/modcmd/tidy.go
src/cmd/go/internal/modcmd/vendor.go
src/cmd/go/internal/modcmd/verify.go
src/cmd/go/internal/modcmd/why.go
src/cmd/go/internal/modget/get.go
src/cmd/go/internal/run/run.go
src/cmd/go/internal/test/test.go
src/cmd/go/internal/tool/tool.go
src/cmd/go/internal/version/version.go
src/cmd/go/internal/vet/vet.go
src/cmd/go/internal/work/build.go
src/cmd/go/main.go

index ab2f1bb4e2c27d774e552a3cbe47cbf5f3a23194..db3ebef933842e736e11f22a0967c93c1dffa5d8 100644 (file)
@@ -7,6 +7,7 @@
 package base
 
 import (
+       "context"
        "flag"
        "fmt"
        "log"
@@ -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.
index fe71281ef054ebebf4a3f51c0f267acbdc4aa999..52bd40f2fb1cd3699840b539992e1f92f57ae843 100644 (file)
@@ -7,6 +7,7 @@ package bug
 
 import (
        "bytes"
+       "context"
        "fmt"
        "io"
        "io/ioutil"
@@ -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")
        }
index 99704cb2b1b8c4da0194c25b5a59ca4d9df7309a..8af3e3df9c342111636239d9031b8f97e6d4a12a 100644 (file)
@@ -6,6 +6,7 @@
 package clean
 
 import (
+       "context"
        "fmt"
        "io/ioutil"
        "os"
@@ -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.
index 4ff08bb9289a09bee8e84dd31cdbe69a987e567b..67f76e22563b6e84d21b7622b55675d03af60db2 100644 (file)
@@ -8,6 +8,7 @@ package doc
 import (
        "cmd/go/internal/base"
        "cmd/go/internal/cfg"
+       "context"
 )
 
 var CmdDoc = &base.Command{
@@ -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)
 }
index 252025dc25595d32c7402e7f1efb5d2b8fe2544a..403e0f4a7b4a2957263d25472a89fb31aff6a4c4 100644 (file)
@@ -6,6 +6,7 @@
 package envcmd
 
 import (
+       "context"
        "encoding/json"
        "fmt"
        "go/build"
@@ -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")
        }
index 4d741df2b4f1a6a83c0a16705f69ef28f81163f2..f16af05fc866015acf69564880d791b394ff1a7f 100644 (file)
@@ -11,6 +11,7 @@ import (
        "cmd/go/internal/load"
        "cmd/go/internal/modload"
        "cmd/go/internal/str"
+       "context"
        "fmt"
        "os"
 )
@@ -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 {
index d6894edc9f500c32383cfb8799bd1df589807981..9868efc7efcd9132d9d57346887ff352ed2f89ff 100644 (file)
@@ -6,6 +6,7 @@
 package fmtcmd
 
 import (
+       "context"
        "errors"
        "fmt"
        "os"
@@ -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)
index 093b19817b5197fead3edcfeea2ffecde7508d87..fb26f77f9597924e1dd69053d5226f1d36fc6a26 100644 (file)
@@ -8,6 +8,7 @@ package generate
 import (
        "bufio"
        "bytes"
+       "context"
        "fmt"
        "go/parser"
        "go/token"
@@ -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 != "" {
index d38350c2a82aef1ce745d165c7a9b5913fead412..f7da5270b0da1f71602aba9d2ff695223feb93da 100644 (file)
@@ -6,6 +6,7 @@
 package get
 
 import (
+       "context"
        "fmt"
        "os"
        "path/filepath"
@@ -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")
index 6ca15611211540c5c0389740c9520f08924ac47d..ef0a5a2f2d53324d5472f68b6483bd42bc15970f 100644 (file)
@@ -8,6 +8,7 @@ package list
 import (
        "bufio"
        "bytes"
+       "context"
        "encoding/json"
        "io"
        "os"
@@ -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)
index 584434935b4bb838a0c81b7e42818946e7011052..b43c32be5a6b4f016985577d4001157d677b9055 100644 (file)
@@ -5,6 +5,7 @@
 package modcmd
 
 import (
+       "context"
        "encoding/json"
        "os"
 
@@ -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'")
index dbbfb96e42b5b2d81d509f751d10e4b7876f1a40..a81c25270fbae8e225521960001f3c12e0eff185 100644 (file)
@@ -8,6 +8,7 @@ package modcmd
 
 import (
        "bytes"
+       "context"
        "encoding/json"
        "errors"
        "fmt"
@@ -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 != "" ||
index 27ae9354f3d97792f8d2f11ed455bcfc4c34cebf..fff5b02626630bd25c1867c898da6003da3d549b 100644 (file)
@@ -8,6 +8,7 @@ package modcmd
 
 import (
        "bufio"
+       "context"
        "os"
        "sort"
 
@@ -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")
        }
index 714ff2e205ab7f572aa2246ca8c94885290af03c..ddb9aeebe93d656dcb7aa6a595397f558e6e68e1 100644 (file)
@@ -10,6 +10,7 @@ import (
        "cmd/go/internal/base"
        "cmd/go/internal/modload"
        "cmd/go/internal/work"
+       "context"
        "os"
        "strings"
 )
@@ -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")
index af2b04c0c20381ec72b0903df3d50b53c695f800..feb41a83b0c36b26edbc0e29cf4a421da7c28c2f 100644 (file)
@@ -12,6 +12,7 @@ import (
        "cmd/go/internal/modfetch"
        "cmd/go/internal/modload"
        "cmd/go/internal/work"
+       "context"
 
        "golang.org/x/mod/module"
 )
@@ -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")
        }
index 8509ceb7a8b948fab8a83bc1a00415e4ae955c5b..257d1cd0ef751758de61c29a5445a7c412881663 100644 (file)
@@ -6,6 +6,7 @@ package modcmd
 
 import (
        "bytes"
+       "context"
        "fmt"
        "io"
        "io/ioutil"
@@ -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")
        }
index b7fd7fa8e0194907160daa37fca0778797421473..570e5710491229d6988a67998634e03d411d152b 100644 (file)
@@ -6,6 +6,7 @@ package modcmd
 
 import (
        "bytes"
+       "context"
        "errors"
        "fmt"
        "io/ioutil"
@@ -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")
index 40d238519b20b09ab9d3495f5206bea8181e2c13..3f9cf0f12016edb42c99a705e84e8f6c7e0de8f8 100644 (file)
@@ -5,6 +5,7 @@
 package modcmd
 
 import (
+       "context"
        "fmt"
        "strings"
 
@@ -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
index 4c6982426fb0c4d34685d41ce523c1da55e44ec0..9836a3e2cc9df4e940c0a5fb9cfe1274c8a5af85 100644 (file)
@@ -6,6 +6,7 @@
 package modget
 
 import (
+       "context"
        "errors"
        "fmt"
        "os"
@@ -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
index 2edae38ccaa797d4851203a40a854799a2340cb5..ca2c3db92c1d8b244f086db45cff4d3cad5ae341 100644 (file)
@@ -6,6 +6,7 @@
 package run
 
 import (
+       "context"
        "fmt"
        "os"
        "path"
@@ -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()
index 873a76aa38037e0d6cc5c00acfc12ddca1d0e2ff..6648d4eab42e476d4ad8e6ba8f8f40345ddf264c 100644 (file)
@@ -6,6 +6,7 @@ package test
 
 import (
        "bytes"
+       "context"
        "crypto/sha256"
        "errors"
        "fmt"
@@ -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)
index 930eecb63f1c4955c842e6e7d10a936e77783254..7f4dc8680211c68c87b2cd4e5bbb1daefcbf7dce 100644 (file)
@@ -6,6 +6,7 @@
 package tool
 
 import (
+       "context"
        "fmt"
        "os"
        "os/exec"
@@ -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
index ac2ae501552926258ef63261be6fc0d92f8f170d..056db7bf9e72512971aa99e37dd371a896539ad4 100644 (file)
@@ -7,6 +7,7 @@ package version
 
 import (
        "bytes"
+       "context"
        "encoding/binary"
        "fmt"
        "os"
@@ -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")
index 4ec58de78544804139a669dc3bda4328073d43f2..717ff2d0aa263b14c6811b9fad269068dc6519b2 100644 (file)
@@ -10,6 +10,7 @@ import (
        "cmd/go/internal/load"
        "cmd/go/internal/modload"
        "cmd/go/internal/work"
+       "context"
        "path/filepath"
 )
 
@@ -48,7 +49,7 @@ See also: go fmt, go fix.
        `,
 }
 
-func runVet(cmd *base.Command, args []string) {
+func runVet(ctx context.Context, cmd *base.Command, args []string) {
        modload.LoadTests = true
 
        vetFlags, pkgArgs := vetFlags(args)
index fbd49b457b21d0f4ad9948cd0cce33a735fccf19..2bbee43ab414ed827555179396406672a1ad8657 100644 (file)
@@ -5,6 +5,7 @@
 package work
 
 import (
+       "context"
        "errors"
        "fmt"
        "go/build"
@@ -344,7 +345,7 @@ var pkgsFilter = func(pkgs []*load.Package) []*load.Package { return pkgs }
 
 var runtimeVersion = runtime.Version()
 
-func runBuild(cmd *base.Command, args []string) {
+func runBuild(ctx context.Context, cmd *base.Command, args []string) {
        BuildInit()
        var b Builder
        b.Init()
@@ -515,7 +516,7 @@ func libname(args []string, pkgs []*load.Package) (string, error) {
        return "lib" + libname + ".so", nil
 }
 
-func runInstall(cmd *base.Command, args []string) {
+func runInstall(ctx context.Context, cmd *base.Command, args []string) {
        BuildInit()
        InstallPackages(args, load.PackagesForBuild(args))
 }
index 3512866e4a0e567ed4fb5084e3508714dcfcaad0..37bb7d6d270bb76d7b44467ac86bde63c340a37a 100644 (file)
@@ -191,8 +191,7 @@ BigCmdLoop:
                        }
                        ctx := maybeStartTrace(context.Background())
                        ctx, span := trace.StartSpan(ctx, fmt.Sprint("Running ", cmd.Name(), " command"))
-                       _ = ctx
-                       cmd.Run(cmd, args)
+                       cmd.Run(ctx, cmd, args)
                        span.Done()
                        base.Exit()
                        return