go-gitconfig: github.com/motemen/go-gitconfig Index | Examples | Files

package gitconfig

import "github.com/motemen/go-gitconfig"

Package gitconfig is an interface to "git config" for reading values.

The GetXXX methods return values of their corresponding types, or errors if "git config" fails e.g. when the key was invalid.

Use Default (or gitconfig package itself), Global, Local, File(file) and Blob(blob) as an entrypoint to a specific config source. They correspond to flags below:

Default    (none)
Global     --global
Local      --local
File(file) --file <file>
Blob(blob) --blob <blob>

Use Load method for loading multiple config values to a struct with fields tagged "gitconfig".

type Config struct {
  UserEmail  string `gitconfig:"user.email"`
  PullRebase bool   `gitconfig:"pull.rebase"`
}

Supported types are string, []string, bool and int families.

Index

Examples

Package Files

default.go gitconfig.go

Variables

var (
    // SourceDefault is a default source (local, global and system).
    SourceDefault Source
    // SourceGlobal looks into global git config (eg. ~/.gitconfig).
    SourceGlobal = []string{"--global"}
    // SourceLocal looks into local git config (eg. .git/config).
    SourceLocal = []string{"--local"}
)
var (
    // Default reads git config from default source i.e. local, global and system
    Default = Config{}
    // Global reads git config from global source (eg. ~/.gitconfig).
    Global = Config{Source: SourceGlobal}
    // Local reads git config from local source (eg. .git/config).
    Local = Config{Source: SourceLocal}
)

func GetBool Uses

func GetBool(key string) (bool, error)

GetBool is a shortcut for Default.Bool.

func GetInt64 Uses

func GetInt64(key string) (int64, error)

GetInt64 is a shortcut for Default.Int64.

func GetPath Uses

func GetPath(key string) (string, error)

GetPath is a shortcut for Default.Path.

func GetPaths Uses

func GetPaths(key string) ([]string, error)

GetPaths is a shortcut for Default.Paths.

func GetString Uses

func GetString(key string) (string, error)

GetString is a shortcut for Default.GetString.

func GetStrings Uses

func GetStrings(key string) ([]string, error)

GetStrings is a shortcut for Default.GetStrings.

func IsInvalidKeyError Uses

func IsInvalidKeyError(err error) bool

IsInvalidKeyError returns true if the given err is a RunError corresponding to "invalid key" error of "git config".

func Load Uses

func Load(v interface{}) error

Load is a shortcut for Default.Load.

Code:

type config struct {
    UserEmail  string `gitconfig:"user.email"`
    PullRebase bool   `gitconfig:"pull.rebase"`
    GCAuto     int    `gitconfig:"gc.auto"`
}

var v config
gitconfig.Load(&v)

type Config Uses

type Config struct {
    Source Source
    Dir    string
}

Config is the main interface of gitconfig package.

func Blob Uses

func Blob(blob string) Config

Blob reads git config from specified blob.

func File Uses

func File(file string) Config

File reads git config from specified file.

func (Config) GetBool Uses

func (c Config) GetBool(key string) (bool, error)

GetBool obtains one boolean value.

func (Config) GetInt64 Uses

func (c Config) GetInt64(key string) (int64, error)

GetInt64 obtains one integer value.

func (Config) GetPath Uses

func (c Config) GetPath(key string) (string, error)

GetPath obtains one path value. eg. "~" expands to home directory.

func (Config) GetPaths Uses

func (c Config) GetPaths(key string) ([]string, error)

GetPaths obtains multiple path values.

func (Config) GetString Uses

func (c Config) GetString(key string) (string, error)

GetString obtains one string value.

func (Config) GetStrings Uses

func (c Config) GetStrings(key string) ([]string, error)

GetStrings obtains multiple string values.

func (Config) Load Uses

func (c Config) Load(v interface{}) error

Load loads git config values to a struct annotated with "gitconfig" tags.

type LoadError Uses

type LoadError map[string]error

LoadError is an error type for Load().

func (LoadError) Error Uses

func (m LoadError) Error() string

func (LoadError) OfField Uses

func (m LoadError) OfField(name string) error

OfField returns the error corresponding to a given field.

type RunError Uses

type RunError struct {
    Msg string
    Err *exec.ExitError
}

RunError is a general error for "git config" failure.

func (RunError) Error Uses

func (err RunError) Error() string

type Source Uses

type Source []string

Source is a source (global, local, file...) of git config.

func SourceBlob Uses

func SourceBlob(blob string) Source

SourceBlob is a source that looks into a specified blob (eg. HEAD:.gitmodules)

func SourceFile Uses

func SourceFile(file string) Source

SourceFile is a source that looks into a specified file.

Package gitconfig imports 7 packages (graph) and is imported by 1 packages. Updated 2016-07-20. Refresh now. Tools for package owners.