This commit is contained in:
Morgan Bazalgette
2017-01-14 18:42:10 +01:00
parent 41ee4c90b3
commit 3961e310b1
444 changed files with 179208 additions and 0 deletions

18
vendor/github.com/thehowl/conf/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,18 @@
Copyright (c) 2016 Morgan Bazalgette
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

83
vendor/github.com/thehowl/conf/README.md generated vendored Normal file
View File

@@ -0,0 +1,83 @@
# conf [![Build Status](https://travis-ci.org/thehowl/conf.svg?branch=master)](https://travis-ci.org/thehowl/conf) [![GoDoc](https://godoc.org/github.com/thehowl/conf?status.svg)](https://godoc.org/github.com/thehowl/conf)
(yes I am that creative with names)
I have been using [ini](http://gopkg.in/ini.v1) for managing configuration files in go for quite some time. One of the things that had bothered me though, was that it really was a pain to set up for small projects, as it's just boilerplate code over and over. So I decided to write my own configuration file system, and now I'm here.
## Quick start
```go
package main
import (
"github.com/thehowl/conf"
)
type myConf struct {
Port string `description:"The port from which the application will take HTTP requests"`
Password string
MaxUsers int
}
func main() {
c := myConf{}
err := conf.Load(&c, "myapp.conf")
if err == conf.ErrNoFile {
// You can export your conf to a file, so you can write default values.
conf.Export(myConf{
Port: ":8080",
Password: "hunter2",
MaxUsers: 9001,
}, "myapp.conf")
fmt.Println("Please compile the configuration file (myapp.conf.)")
return
}
if err != nil {
panic(err)
}
// You can now use the values in `c` without thinking about the actual configuration ever again!
fmt.Printf("%#v\n", c)
}
```
## Configuration file format
```
; This is an example configuration file generated with `conf`. Comments are done using semicolons.
;
; This is a simple string value in the configuration:
String=Hello world!
; Note that there are no spaces between the field (key) name and its value. Conf does not trim strings.
; int, float, uint values are done just as easily. You just need to write that they're of that type in
; the struct, and conf will do all the magic!
Int=481
; There are also bools.
Bool=1
; Bools are retrieved through [ParseBool](https://golang.org/pkg/strconv/#ParseBool), as such they
; need to be one of 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.
; But, what about strings with newlines?
AreTheyPossible=Yes\
They\
Are!
; If you need to export a flag with a multiline string, conf will automatically escape it.
;
; By the way, conf automatically ignores lines without a valid field=value combination, including
; empty lines, so you can use them as comments, although discouraged.
So yes, this line will be silently ignored!
=This one, too!
And this one, too!=
; Escaping can not only be done with newlines. Here's what you can possibly escape!
Fields\=With\=Equal\=Signs=can be escaped!
Comments=Can \; be escaped!
Oh yeah, fields=can also have spaces and what not in them.; You can also write comments straight after a value!
; And that's all you need to know for using `conf`!
```
## License
MIT

165
vendor/github.com/thehowl/conf/conf.go generated vendored Normal file
View File

@@ -0,0 +1,165 @@
// Package conf lets you manage configuration files in the easiest way possible, without the unnecessary pain.
package conf
import (
"errors"
"fmt"
"io/ioutil"
"os"
"reflect"
"strconv"
"strings"
)
// The only custom errors this package will return.
var (
ErrNoFile = errors.New("conf: the configuration file doesn't exist")
ErrNotAStruct = errors.New("conf: the passed into/from variable is not a pointer to a struct")
)
// Load unmarshals a file into the struct passed as the argument "into".
func Load(into interface{}, filename string) error {
intoValue := reflect.ValueOf(into)
if intoValue.Kind() != reflect.Ptr || intoValue.Elem().Kind() != reflect.Struct {
return ErrNotAStruct
}
intoValue = intoValue.Elem()
f, err := ioutil.ReadFile(filename)
if os.IsNotExist(err) {
return ErrNoFile
}
if err != nil {
return err
}
return loadRaw(intoValue, f)
}
// LoadRaw allows you to load into a struct some raw data bytes.
func LoadRaw(into interface{}, data []byte) error {
intoValue := reflect.ValueOf(into)
if intoValue.Kind() != reflect.Ptr || intoValue.Elem().Kind() != reflect.Struct {
return ErrNotAStruct
}
intoValue = intoValue.Elem()
return loadRaw(intoValue, data)
}
func loadRaw(intoValue reflect.Value, data []byte) error {
fvs := Parse(data)
for _, v := range fvs {
for i := 0; i < intoValue.Type().NumField(); i++ {
field := intoValue.Type().Field(i)
if !intoValue.Field(i).CanSet() {
continue
}
if field.Name == v.Field {
switch field.Type.Kind() {
case reflect.String:
intoValue.Field(i).SetString(v.Value)
case reflect.Bool:
boolVal, err := strconv.ParseBool(v.Value)
if err != nil {
return err
}
intoValue.Field(i).SetBool(boolVal)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
intVal, err := strconv.ParseInt(v.Value, 10, 64)
if err != nil {
return err
}
intoValue.Field(i).SetInt(intVal)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
uintVal, err := strconv.ParseUint(v.Value, 10, 64)
if err != nil {
return err
}
intoValue.Field(i).SetUint(uintVal)
case reflect.Float32, reflect.Float64:
floatVal, err := strconv.ParseFloat(v.Value, 64)
if err != nil {
return err
}
intoValue.Field(i).SetFloat(floatVal)
}
}
}
}
return nil
}
// MustLoad has the same behaviour as Load, but panics if it returns an error.
func MustLoad(into interface{}, filename string) {
if err := Load(into, filename); err != nil {
panic(err)
}
}
// MustLoadRaw has the same behaviour as LoadRaw, but panics if it returns an error.
func MustLoadRaw(into interface{}, data []byte) {
if err := LoadRaw(into, data); err != nil {
panic(err)
}
}
// Export uses ExportRaw to put the data into a file, specified with its name.
func Export(from interface{}, filename string) error {
data, err := ExportRaw(from)
if err != nil {
return err
}
return ioutil.WriteFile(filename, data, 0644)
}
// ExportRaw can create a []byte that can then be loaded back by LoadRaw to get a struct's original form back.
// I suck at explaining stuff.
func ExportRaw(from interface{}) ([]byte, error) {
fromValue := reflect.ValueOf(from)
if fromValue.Kind() == reflect.Ptr {
return ExportRaw(fromValue.Elem().Interface())
}
if fromValue.Kind() != reflect.Struct {
return []byte{}, ErrNotAStruct
}
return exportRaw(fromValue), nil
}
func exportRaw(fromValue reflect.Value) []byte {
var ret []byte
for i := 0; i < fromValue.Type().NumField(); i++ {
curfield := fromValue.Field(i)
curfieldType := fromValue.Type().Field(i)
// Dirty hack to ignore that field if we don't support that type.
if !((curfield.Kind() >= reflect.Bool && curfield.Kind() <= reflect.Uint64) ||
curfield.Kind() == reflect.String || curfield.Kind() == reflect.Float32 ||
curfield.Kind() == reflect.Float64) {
continue
}
/* guten */ tag := curfieldType.Tag.Get("description")
if tag != "" {
tag = strings.Replace(tag, "\n", "\n; ", -1)
ret = append(ret, []byte("; "+tag+"\n")...)
}
ret = append(ret, []byte(Escape(curfieldType.Name)+"="+Escape(fmt.Sprint(curfield.Interface()))+"\n")...)
}
return ret
}
// MustExport panics if Export returns an error, removing error checking from your code. For the lazy.
func MustExport(from interface{}, filename string) {
if err := Export(from, filename); err != nil {
panic(err)
}
}
// MustExportRaw panics if ExportRaw returns an error, removing error checking from your code. For the lazy.
func MustExportRaw(from interface{}) []byte {
data, err := ExportRaw(from)
if err != nil {
panic(err)
}
return data
}

15
vendor/github.com/thehowl/conf/escape.go generated vendored Normal file
View File

@@ -0,0 +1,15 @@
package conf
import (
"strings"
)
// Escape escapes characters for then putting it into conf field/values without issues.
func Escape(s string) string {
return strings.NewReplacer(
"\n", "\\\n",
`\`, `\\`,
`;`, `\;`,
`=`, `\=`,
).Replace(s)
}

89
vendor/github.com/thehowl/conf/parse.go generated vendored Normal file
View File

@@ -0,0 +1,89 @@
package conf
// FieldValue is a field=value pair in the configuration.
type FieldValue struct {
Field string
Value string
}
const (
currentName = iota
currentValue
currentComment
)
// Parse converts some bytes into various FieldValue pairs.
func Parse(data []byte) []FieldValue {
fvs := []FieldValue{}
var (
fieldName string
fieldValue string
nextNormal bool
current byte
)
for _, c := range data {
if current == currentComment && c != '\n' {
continue
}
if nextNormal {
switch current {
case currentName:
fieldName += string(c)
case currentValue:
fieldValue += string(c)
}
nextNormal = false
continue
}
switch c {
case '=':
switch current {
// if we are still at the name, let's switch to a value.
case currentName:
current = currentValue
// if we are already at the value, treat the = character like any other sign
case currentValue:
fieldValue += string(c)
}
case ';':
current = currentComment
case '\n':
if fieldName != "" && fieldValue != "" {
fvs = append(fvs, FieldValue{
Field: fieldName,
Value: removeTrailingCR(fieldValue),
})
}
fieldName = ""
fieldValue = ""
current = currentName
case '\\':
nextNormal = true
default:
switch current {
case currentName:
fieldName += string(c)
case currentValue:
fieldValue += string(c)
}
nextNormal = false
}
}
if fieldName != "" && fieldValue != "" {
fvs = append(fvs, FieldValue{
Field: fieldName,
Value: removeTrailingCR(fieldValue),
})
}
return fvs
}
func removeTrailingCR(s string) string {
if len(s) == 0 {
return s
}
if s[len(s)-1] == '\r' {
return s[:len(s)-1]
}
return s
}