func main() { var opts tunnelproxy.ServerOptions mc := multiconfig.New() mc.Loader = multiconfig.MultiLoader( &multiconfig.TagLoader{}, &multiconfig.EnvironmentLoader{}, &multiconfig.EnvironmentLoader{Prefix: "KONFIG_TUNNELSERVER"}, &multiconfig.FlagLoader{}, ) mc.MustLoad(&opts) server, err := tunnelproxy.NewServer(&opts) if err != nil { die(err) } k, err := tunnelproxy.NewServerKite(server, Name, Version) if err != nil { die(err) } k.Run() }
func init() { rawCfg := MustAsset("config.json") loaders := []multiconfig.Loader{ &multiconfig.JSONLoader{Reader: bytes.NewReader(rawCfg)}, &multiconfig.EnvironmentLoader{Prefix: "KONFIG_GOKODING"}, } d := &multiconfig.DefaultLoader{ Loader: multiconfig.MultiLoader(loaders...), Validator: &multiconfig.RequiredValidator{}, } Builtin = &Config{} d.MustLoad(Builtin) d.MustValidate(Builtin) // set global compile time environment. environment = Builtin.Environment // Check if routes values are correct. for route, host := range Builtin.Routes { if host == "" { panic("empty host for route: " + route) } } }
func init() { config.Loader = multiconfig.MultiLoader( &multiconfig.TagLoader{}, &multiconfig.EnvironmentLoader{}, &multiconfig.EnvironmentLoader{Prefix: "KONFIG_KLOUD"}, &multiconfig.FlagLoader{}, ) log.SetFlags(log.LstdFlags | log.Lshortfile) }
func init() { var m multiconfig.Loader var l multiconfig.Loader var found = true if _, err := os.Stat(".uplay.toml"); err == nil { l = &multiconfig.TOMLLoader{Path: ".uplay.toml"} } else if _, err := os.Stat(".uplay.json"); err == nil { l = &multiconfig.JSONLoader{Path: ".uplay.json"} } else if _, err := os.Stat(fmt.Sprintf("%s/.uplay.toml", os.Getenv("HOME"))); err == nil { l = &multiconfig.TOMLLoader{Path: fmt.Sprintf("%s/.uplay.toml", os.Getenv("HOME"))} } else if _, err := os.Stat(fmt.Sprintf("%s/.uplay.json", os.Getenv("HOME"))); err == nil { l = &multiconfig.JSONLoader{Path: fmt.Sprintf("%s/.uplay.json", os.Getenv("HOME"))} } else { found = false } if found { m = multiconfig.MultiLoader(l, &multiconfig.FlagLoader{}, &multiconfig.EnvironmentLoader{}) } else { m = multiconfig.MultiLoader(&multiconfig.FlagLoader{}, &multiconfig.EnvironmentLoader{}) } m.Load(config) }
func main() { conf := &terraformer.Config{} mc := multiconfig.New() mc.Loader = multiconfig.MultiLoader( &multiconfig.TagLoader{}, &multiconfig.EnvironmentLoader{}, &multiconfig.EnvironmentLoader{Prefix: "KONFIG_TERRAFORMER"}, &multiconfig.FlagLoader{}, ) mc.MustLoad(conf) if !conf.TerraformDebug { // hashicorp.terraform outputs many logs, discard them log.SetOutput(ioutil.Discard) } log := logging.NewCustom(terraformer.Name, conf.Debug) // init terraformer t, err := terraformer.New(conf, log) if err != nil { log.Fatal(err.Error()) } k, err := terraformer.NewKite(t, conf) if err != nil { log.Fatal(err.Error()) } if err := k.RegisterForever(k.RegisterURL(true)); err != nil { log.Fatal(err.Error()) } go k.Run() <-k.ServerReadyNotify() log.Debug("Kite Started Listening") // terraformer can only be closed with signals, wait for any signal if err := t.Wait(); err != nil { log.Error("Err after waiting terraformer %s", err) } k.Close() }
// ParseFlags uses multiconfig to fill the given v with matching // flag values read from tags, environment and command line. func ParseFlags(v interface{}) error { type parentFlags interface { Underlying() *Flags } envPrefix := "keygen" // Try to read the EnvPrefix from the v, so it is possible // to set different flags for different tests when // run at once e.g. with: // // go test ./... // switch f := v.(type) { case *Flags: if f.EnvPrefix != "" { envPrefix = f.EnvPrefix } case parentFlags: if f := f.Underlying(); f.EnvPrefix != "" { envPrefix = f.EnvPrefix } } args := make([]string, 0) // non-nil to force FlagLoader to not read test flags for i, arg := range os.Args { if arg == "--" { args = os.Args[i+1:] break } } mc := multiconfig.New() mc.Loader = multiconfig.MultiLoader( &multiconfig.TagLoader{}, &multiconfig.EnvironmentLoader{ Prefix: strings.ToUpper(envPrefix), }, &multiconfig.FlagLoader{ Args: args, }, ) return mc.Load(v) }
func main() { c := &Conf{} mc := multiconfig.New() mc.Loader = multiconfig.MultiLoader( &multiconfig.TagLoader{}, &multiconfig.EnvironmentLoader{}, &multiconfig.EnvironmentLoader{Prefix: "ASGD"}, &multiconfig.FlagLoader{}, ) mc.MustLoad(c) conf := &asgd.Config{ Name: c.Name, AccessKeyID: c.AccessKeyID, SecretAccessKey: c.SecretAccessKey, Region: c.Region, AutoScalingName: c.AutoScalingName, Debug: c.Debug, } session, err := asgd.Configure(conf) if err != nil { log.Fatal("Reading config failed: ", err.Error()) } log := logging.NewCustom("asgd", conf.Debug) // remove formatting from call stack and output correct line log.SetCallDepth(1) // create lifecycle l := asgd.NewLifeCycle(session, log, conf.AutoScalingName) // configure lifecycle with system name if err := l.Configure(conf.Name); err != nil { log.Fatal(err.Error()) } done := registerSignalHandler(l, log) // listen to lifecycle events if err := l.Listen(process(c.Execute)); err != nil { log.Fatal(err.Error()) } <-done }
func main() { conf := &Config{} loader := multiconfig.MultiLoader( &multiconfig.TagLoader{}, // assign default values &multiconfig.FlagLoader{}, // read flag params &multiconfig.EnvironmentLoader{}, ) if err := loader.Load(conf); err != nil { log.Fatalf("config read err: %s", err.Error()) } if err := (&multiconfig.RequiredValidator{}).Validate(conf); err != nil { log.Fatalf("validation err: %s", err.Error()) } c := common.NewContext() c.Config.Schema = conf.Schema c.Config.Target = conf.Target s, err := common.Read(c.Config.Schema) if err != nil { log.Fatalf("schema read err: %s", err.Error()) } s = s.Resolve(s) req := &common.Req{ Schema: s, Context: c, } res := &common.Res{} err = conf.DDL.Generate(req, res) if err != nil { log.Fatal("geneddl err: %s", err.Error()) } if err := common.WriteOutput(res.Output); err != nil { log.Fatal("output write err: %s", err.Error()) } log.Println("module created with success") }
func NewPostgres(conf *PostgresConfig, log kite.Logger) *Postgres { if conf == nil { conf = new(PostgresConfig) envLoader := &multiconfig.EnvironmentLoader{Prefix: "kontrol_postgres"} configLoader := multiconfig.MultiLoader( &multiconfig.TagLoader{}, envLoader, ) if err := configLoader.Load(conf); err != nil { fmt.Println("Valid environment variables are: ") envLoader.PrintEnvs(conf) panic(err) } err := multiconfig.MultiValidator(&multiconfig.RequiredValidator{}).Validate(conf) if err != nil { fmt.Println("Valid environment variables are: ") envLoader.PrintEnvs(conf) panic(err) } } connString := fmt.Sprintf( "host=%s port=%d dbname=%s user=%s password=%s sslmode=disable connect_timeout=%d", conf.Host, conf.Port, conf.DBName, conf.Username, conf.Password, conf.ConnectTimeout, ) db, err := sql.Open("postgres", connString) if err != nil { panic(err) } p := &Postgres{ DB: db, Log: log, } cleanInterval := 120 * time.Second // clean every 120 second go p.RunCleaner(cleanInterval, KeyTTL) return p }
func getConfig(path string) (*hero.Config, error) { if path == "" { path = configName } loader := multiconfig.MultiLoader( &multiconfig.TagLoader{}, &multiconfig.EnvironmentLoader{}, &multiconfig.JSONLoader{Path: path}, ) d := &multiconfig.DefaultLoader{} d.Loader = loader d.Validator = multiconfig.MultiValidator(&multiconfig.RequiredValidator{}) cfg := &hero.Config{} err := d.Load(cfg) if err != nil { return nil, err } return cfg, nil }
func configure() (*tunnelproxymanager.Config, error) { c := &tunnelproxymanager.Config{} mc := multiconfig.New() mc.Loader = multiconfig.MultiLoader( &multiconfig.TagLoader{}, &multiconfig.EnvironmentLoader{}, &multiconfig.EnvironmentLoader{Prefix: "KONFIG_TUNNELPROXYMANAGER"}, &multiconfig.FlagLoader{}, ) mc.MustLoad(c) // decide on eb env name ebEnvName, err := getEBEnvName(c) if err != nil { return nil, err } c.EBEnvName = ebEnvName return c, nil }
func TestMain(m *testing.M) { rand.Seed(time.Now().UnixNano() + int64(os.Getpid())) argsTesting, argsConfig := splitArgs() flag.CommandLine.Parse(argsTesting) l := multiconfig.MultiLoader( &multiconfig.EnvironmentLoader{ Prefix: "e2etest", }, &multiconfig.FlagLoader{ EnvPrefix: "e2etest", Args: argsConfig, }, ) if err := l.Load(&Test); err != nil { die("unable to load configuration", err) } Test.Log = logging.NewCustom("test", Test.Debug) Test.setDefaults() if Test.Debug { fmt.Printf("e2etest.Test = %s\n", &Test) } ktrl := NewKontrol() ktrl.Start() exit := m.Run() if !Test.NoClean { Test.cleanupRoute53() } ktrl.Close() os.Exit(exit) }
func main() { loader := multiconfig.MultiLoader( &multiconfig.TagLoader{}, &multiconfig.EnvironmentLoader{Prefix: "kontrol"}, &multiconfig.EnvironmentLoader{Prefix: "KONFIG_KONTROL"}, &multiconfig.FlagLoader{EnvPrefix: "kontrol"}, ) conf := new(kontrol.Config) // Load the config, it's reads from the file, environment variables and // lastly from flags in order if err := loader.Load(conf); err != nil { panic(err) } if err := multiconfig.MultiValidator(&multiconfig.RequiredValidator{}).Validate(conf); err != nil { panic(err) } fmt.Printf("Kontrol loaded with following variables: %+v\n", conf) k := kontrol.New(conf) k.Kite.HandleHTTPFunc("/healthCheck", artifact.HealthCheckHandler(Name)) k.Kite.HandleHTTPFunc("/version", artifact.VersionHandler()) if conf.Debug { k.Kite.SetLogLevel(kite.DEBUG) } go func() { // Kloud runs on 6060, so we choose 6061 for kontrol err := http.ListenAndServe("0.0.0.0:6061", nil) k.Kite.Log.Error(err.Error()) }() k.Run() }
// MustRead takes a relative file path // and tries to open and read it into Config struct // If file is not there or file is not given, it panics // If the given file is not formatted well, panics func MustRead(path string) *Config { conf = &Config{} loaders := []multiconfig.Loader{} if path != "" { tomlLoader := &multiconfig.TOMLLoader{Path: path} loaders = append(loaders, tomlLoader) } envLoader := &multiconfig.EnvironmentLoader{Prefix: "KONFIG_SOCIALAPI"} loaders = append(loaders, envLoader) d := &multiconfig.DefaultLoader{ Loader: multiconfig.MultiLoader(loaders...), } if err := d.Load(conf); err != nil { panic(err) } return conf }
func main() { conf := &Config{} loader := multiconfig.MultiLoader( &multiconfig.TagLoader{}, // assign default values &multiconfig.FlagLoader{}, // read flag params &multiconfig.EnvironmentLoader{}, ) if err := loader.Load(conf); err != nil { log.Fatalf("config read err: %s", err.Error()) } if err := (&multiconfig.RequiredValidator{}).Validate(conf); err != nil { log.Fatalf("validation err: %s", err.Error()) } c := common.NewContext() c.Config.Schema = conf.Schema c.Config.Target = conf.Target s, err := common.Read(conf.Schema) if err != nil { log.Fatalf("schema read err: %s", err.Error()) } output, err := conf.Struct.Generate(c, s) if err != nil { log.Fatal("genestruct err: %s", err.Error()) } if err := common.WriteOutput(output); err != nil { log.Fatal("output write err: %s", err.Error()) } log.Println("module created with success") }
// Load loads the given config to the rules of images CLI func Load(conf interface{}, args []string) error { configArgs := FilterArgs(conf, args) loaders := []multiconfig.Loader{} // check for any files path, ext, err := discoverConfigPath(DefaultConfigName) if err == nil { // Choose what while is passed switch ext { case "json": // .imagesrc.json loaders = append(loaders, &multiconfig.JSONLoader{Path: path}) case "toml": fallthrough default: // .imagesrc or .imagesrc.toml loaders = append(loaders, &multiconfig.TOMLLoader{Path: path}) } } e := &multiconfig.EnvironmentLoader{ Prefix: "IMAGES", CamelCase: true, } f := &multiconfig.FlagLoader{ Args: configArgs, Flatten: true, CamelCase: true, EnvPrefix: "IMAGES", } loaders = append(loaders, e, f) l := multiconfig.MultiLoader(loaders...) return l.Load(conf) }
func main() { conf := &Config{} loader := multiconfig.MultiLoader( &multiconfig.TagLoader{}, // assign default values &multiconfig.FlagLoader{}, // read flag params ) if err := loader.Load(conf); err != nil { log.Fatalf("config read err:", err.Error()) } if err := (&multiconfig.RequiredValidator{}).Validate(conf); err != nil { log.Fatalf("validation err: %s", err.Error()) } c := common.NewContext() c.Config.Schema = conf.Schema c.Config.Target = conf.Target c.FieldNameFunc = geneddl.GetFieldNameFunc(conf.DDL.FieldNameCase) s, err := common.Read(c.Config.Schema) if err != nil { log.Fatalf("schema read err: %s", err.Error()) } s.Resolve(s) // // generate sql definitions // c.Config.Target = conf.Target + "db" + "/" output, err := conf.DDL.Generate(c, s) if err != nil { log.Fatal("geneddl err: %s", err.Error()) } if err := common.WriteOutput(output); err != nil { log.Fatal("output write err: %s", err.Error()) } // // generate models // c.Config.Target = conf.Target + "models" + "/" output, err = conf.Models.Generate(c, s) if err != nil { log.Fatalf("err while generating models", err.Error()) } if err := common.WriteOutput(output); err != nil { log.Fatal("output write err: %s", err.Error()) } // // generate rowsscanner // c.Config.Target = conf.Target + "models" + "/" output, err = conf.Rows.Generate(c, s) if err != nil { log.Fatalf("err while generating rows", err.Error()) } if err := common.WriteOutput(output); err != nil { log.Fatal("output write err: %s", err.Error()) } // // generate crud statements // c.Config.Target = conf.Target + "models" + "/" output, err = conf.Statements.Generate(c, s) if err != nil { log.Fatalf("err while generating crud statements", err.Error()) } if err := common.WriteOutput(output); err != nil { log.Fatal("output write err: %s", err.Error()) } // // generate errors // c.Config.Target = conf.Target + "errors" + "/" output, err = conf.Errors.Generate(c, s) if err != nil { log.Fatalf("err while generating errors", err.Error()) } if err := common.WriteOutput(output); err != nil { log.Fatal("output write err: %s", err.Error()) } // // generate main file // c.Config.Target = conf.Target + "cmd" + "/" output, err = conf.Mainfile.Generate(c, s) if err != nil { log.Fatalf("err while generating main file", err.Error()) } if err := common.WriteOutput(output); err != nil { log.Fatal("output write err: %s", err.Error()) } // // generate clients // c.Config.Target = conf.Target + "workers" + "/" output, err = conf.Clients.Generate(c, s) if err != nil { log.Fatalf("err while generating clients", err.Error()) } if err := common.WriteOutput(output); err != nil { log.Fatal("output write err: %s", err.Error()) } // // generate exported functions // c.Config.Target = conf.Target + "workers" + "/" output, err = conf.Functions.Generate(c, s) if err != nil { log.Fatalf("err while generating clients", err.Error()) } if err := common.WriteOutput(output); err != nil { log.Fatal("output write err: %s", err.Error()) } log.Println("module created with success") }