Documentation
¶
Overview ¶
Package gitconfig implements a pure Go parser of Git SCM config files. The support is currently not matching git exactly, e.g. includes, urlmatches and multivars are currently not supported. And while we try to preserve the original file a much as possible when writing we currently don't exactly retain (insignificant) whitespaces.
The reference for this implementation is https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-config.html
Usage ¶
Use gitconfig.LoadAll with an optional workspace argument to process configuration input from these locations in order (i.e. the later ones take precedence):
- `system` - /etc/gitconfig
- `global` - `$XDG_CONFIG_HOME/git/config` or `~/.gitconfig`
- `local` - `<workdir>/config`
- `worktree` - `<workdir>/config.worktree`
- `command` - GIT_CONFIG_{COUNT,KEY,VALUE} environment variables
Note: We do not support parsing command line flags directly, but one can use the SetEnv method to set flags from the command line in the config.
Customization ¶
`gopass` and other users of this package can easily customize file and environment names by utilizing the exported variables from the Configs struct:
- SystemConfig
- GlobalConfig (can be set to the empty string to disable)
- LocalConfig
- WorktreeConfig
- EnvPrefix
Note: For tests users will want to set `NoWrites = true` to avoid overwriting their real configs.
Example
import "github.com/gopasspw/gopass/pkg/gitconfig" func main() { cfg := gitconfig.New() cfg.SystemConfig = "/etc/gopass/config" cfg.GlobalConfig = "" cfg.EnvPrefix = "GOPASS_CONFIG" cfg.LoadAll(".") _ = cfg.Get("core.notifications") }
Versioning and Compatibility ¶
We aim to support the latest stable release of Git only. Currently we do not provide any backwards compatibility and semantic versioning. Once this package has become mostly feature complete and if there is interest from other projects in using it we may choose to move it to it's own repository and start proper versioning.
Known limitations ¶
* Worktree support is only partial
Index ¶
- type Config
- type Configs
- func (cs *Configs) Get(key string) string
- func (cs *Configs) GetAll(key string) []string
- func (cs *Configs) GetGlobal(key string) string
- func (cs *Configs) GetLocal(key string) string
- func (cs *Configs) HasGlobalConfig() bool
- func (cs *Configs) IsSet(key string) bool
- func (cs *Configs) Keys() []string
- func (cs *Configs) List(prefix string) []string
- func (cs *Configs) ListSections() []string
- func (cs *Configs) ListSubsections(wantSection string) []string
- func (cs *Configs) LoadAll(workdir string) *Configs
- func (cs *Configs) Reload()
- func (cs *Configs) SetEnv(key, value string) error
- func (cs *Configs) SetGlobal(key, value string) error
- func (cs *Configs) SetLocal(key, value string) error
- func (cs *Configs) String() string
- func (cs *Configs) UnsetGlobal(key string) error
- func (cs *Configs) UnsetLocal(key string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config is a single parsed config file. It contains a reference of the input file, if any. It can only be populated only by reading the environment variables.
func LoadConfig ¶
LoadConfig tries to load a gitconfig from the given path.
func LoadConfigFromEnv ¶
LoadConfigFromEnv will try to parse an overlay config from the environment variables. If no environment variables are set the resulting config will be valid but empty. Either way it will not be writeable.
func NewFromMap ¶
NewFromMap allows creating a new preset config from a map.
func ParseConfig ¶
ParseConfig will try to parse a gitconfig from the given io.Reader. It never fails. Invalid configs will be silently rejected.
func (*Config) IsEmpty ¶ added in v1.15.10
IsEmpty returns true if the config is empty (typically a newly initialized config, but still unused). Since gitconfig.New() already sets the global path to the globalConfigFile() one, we cannot rely on the path being set to checki this. We need to check the raw length to be sure it wasn't just the default empty config struct.
type Configs ¶
type Configs struct { Preset *Config Name string SystemConfig string GlobalConfig string LocalConfig string WorktreeConfig string EnvPrefix string NoWrites bool // contains filtered or unexported fields }
Configs is a container for a config "view" that is composed of several different config objects. The intention is for the ones with a wider scope to provide defaults so those with a more narrow scope then only have to override what they are interested in.
func (*Configs) Get ¶
Get returns the value for the given key from the first location that is found. Lookup order: env, worktree, local, global, system and presets.
func (*Configs) GetAll ¶ added in v1.15.3
GetAll returns all values for the given key from the first location that is found. See the description of Get for more details.
func (*Configs) HasGlobalConfig ¶ added in v1.15.1
HasGlobalConfig indicates if a per-user config can be found.
func (*Configs) Keys ¶
Keys returns a list of all keys from all available scopes. Every key has section and possibly a subsection. They are separated by dots. The subsection itself may contain dots. The final key name and the section MUST NOT contain dots.
Examples
- remote.gist.gopass.pw.path -> section: remote, subsection: gist.gopass.pw, key: path
- core.timeout -> section: core, key: timeout
func (*Configs) List ¶
List returns all keys matching the given prefix. The prefix can be empty, then this is identical to Keys().
func (*Configs) ListSections ¶
ListSections returns a sorted list of all sections.
func (*Configs) ListSubsections ¶
ListSubsections returns a sorted list of all subsections in the given section.
func (*Configs) LoadAll ¶
LoadAll tries to load all known config files. Missing or invalid files are silently ignored. It never fails. The workdir is optional. If non-empty this method will try to load a local config from this location.
func (*Configs) SetEnv ¶
SetEnv sets (or adds) a key in the per-process (env) config. Useful for persisting flags during the invocation.
func (*Configs) UnsetGlobal ¶
UnsetGlobal deletes a key from the global config.
func (*Configs) UnsetLocal ¶
UnsetLocal deletes a key from the local config.