goconfig

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2022 License: GPL-2.0-or-later Imports: 14 Imported by: 8

README

goconfig

Travis Build Status

Table of contents

  1. Introduction
  2. Usage
  3. Contributing
  4. Reporting bugs

1. Introduction

This project parses config files that have the same syntax as gitconfig files. It understands multiple values configuration, and can parse included configs by include.path directions (includeIf.*.path configuration is not supported yet).

It has no knowledge of git-specific keys and as such, does not provide any convenience methods like config.GetUserName(). For these, look into go-gitconfig

Most of the code was copied and translated to Go from git/config.c

2. Usage

To load specific git config file and inherit global and system git config, using:

package main

import (
	"fmt"
	"log"

	"github.com/jiangxin/goconfig"
)

func main() {
	cfg, err := goconfig.LoadAll("")
	if err != nil {
		log.Fatal(err)
	}
	if cfg == nil {
		log.Fatal("cfg is nil")
	}

	fmt.Printf(cfg.Get("user.name"))
}

As an example, there is a full functional git config to read/write git config file implemented by goconfig, see:

cmd/goconfig/main.go

3. Contributing

Contributions are welcome! Fork -> Push -> Pull request.

4. Bug report / suggestions

Just create an issue! I will try to reply as soon as possible.

Documentation

Index

Examples

Constants

View Source
const (
	ScopeInclude = 1 << iota
	ScopeSystem
	ScopeGlobal
	ScopeSelf

	ScopeAll  = 0xFFFF
	ScopeMask = ^ScopeInclude
)

Define scopes for config variables

Variables

View Source
var ErrInvalidEscapeSequence = errors.New("unknown escape sequence")

