GithubHelp home page GithubHelp logo

segmentio / conf Goto Github PK

View Code? Open in Web Editor NEW
84.0 7.0 10.0 121 KB

Go package for loading program configuration from multiple sources.

License: MIT License

Go 99.78% Makefile 0.22%
command-line command-line-parser configuration configuration-file configuration-loader environment environment-variables environment-vars golang go

conf's Issues

flags with string literal "null" get skipped

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
{}

Embedded structs don't have their config merged up

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

configure next source based on parsed conf

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

string fields are not parsed when numbers are set in cmd line.

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.

Special case the "version" string

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.

behavior with maps

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.

Personalize help output

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?

String configs that look like numbers can get corrupted on parse

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

Env Vars get interpreted incorrectly for default source

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.