goconfig

package module
v0.0.0-...-0a63550 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2018 License: GPL-2.0-or-later Imports: 1 Imported by: 7

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 is a minimal parser, and maybe a writer sometime in the future. 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

Currently, there is only one function: Parse.

import "os/user"
import "path/filepath"
import "io/ioutil"
import "github.com/muja/goconfig"

user, _ := user.Current()
// don't forget to handle error!
gitconfig := filepath.Join(user.HomeDir, ".gitconfig")
bytes, _ := ioutil.ReadFile(gitconfig)

config, lineno, err := goconfig.Parse(bytes)
if err != nil {
  // Note: config is non-nil and contains successfully parsed values
  log.Fatalf("Error on line %d: %v.\n", err)
}
fmt.Println(config["user.name"])
fmt.Println(config["user.email"])

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

This section is empty.

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 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 Parse

func Parse(bytes []byte) (map[string]string, 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)
if err != nil {
	log.Fatalf("Error on line %d: %v\n", lineno, err)
}
fmt.Println()
fmt.Println(lineno)
fmt.Println(config["user.name"])
fmt.Println(config["user.email"])
Output:

10
Danyel Bayraktar
cydrop@gmail.com

Types

This section is empty.

Jump to

Keyboard shortcuts

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