Help us shape the future of Go! Take the 2026 Go Developer Survey

config

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package config resolves gotools' configuration.

A value can come from three places. In increasing priority:

  1. the built-in default,
  2. the manifest file, .gotools.json in the project root,
  3. the environment, GOTOOLS_*,
  4. overrides the caller passes, such as command line flags.

Load stacks the three as koanf layers, so a later layer overrides an earlier one key by key, and reads the result into a Config. The manifest is read as JSON by koanf, so Config mirrors its shape: the layout keys at the top level and the settings in a "settings" object. Nothing here reads the process environment; the caller passes it in.

Index

Constants

View Source
const (
	EnvVarStrategy          = "GOTOOLS_STRATEGY"
	EnvVarDir               = "GOTOOLS_DIR"
	EnvVarGoVersion         = "GOTOOLS_GO_VERSION"
	EnvVarModulePrefix      = "GOTOOLS_MODULE_PREFIX"
	EnvVarOffline           = "GOTOOLS_OFFLINE"
	EnvVarJobs              = "GOTOOLS_JOBS"
	EnvVarOperationTimeout  = "GOTOOLS_OPERATION_TIMEOUT"
	EnvVarLockTimeout       = "GOTOOLS_LOCK_TIMEOUT"
	EnvVarNoLock            = "GOTOOLS_NO_LOCK"
	EnvVarTrace             = "GOTOOLS_TRACE"
	EnvVarVerbose           = "GOTOOLS_VERBOSE"
	KeyStrategy             = "strategy"
	KeyDir                  = "dir"
	KeyGoVersion            = "go_version"
	KeyModulePrefix         = "module_prefix"
	KeyOffline              = "settings.offline"
	KeyJobs                 = "settings.jobs"
	KeyOperationTimeout     = "settings.operation_timeout"
	KeyLockTimeout          = "settings.lock_timeout"
	KeyNoLock               = "settings.no_lock"
	KeyTrace                = "settings.trace"
	KeyVerbose              = "settings.verbose"
	DefaultStrategy         = StrategySplit
	DefaultDir              = "tools"
	DefaultGoVersion        = "inherit"
	DefaultModulePrefix     = ""
	DefaultOffline          = false
	DefaultJobs             = 1
	DefaultOperationTimeout = 120
	DefaultLockTimeout      = 10
	DefaultNoLock           = false
	DefaultTrace            = TraceOff
	DefaultVerbose          = false
)

The environment variables.

View Source
const (
	StrategySplit   = strategy.Split
	StrategyModule  = strategy.Module
	StrategyUnified = strategy.Unified
)
View Source
const DefaultLockStaleTimeout = 300

DefaultLockStaleTimeout is the value reported for EnvVarLockStaleTimeout.

View Source
const EnvPrefix = "GOTOOLS_"

EnvPrefix starts the name of every environment variable gotools reads.

View Source
const EnvVarLockStaleTimeout = "GOTOOLS_LOCK_STALE_TIMEOUT"

EnvVarLockStaleTimeout is accepted by Keys for compatibility. Nothing reads it, since the lock is released by the kernel when its holder dies.

View Source
const ManifestFile = ".gotools.json"

ManifestFile is the manifest's name in the project root.

Variables

Keys lists the GOTOOLS_* names a user can read and change, in help order. The first four are layout keys; the rest are settings.

Functions

func FormatSetting

func FormatSetting(name string, s Settings) (string, bool)

FormatSetting returns the settings key name of s in the form it is written and accepted, and false when name is not a settings key.

func IsKey

func IsKey(name string) bool

IsKey reports whether name is one of Keys.

func IsSetting

func IsSetting(name string) bool

IsSetting reports whether name is a key of the settings block, as opposed to a layout key.

func ParseSetting

func ParseSetting(name, value string, s *Settings) error

ParseSetting applies value to the settings key name in s, checking it against the type the setting has: a boolean, a positive number of jobs, a non-negative number of seconds, or a trace mode. Booleans are true, false, 1 or 0; trace also takes stdout. A value that does not fit is a usage error and leaves s unchanged.

func ValidateDir

func ValidateDir(dir string) error

ValidateDir reports whether dir may serve as the tools directory. It must be a relative path that stays inside the project and is not the project root, since everything in it is created, rewritten and removed.

Types

type Config

type Config struct {
	Layout   `koanf:",squash"`
	Settings Settings `koanf:"settings"`
}

Config is the resolved configuration, shaped like the manifest.

func Load

func Load(root string, environ []string, opts ...Option) (Config, error)

Load resolves the configuration of the project at root. environ lists the environment as NAME=value pairs, like os.Environ; nil means no variables.

A project without a manifest is configured by the defaults and the environment alone. A manifest that is not valid JSON, a value of the wrong type, or a value that fails Validate is an error.

func (Config) Validate

func (c Config) Validate() error

Validate reports every value that is out of range.

type Layout

type Layout struct {
	Strategy     Strategy `koanf:"strategy"`
	Dir          string   `koanf:"dir"`
	GoVersion    string   `koanf:"go_version"`
	ModulePrefix string   `koanf:"module_prefix"`
}

Layout says where the tools live and how they are isolated.

type Option

type Option func(*loadOptions)

Option adjusts how Load resolves the configuration.

func WithOverrides

func WithOverrides(o Overrides) Option

WithOverrides adds a layer above the environment: default < manifest < environment < overrides.

func WithoutManifest

func WithoutManifest() Option

WithoutManifest leaves the manifest out: the configuration is the defaults, the environment and the overrides. A diagnostic uses it to still resolve a layout when the manifest cannot be read.

type Overrides

type Overrides struct {
	Strategy     Strategy
	Dir          string
	GoVersion    string
	ModulePrefix string
}

Overrides are values that outrank every other layer, such as command line flags. An empty field is not an override.

type Settings

type Settings struct {
	Offline          bool      `koanf:"offline"`
	Jobs             int       `koanf:"jobs"`
	OperationTimeout int       `koanf:"operation_timeout"`
	LockTimeout      int       `koanf:"lock_timeout"`
	NoLock           bool      `koanf:"no_lock"`
	Trace            TraceMode `koanf:"trace"`
	Verbose          bool      `koanf:"verbose"`
}

Settings are the runtime knobs. Durations are in seconds.

type Strategy

type Strategy = strategy.Strategy

Strategy is how tools are kept out of the project's own module graph.

type TraceMode

type TraceMode string

TraceMode says where trace records are echoed. They are always written to the trace file as well.

const (
	TraceOff    TraceMode = "false"
	TraceStderr TraceMode = "true"
	TraceStdout TraceMode = "stdout"
)

Jump to

Keyboard shortcuts

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