/sconfig

Configure your Go applications with a single struct

Primary LanguageGoMIT LicenseMIT

sconfig

Configure your Go applications with a single struct

Example

import (
    "github.com/psiemens/sconfig"
    "github.com/spf13/cobra"
)

type Config struct {
    Environment string        `default:"LOCAL" flag:"env,e" info:"application environment"`
    Port        int           `default:"80" flag:"port,p" info:"port to start the server on"`
    Timeout     time.Duration `default:"1s"`

    // Deprecated flags
    HostPort int `default:"80" flag:"host-port" deprecated:"use --port flag instead"`
}

var conf Config

var cmd = &cobra.Command{
	Use: "hello-world",
	Run: func(cmd *cobra.Command, args []string) {
		log.Println("Hello world!")
	},
}

func init() {
	err := sconfig.New(&conf).
		FromEnvironment("APP").
		BindFlags(cmd.PersistentFlags()).
		Parse()
	if err != nil {
		log.Fatal(err)
	}
}
# call with env vars and/or command line flags
APP_TIMEOUT=5s hello-world --port 8080 -e PRODUCTION