This repository has been archived by the owner on Apr 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add ignore * add license * edit readme Signed-off-by: Martin <martin.piegay@zenika.com>
- Loading branch information
Martin Piegay
committed
May 13, 2016
1 parent
ad2c190
commit 96aacf7
Showing
3 changed files
with
162 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.vscode | ||
debug.test | ||
coverage.out | ||
coverage.out | ||
example* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Containous SAS, Emile Vauge, emile@vauge.com | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,139 @@ | ||
# staert | ||
# Stært | ||
[![Travis branch](https://img.shields.io/travis/containous/staert/master.svg)](https://travis-ci.org/containous/staert) | ||
[![Coverage Status](https://coveralls.io/repos/github/containous/staert/badge.svg?branch=master)](https://coveralls.io/github/containous/staert?branch=master) | ||
[![license](https://img.shields.io/github/license/containous/staert.svg)](https://github.com/containous/staert/blob/master/LICENSE.md) | ||
|
||
Stært is a Go library for loading and merging a program configuration structure from many sources. | ||
|
||
## Overview | ||
Stært was born in order to merge two sources of Configuration ([Flæg](https://github.com/containous/flaeg), [Toml](http://github.com/BurntSushi/toml)) | ||
|
||
## Features | ||
- Load your Configuration structure from many sources | ||
- Keep your Configuration structure values unchanged if no overwriting (support defaults values) | ||
- Two native sources : | ||
- [Flæg](https://github.com/containous/flaeg) | ||
- [Toml](http://github.com/BurntSushi/toml) | ||
- An Interface to add your own sources | ||
- Handle pointers field : | ||
- You can give a structure of default values for pointers | ||
- Same comportment as [Flæg](https://github.com/containous/flaeg) | ||
- Stært is Command oriented | ||
- It use `flaeg.Command` | ||
- Same comportment as [Flæg](https://github.com/containous/flaeg) commands | ||
|
||
## Getting Started | ||
### The configuration | ||
It works on your own Configuration structure, like this one : | ||
```go | ||
package example | ||
|
||
import ( | ||
"fmt" | ||
"github.com/containous/flaeg" | ||
"github.com/containous/staert" | ||
"os" | ||
) | ||
|
||
//Configuration is a struct which contains all differents type to field | ||
type Configuration struct { | ||
IntField int `description:"An integer field"` | ||
StringField string `description:"A string field"` | ||
PointerField *PointerSubConfiguration `description:"A pointer field"` | ||
} | ||
|
||
//PointerSubConfiguration is a SubStructure Configuration | ||
type PointerSubConfiguration struct { | ||
BoolField bool `description:"A boolean field"` | ||
FloatField float64 `description:"A float field"` | ||
} | ||
``` | ||
|
||
Let's initialize it: | ||
```go | ||
func main() { | ||
//Init with default value | ||
config := &Configuration{ | ||
IntField: 1, | ||
StringField: "init", | ||
PointerField: &PointerSubConfiguration{ | ||
FloatField: 1.1, | ||
}, | ||
} | ||
//Set default pointers value | ||
defaultPointersConfig := &Configuration{ | ||
PointerField: &PointerSubConfiguration{ | ||
BoolField: true, | ||
FloatField: 99.99, | ||
}, | ||
} | ||
``` | ||
### The command | ||
Stært uses `flaeg.Command` Structure, like this : | ||
```go | ||
//Create Command | ||
command:=&flaeg.Command{ | ||
Name:"example", | ||
Description:"This is an example of description", | ||
Config:config, | ||
DefaultPointersConfig:defaultPointersConfig, | ||
Run: func() error { | ||
fmt.Printf("Run example with the config :\n%+v\n",config) | ||
fmt.Printf("PointerField contains:%+v\n", config.PointerField) | ||
return nil | ||
} | ||
} | ||
``` | ||
### Use stært with sources | ||
Init Stært | ||
```go | ||
s:=staert.NewStaert(command) | ||
``` | ||
Init TOML source | ||
```go | ||
toml:=staert.NewTomlSource("example", []string{"./toml/", "/any/other/path"}) | ||
``` | ||
Init Flæg source | ||
```go | ||
f:=flaeg.New(command, os.Args[1:]) | ||
``` | ||
### Add sources | ||
Add TOML and flæg sources | ||
```go | ||
s.AddSource(toml) | ||
s.AddSource(f) | ||
``` | ||
NB : You can change order, so that, flaeg configuration will overwrite toml one | ||
### Call Run | ||
Just call Run function : | ||
```go | ||
if err := s.Run(); err != nil { | ||
//OOPS | ||
} | ||
} | ||
``` | ||
### Let's run example | ||
|
||
TOML file `./toml/example.toml` : | ||
```toml | ||
IntField= 2 | ||
[PointerField] | ||
``` | ||
We can run the example program using folowing CLI arguments : | ||
``` | ||
$ ./example --stringfield=owerwrittenFromFlag --pointerfield.floatfield=55.55 | ||
Run example with the config : | ||
&{IntField:2 StringField:owerwrittenFromFlag PointerField:0xc82000ec80} | ||
PointerField contains:&{BoolField:true FloatField:55.55} | ||
``` | ||
|
||
|
||
## Contributing | ||
1. Fork it! | ||
2. Create your feature branch: `git checkout -b my-new-feature` | ||
3. Commit your changes: `git commit -am 'Add some feature'` | ||
4. Push to the branch: `git push origin my-new-feature` | ||
5. Submit a pull request :D |