segmentio / conf Goto Github PK
View Code? Open in Web Editor NEWGo package for loading program configuration from multiple sources.
License: MIT License
Go package for loading program configuration from multiple sources.
License: MIT License
This was unexpected behavior for me. I imagine it has to do with JSON/yaml parsing support.
Perhaps documentation is all that's needed.
package main
import (
"fmt"
"github.com/segmentio/conf"
)
type config struct {
Name string `conf:"name" help:"set me please"`
}
func main() {
c := config{}
conf.Load(&c)
fmt.Println(c)
}
abraithwaite at alan-mbpr in ~GOPATH/src/github.com/segmentio
10:57:05 $ go run play.go -name null
{}
When processing command line flags, you must specify the embedded structs name or tag when loading configuration. This is unlike the behavior of other deserializers (json, primarily).
An example is much easier to understand than this is to explain:
abraithwaite at alan-mbpr in ~GOPATH/src/github.com/segmentio
15:58:58 $ go run play.go -name "blah" -D.location "SF"
Hello, blah
{"name": "asdf", "location": "NY"}
{"location":"SF","name":"blah"}
abraithwaite at alan-mbpr in ~GOPATH/src/github.com/segmentio
15:59:14 $ go run play.go -name "blah" -location "SF"
Usage:
play [-h] [-help] [options...]
Options:
-D object
-D.location string
-config-file source
Location to load the configuration file from.
-name string
(default blah)
Error:
flag provided but not defined: -location
exit status 1
For the file: https://gist.github.com/abraithwaite/3f74d9fbd06dbcba0c2ac14d61cc2a09#file-play-go
It would be preferable (although it would be a breaking change, maybe that can be avoided) for the properties on D to be merged up to C without needing a prefix. Perhaps we can get away with a special tag like, but not equal to -
.
i have useful case - after parsing env or cmdline i need to configure consul and vault source and load conf from it
how to do that? before first source loaded (env and/or cmdline) i can't configure consul source
conf example:
type Config struct {
Name string `conf:"name" help:"Package name"`
ID string `conf:"id" help:"UTI representing the app."`
}
if I launch my program like that:
myprog -id 42
myprog -id "42"
The usage description is displayed like if -h
is used and instructions after conf.load
are not called.
We have a config that looks like e.g.
type config struct {
Version bool `conf:"version" help:"Print the program version and exit."`
S3Bucket string `conf:"s3-bucket" help:"The S3 bucket to deliver logs batches to." validate:"nonzero"`
}
If you run ./myprogram --version
you get the following error:
Error:
invalid value passed to s3-bucket: zero value
Instead I expect to see the version number printed out. I'm not sure what the right approach would be.
#23 was merged, but it didn't get a corresponding documentation update.
I guess I expected based on the Map abstraction that you could deserialize config into either a map or a struct. One of the first tests I wrote did this:
source := NewKubernetesConfigMapSource("./testdata/configmap")
base := make(map[string]string)
mp := makeNodeMap(reflect.ValueOf(base), reflect.TypeOf(base))
if err := source.Load(mp); err != nil {
t.Fatal(err)
}
Which "worked" but produces a panic if you try Loader.Load.
We should maybe try to make more clear that you should only be passing structs, maybe by also panicking further down.
It would be awesome if we can personalize the -h output.
I m in the case where I want to specify a command as parameter.
e.g.
gomac build -name MyProg
gomac run -v
I can retrieve the command with the result of conf.Load
but if i don't find the command i want, I would like to print the help with a customized header like:
unknown command.
Usage:
gomac [-h] [-help] [options...] [build | run]
Options:
... // The rest of the option output.
Have a way to set the usage would be nice.
Also is there a way to trigger manually the help output?
Sample case:
package main
import (
"fmt"
"github.com/segmentio/conf"
)
func main() {
var config struct {
StringValue string `conf:"string-value"`
}
conf.LoadWith(&config, conf.Loader{
Args: []string{"-string-value", "0123456789"},
})
fmt.Println(config.StringValue)
}
Outputs:
1.23456789e+08
At Golab, I was watching the keynote by @ardan-bkennedy where he shared their Service template. He demonstrated their conf package which works almost exactly like ours:
https://github.com/ardanlabs/conf
One killer feature in that package which we're missing is a flag to disable logging of sensitive parameters. Let's add something to that regard!
Using the default loader, it includes the file source with a templated yaml file.
It populates the template with the environment variables, then parses the yaml file. This causes strings like a: b
to get interpreted as yaml kv pairs.
Not sure what the best course of action is, but probably needs to be addressed by escaping/cleaning the input somehow.
Common example scenario is addr:port URIs.
https://github.com/segmentio/conf/blob/master/source.go#L127-L132
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.