ErrInvalidEscapeSequence indicates that the escape character ('\') was followed by an invalid character.

View Source
var ErrInvalidKeyChar = errors.New("invalid key character")

ErrInvalidKeyChar indicates that there was an invalid key character

View Source
var ErrInvalidSectionChar = errors.New("invalid character in section")

ErrInvalidSectionChar indicates that there was an invalid character in section

View Source
var ErrMissingClosingBracket = errors.New("missing closing section bracket")

ErrMissingClosingBracket indicates that there was a missing closing bracket in section

View Source
var ErrMissingEquals = errors.New("expected '='")

ErrMissingEquals indicates that an equals sign ('=') was expected but not found

View Source
var ErrMissingStartQuote = errors.New("missing start quote")

ErrMissingStartQuote indicates that there was a missing start quote

View Source
var ErrNotBoolValue = errors.New("not a bool value")

ErrNotBoolValue indicates fail to convert config variable to bool

View Source
var ErrNotExist = errors.New("config file or dir not exist")

ErrNotExist indicates file or dir not exist

View Source
var ErrNotInGitDir = errors.New("not in a git dir")

ErrNotInGitDir indicates not in a git dir

View Source
var ErrPartialBOM = errors.New("partial UTF8-BOM")

ErrPartialBOM indicates that the file begins with a partial UTF8-BOM

View Source
var ErrSectionNewLine = errors.New("newline in section")

ErrSectionNewLine indicates that there was a newline in section

View Source
var ErrUnexpectedEOF = errors.New("unexpected EOF")

ErrUnexpectedEOF indicates that there was an unexpected EOF

View Source
var ErrUnfinishedQuote = errors.New("unfinished quote")

ErrUnfinishedQuote indicates that a value has an odd number of (unescaped) quotes

Functions

func CacheSet added in v1.1.0

func CacheSet(key string, cfg GitConfig, size int64, modTime time.Time)

CacheSet will set cache entry

func Exist added in v1.1.0

func Exist(name string) bool

Exist check if path is exist.

func FindGitConfig

func FindGitConfig(dir string) (string, error)

FindGitConfig returns local git config file

func GlobalConfigFile

func GlobalConfigFile() (string, error)

GlobalConfigFile returns global git config file

func IsDir added in v1.1.0

func IsDir(name string) bool

IsDir returns true if path is exist and is a directory.

func IsFile added in v1.1.0

func IsFile(name string) bool

IsFile returns true if path is exist and is a file.

func SystemConfigFile

func SystemConfigFile() string

SystemConfigFile returns system git config file

Types

type GitConfig

type GitConfig map[string]gitConfigKeys

GitConfig maps section to key-value pairs

func CacheGet added in v1.1.0

func CacheGet(key string) (GitConfig, bool)

CacheGet returns git config if config file is up-to-date

func DefaultConfig added in v1.1.0

func DefaultConfig() GitConfig

DefaultConfig returns global and system wide config

func GlobalConfig

func GlobalConfig() (GitConfig, error)

GlobalConfig returns global user config, reload if necessary

func Load

func Load(name string) (GitConfig, error)

Load only loads one file or config of current repository

func LoadAll

func LoadAll(name string) (GitConfig, error)

LoadAll will load additional global and system config files

func NewGitConfig

func NewGitConfig() GitConfig

NewGitConfig returns GitConfig with initialized maps

func Parse

func Parse(bytes []byte, filename string) (GitConfig, uint, error)

Parse takes given bytes as configuration file (according to gitconfig syntax)

Example
gitconfig := "configs/danyel.gitconfig"
bytes, err := ioutil.ReadFile(gitconfig)
if err != nil {
	log.Fatalf("Couldn't read file %v\n", gitconfig)
}

config, lineno, err := Parse(bytes, gitconfig)
if err != nil {
	log.Fatalf("Error on line %d: %v\n", lineno, err)
}
fmt.Println()
fmt.Println(lineno)
fmt.Println(config.Get("user.name"))
fmt.Println(config.Get("user.email"))
Output:

10
Danyel Bayraktar
cydrop@gmail.com

func SystemConfig

func SystemConfig() (GitConfig, error)

SystemConfig returns system git config, reload if necessary

func (GitConfig) Add

func (v GitConfig) Add(key string, value interface{})

Add will add user input key-value pair

func (GitConfig) Get

func (v GitConfig) Get(key string) string

Get value from key

func (GitConfig) GetAll

func (v GitConfig) GetAll(key string) []string

GetAll gets all values of a key

func (GitConfig) GetBool

func (v GitConfig) GetBool(key string, defaultValue bool) bool

GetBool gets boolean from key with default value

func (GitConfig) GetBoolE added in v1.1.0

func (v GitConfig) GetBoolE(key string, defaultValue bool) (bool, error)

GetBoolE gets boolean from key with default value with error

func (GitConfig) GetInt

func (v GitConfig) GetInt(key string, defaultValue int) int

GetInt return integer value of key with default

func (GitConfig) GetInt64

func (v GitConfig) GetInt64(key string, defaultValue int64) int64

GetInt64 return int64 value of key with default

func (GitConfig) GetInt64E added in v1.1.0

func (v GitConfig) GetInt64E(key string, defaultValue int64) (int64, error)

GetInt64E return int64 value of key with default with error

func (GitConfig) GetIntE added in v1.1.0

func (v GitConfig) GetIntE(key string, defaultValue int) (int, error)

GetIntE return integer value of key with default with error

func (GitConfig) GetUint64

func (v GitConfig) GetUint64(key string, defaultValue uint64) uint64

GetUint64 return uint64 value of key with default

func (GitConfig) GetUint64E added in v1.1.0

func (v GitConfig) GetUint64E(key string, defaultValue uint64) (uint64, error)

GetUint64E return uint64 value of key with default with error

func (GitConfig) HasKey added in v1.1.0

func (v GitConfig) HasKey(key string) bool

HasKey checks whether key is set

func (GitConfig) Keys

func (v GitConfig) Keys() []string

Keys returns all config variable keys (in lower case)

func (GitConfig) Merge

func (v GitConfig) Merge(c GitConfig, scope scope) GitConfig

Merge will merge another GitConfig, and new value(s) of the same key will append to the end of value list, and new value has higher priority.

func (GitConfig) Save

func (v GitConfig) Save(file string) error

Save will save git config to file

func (GitConfig) Sections

func (v GitConfig) Sections() []string

Sections returns sorted sections

func (GitConfig) Set

func (v GitConfig) Set(key string, value interface{})

Set will replace old config variable

func (GitConfig) String

func (v GitConfig) String() string

String returns content of GitConfig ready to save config file

func (GitConfig) Unset

func (v GitConfig) Unset(key string)

Unset will remove latest setting of a config variable

func (GitConfig) UnsetAll

func (v GitConfig) UnsetAll(key string)

UnsetAll will remove all settings of a config variable

type Repository added in v1.1.0

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

Repository defines struct for a Git repository.

func FindRepository added in v1.1.0

func FindRepository(dir string) (*Repository, error)

FindRepository locates repository object search from the given dir.

func (Repository) Config added in v1.1.0

func (v Repository) Config() GitConfig

Config returns git config object

func (Repository) GitCommonDir added in v1.1.0

func (v Repository) GitCommonDir() string

GitCommonDir returns commondir where contains "config" file

func (Repository) GitDir added in v1.1.0

func (v Repository) GitDir() string

GitDir returns GitDir

func (Repository) IsBare added in v1.1.0

func (v Repository) IsBare() bool

IsBare indicates a repository is a bare repository.

func (Repository) WorkDir added in v1.1.0

func (v Repository) WorkDir() string

WorkDir returns workdir

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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