gocommand

package
v0.0.0-...-68c7731 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package gocommand is a helper for calling the go command.

Index

Constants

This section is empty.

Variables

View Source
var DebugHangingGoCommands = false

DebugHangingGoCommands may be set by tests to enable additional instrumentation (including panics) for debugging hanging Go commands.

See golang/go#54461 for details.

Functions

func GoVersion

func GoVersion(ctx context.Context, inv Invocation, r *Runner) (int, error)

GoVersion reports the minor version number of the highest release tag built into the go command on the PATH.

Note that this may be higher than the version of the go tool used to build this application, and thus the versions of the standard go/{scanner,parser,ast,types} packages that are linked into it. In that case, callers should either downgrade to the version of go used to build the application, or report an error that the application is too old to use the go command on the PATH.

func GoVersionOutput

func GoVersionOutput(ctx context.Context, inv Invocation, r *Runner) (string, error)

GoVersionOutput returns the complete output of the go version command.

func HandleHangingGoCommand

func HandleHangingGoCommand(start time.Time, cmd *exec.Cmd)

func ParseGoVersionOutput

func ParseGoVersionOutput(data string) string

ParseGoVersionOutput extracts the Go version string from the output of the "go version" command. Given an unrecognized form, it returns an empty string.

func WriteOverlays

func WriteOverlays(overlay map[string][]byte) (filename string, cleanup func(), err error)

WriteOverlays writes each value in the overlay (see the Overlay field of go/packages.Config) to a temporary file and returns the name of a JSON file describing the mapping that is suitable for the "go list -overlay" flag.

On success, the caller must call the cleanup function exactly once when the files are no longer needed.

Types

type Invocation

type Invocation struct {
	Verb       string
	Args       []string
	BuildFlags []string

	// If ModFlag is set, the go command is invoked with -mod=ModFlag.
	// TODO(rfindley): remove, in favor of Args.
	ModFlag string

	// If ModFile is set, the go command is invoked with -modfile=ModFile.
	// TODO(rfindley): remove, in favor of Args.
	ModFile string

	// Overlay is the name of the JSON overlay file that describes
	// unsaved editor buffers; see [WriteOverlays].
	// If set, the go command is invoked with -overlay=Overlay.
	// TODO(rfindley): remove, in favor of Args.
	Overlay string

	// If CleanEnv is set, the invocation will run only with the environment
	// in Env, not starting with os.Environ.
	CleanEnv   bool
	Env        []string
	WorkingDir string
	Logf       func(format string, args ...interface{})
}

An Invocation represents a call to the go command.

type ModuleJSON

type ModuleJSON struct {
	Path      string      // module path
	Version   string      // module version
	Versions  []string    // available module versions (with -versions)
	Replace   *ModuleJSON // replaced by this module
	Time      *time.Time  // time version was created
	Update    *ModuleJSON // available update, if any (with -u)
	Main      bool        // is this the main module?
	Indirect  bool        // is this module only an indirect dependency of main module?
	Dir       string      // directory holding files for this module, if any
	GoMod     string      // path to go.mod file used when loading this module, if any
	GoVersion string      // go version used in module
}

ModuleJSON holds information about a module.

func VendorEnabled

func VendorEnabled(ctx context.Context, inv Invocation, r *Runner) (bool, *ModuleJSON, error)

VendorEnabled reports whether vendoring is enabled. It takes a *Runner to execute Go commands with the supplied context.Context and Invocation. The Invocation can contain pre-defined fields, of which only Verb and Args are modified to run the appropriate Go command. Inspired by setDefaultBuildMod in modload/init.go

func WorkspaceVendorEnabled

func WorkspaceVendorEnabled(ctx context.Context, inv Invocation, r *Runner) (bool, []*ModuleJSON, error)

WorkspaceVendorEnabled reports whether workspace vendoring is enabled. It takes a *Runner to execute Go commands with the supplied context.Context and Invocation. The Invocation can contain pre-defined fields, of which only Verb and Args are modified to run the appropriate Go command. Inspired by setDefaultBuildMod in modload/init.go

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

An Runner will run go command invocations and serialize them if it sees a concurrency error.

func (*Runner) Run

func (runner *Runner) Run(ctx context.Context, inv Invocation) (*bytes.Buffer, error)

Run is a convenience wrapper around RunRaw. It returns only stdout and a "friendly" error.

func (*Runner) RunPiped

func (runner *Runner) RunPiped(ctx context.Context, inv Invocation, stdout, stderr io.Writer) error

RunPiped runs the invocation serially, always waiting for any concurrent invocations to complete first.

func (*Runner) RunRaw

func (runner *Runner) RunRaw(ctx context.Context, inv Invocation) (*bytes.Buffer, *bytes.Buffer, error, error)

RunRaw runs the invocation, serializing requests only if they fight over go.mod changes. Postcondition: both error results have same nilness.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL