base

package standard library
go1.22.9 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2024 License: BSD-3-Clause Imports: 21 Imported by: 0

Documentation

Overview

Package base defines shared basic pieces of the go command, in particular logging and the Command structure.

Index

Constants

This section is empty.

Variables

View Source
var Go = &Command{
	UsageLine: "go",
	Long:      `Go is a tool for managing Go source code.`,
}
View Source
var Interrupted = make(chan struct{})

Interrupted is closed when the go command receives an interrupt signal.

View Source
var NetLimitGodebug = godebug.New("#cmdgonetlimit")
View Source
var SignalTrace os.Signal = syscall.SIGQUIT

SignalTrace is the signal to send to make a Go program crash with a stack trace.

View Source
var Usage func()

Usage is the usage-reporting function, filled in by package main but here for reference by other packages.

Functions

func AcquireNet added in go1.21.0

func AcquireNet() (release func(), err error)

AcquireNet acquires a semaphore token for a network operation.

func AddBuildFlagsNX

func AddBuildFlagsNX(flags *flag.FlagSet)

AddBuildFlagsNX adds the -n and -x build flags to the flag set.

func AddChdirFlag added in go1.20

func AddChdirFlag(flags *flag.FlagSet)

AddChdirFlag adds the -C flag to the flag set.

func AddModCommonFlags added in go1.16

func AddModCommonFlags(flags *flag.FlagSet)

AddModCommonFlags adds the module-related flags common to build commands and 'go mod' subcommands.

func AddModFlag added in go1.16

func AddModFlag(flags *flag.FlagSet)

AddModFlag adds the -mod build flag to the flag set.

func AppendPATH added in go1.19

func AppendPATH(base []string) []string

AppendPATH returns the result of appending PATH=$GOROOT/bin:$PATH (or the platform equivalent) to the environment base.

func AppendPWD added in go1.15

func AppendPWD(base []string, dir string) []string

AppendPWD returns the result of appending PWD=dir to the environment base.

The resulting environment makes os.Getwd more efficient for a subprocess running in dir, and also improves the accuracy of paths relative to dir if one or more elements of dir is a symlink.

func AtExit

func AtExit(f func())

func ChdirFlag added in go1.21.0

func ChdirFlag(s string) error

func Cwd

func Cwd() string

Cwd returns the current working directory at the time of the first call.

func Error added in go1.21.0

func Error(err error)

func Errorf

func Errorf(format string, args ...any)

func Exit

func Exit()

func ExitIfErrors

func ExitIfErrors()

func Fatal added in go1.21.0

func Fatal(err error)

func Fatalf

func Fatalf(format string, args ...any)

func GOFLAGS added in go1.11

func GOFLAGS() []string

GOFLAGS returns the flags from $GOFLAGS. The list can be assumed to contain one string per flag, with each string either beginning with -name or --name.

func GetExitStatus added in go1.13

func GetExitStatus() int

func InGOFLAGS added in go1.15.3

func InGOFLAGS(flag string) bool

InGOFLAGS returns whether GOFLAGS contains the given flag, such as "-mod".

func InitGOFLAGS added in go1.11

func InitGOFLAGS()

InitGOFLAGS initializes the goflags list from $GOFLAGS. If goflags is already initialized, it does nothing.

func IsNull added in go1.20

func IsNull(path string) bool

IsNull reports whether the path is a common name for the null device. It returns true for /dev/null on Unix, or NUL (case-insensitive) on Windows.

func IsTestFile

func IsTestFile(file string) bool

IsTestFile reports whether the source file is a set of tests and should therefore be excluded from coverage analysis.

func NetLimit added in go1.21.0

func NetLimit() (int, bool)

NetLimit returns the limit on concurrent network operations configured by GODEBUG=cmdgonetlimit, if any.

A limit of 0 (indicated by 0, true) means that network operations should not be allowed.

func RelPaths

func RelPaths(paths []string) []string

RelPaths returns a copy of paths with absolute paths made relative to the current directory if they would be shorter.

func Run

func Run(cmdargs ...any)

Run runs the command, with stdout and stderr connected to the go command's own stdout and stderr. If the command fails, Run reports the error using Errorf.

func RunStdin

func RunStdin(cmdline []string)

RunStdin is like run but connects Stdin.

func SetExitStatus

func SetExitStatus(n int)

func SetFromGOFLAGS added in go1.11

func SetFromGOFLAGS(flags *flag.FlagSet)

SetFromGOFLAGS sets the flags in the given flag set using settings in $GOFLAGS.

func ShortPath

func ShortPath(path string) string

ShortPath returns an absolute or relative name for path, whatever is shorter.

func StartSigHandlers

func StartSigHandlers()

StartSigHandlers starts the signal handlers.

func Tool

func Tool(toolName string) string

Tool returns the path to the named tool (for example, "vet"). If the tool cannot be found, Tool exits the process.

func ToolPath added in go1.21.0

func ToolPath(toolName string) (string, error)

Tool returns the path at which we expect to find the named tool (for example, "vet"), and the error (if any) from statting that path.

func UncachedCwd added in go1.21.0

func UncachedCwd() string

UncachedCwd returns the current working directory. Most callers should use Cwd, which caches the result for future use. UncachedCwd is appropriate to call early in program startup before flag parsing, because the -C flag may change the current directory.

Types

type Command

type Command struct {
	// Run runs the command.
	// The args are the arguments after the command name.
	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.
	UsageLine string

	// Short is the short description shown in the 'go help' output.
	Short string

	// Long is the long message shown in the 'go help <this-command>' output.
	Long string

	// Flag is a set of flags specific to this command.
	Flag flag.FlagSet

	// CustomFlags indicates that the command will do its own
	// flag parsing.
	CustomFlags bool

	// Commands lists the available commands and help topics.
	// The order here is the order in which they are printed by 'go help'.
	// Note that subcommands are in general best avoided.
	Commands []*Command
}

A Command is an implementation of a go command like go build or go fix.

func (*Command) LongName added in go1.11

func (c *Command) LongName() string

LongName returns the command's long name: all the words in the usage line between "go" and a flag or argument,

func (*Command) Lookup added in go1.21.0

func (c *Command) Lookup(name string) *Command

Lookup returns the subcommand with the given name, if any. Otherwise it returns nil.

Lookup ignores subcommands that have len(c.Commands) == 0 and c.Run == nil. Such subcommands are only for use as arguments to "help".

func (*Command) Name

func (c *Command) Name() string

Name returns the command's short name: the last word in the usage line before a flag or argument.

func (*Command) Runnable

func (c *Command) Runnable() bool

Runnable reports whether the command can be run; otherwise it is a documentation pseudo-command such as importpath.

func (*Command) Usage

func (c *Command) Usage()

type StringsFlag

type StringsFlag []string

A StringsFlag is a command-line flag that interprets its argument as a space-separated list of possibly-quoted strings.

func (*StringsFlag) Set

func (v *StringsFlag) Set(s string) error

func (*StringsFlag) String

func (v *StringsFlag) String() string

Jump to

Keyboard shortcuts

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