Beispiel #1
0
func TestParsesEnv(t *testing.T) {
	os.Setenv("somevar", "somevalue")
	os.Setenv("othervar", "true")
	os.Setenv("PORT", "8080")
	os.Setenv("STRINGS", "string1,string2,string3")
	os.Setenv("SEPSTRINGS", "string1:string2:string3")
	os.Setenv("NUMBERS", "1,2,3,4")
	os.Setenv("BOOLS", "t,TRUE,0,1")

	defer os.Setenv("somevar", "")
	defer os.Setenv("othervar", "")
	defer os.Setenv("PORT", "")
	defer os.Setenv("STRINGS", "")
	defer os.Setenv("SEPSTRINGS", "")
	defer os.Setenv("NUMBERS", "")
	defer os.Setenv("BOOLS", "")

	cfg := Config{}
	assert.NoError(t, env.Parse(&cfg))
	assert.Equal(t, "somevalue", cfg.Some)
	assert.Equal(t, true, cfg.Other)
	assert.Equal(t, 8080, cfg.Port)
	assert.Equal(t, []string{"string1", "string2", "string3"}, cfg.Strings)
	assert.Equal(t, []string{"string1", "string2", "string3"}, cfg.SepStrings)
	assert.Equal(t, []int{1, 2, 3, 4}, cfg.Numbers)
	assert.Equal(t, []bool{true, true, false, true}, cfg.Bools)
}
Beispiel #2
0
func TestEmptyVars(t *testing.T) {
	cfg := Config{}
	assert.NoError(t, env.Parse(&cfg))
	assert.Equal(t, "", cfg.Some)
	assert.Equal(t, false, cfg.Other)
	assert.Equal(t, 0, cfg.Port)
}
Beispiel #3
0
func TestInvalidBool(t *testing.T) {
	os.Setenv("othervar", "should-be-a-bool")
	defer os.Setenv("othervar", "")

	cfg := Config{}
	assert.Error(t, env.Parse(&cfg))
}
Beispiel #4
0
func TestInvalidDuration(t *testing.T) {
	os.Setenv("DURATION", "should-be-a-valid-duration")
	defer os.Setenv("DURATION", "")

	cfg := Config{}
	assert.Error(t, env.Parse(&cfg))
}
Beispiel #5
0
func TestInvalidInt(t *testing.T) {
	os.Setenv("PORT", "should-be-an-int")
	defer os.Setenv("PORT", "")

	cfg := Config{}
	assert.Error(t, env.Parse(&cfg))
}
Beispiel #6
0
func TestParseStructWithInvalidFieldKind(t *testing.T) {
	type config struct {
		WontWork int64 `env:"BLAH"`
	}
	os.Setenv("BLAH", "10")
	cfg := config{}
	assert.Error(t, env.Parse(&cfg))
}
Beispiel #7
0
func TestErrorRequiredNotSet(t *testing.T) {
	type config struct {
		IsRequired string `env:"IS_REQUIRED,required"`
	}

	cfg := &config{}
	assert.Error(t, env.Parse(cfg))
}
Beispiel #8
0
func main() {
	cfg := config{}
	err := env.Parse(&cfg)
	if err != nil {
		fmt.Printf("%+v\n", err)
	}
	fmt.Printf("%+v\n", cfg)
}
Beispiel #9
0
func TestErrorOptionNotRecognized(t *testing.T) {
	type config struct {
		Var string `env:"VAR,not_supported!"`
	}

	cfg := &config{}
	assert.Error(t, env.Parse(cfg))

}
Beispiel #10
0
func main() {
	var config config
	env.Parse(&config)
	log.Println(config)
	db := datastores.NewDBConnectionPool(config.DatabaseURL)
	defer db.Close()
	exec := server(config, db)
	exec.Run(":" + config.Port)
}
Beispiel #11
0
func TestInvalidBoolsSlice(t *testing.T) {
	type config struct {
		BadBools []bool `env:"BADBOOLS"`
	}

	os.Setenv("BADBOOLS", "t,f,TRUE,faaaalse")
	cfg := &config{}
	assert.Error(t, env.Parse(cfg))
}
Beispiel #12
0
func TestUnsupportedSliceType(t *testing.T) {
	type config struct {
		WontWork []map[int]int `env:"WONTWORK"`
	}

	os.Setenv("WONTWORK", "1,2,3")
	defer os.Setenv("WONTWORK", "")

	cfg := &config{}
	assert.Error(t, env.Parse(cfg))
}
Beispiel #13
0
func TestEmptyVars(t *testing.T) {
	cfg := Config{}
	assert.NoError(t, env.Parse(&cfg))
	assert.Equal(t, "", cfg.Some)
	assert.Equal(t, false, cfg.Other)
	assert.Equal(t, 0, cfg.Port)
	assert.Equal(t, 0, len(cfg.Strings))
	assert.Equal(t, 0, len(cfg.SepStrings))
	assert.Equal(t, 0, len(cfg.Numbers))
	assert.Equal(t, 0, len(cfg.Bools))
}
Beispiel #14
0
func TestBadSeparator(t *testing.T) {
	type config struct {
		WontWork []int `env:"WONTWORK" envSeparator:":"`
	}

	cfg := &config{}
	os.Setenv("WONTWORK", "1,2,3,4")
	defer os.Setenv("WONTWORK", "")

	assert.Error(t, env.Parse(cfg))
}
Beispiel #15
0
func TestNoErrorRequiredSet(t *testing.T) {
	type config struct {
		IsRequired string `env:"IS_REQUIRED,required"`
	}

	cfg := &config{}

	os.Setenv("IS_REQUIRED", "val")
	defer os.Setenv("IS_REQUIRED", "")
	assert.NoError(t, env.Parse(cfg))
	assert.Equal(t, "val", cfg.IsRequired)
}
Beispiel #16
0
func TestEmptyOption(t *testing.T) {
	type config struct {
		Var string `env:"VAR,"`
	}

	cfg := &config{}

	os.Setenv("VAR", "val")
	defer os.Setenv("VAR", "")
	assert.NoError(t, env.Parse(cfg))
	assert.Equal(t, "val", cfg.Var)
}
Beispiel #17
0
func ExampleParse() {
	type config struct {
		Home         string `env:"HOME"`
		Port         int    `env:"PORT" envDefault:"3000"`
		IsProduction bool   `env:"PRODUCTION"`
	}
	os.Setenv("HOME", "/tmp/fakehome")
	cfg := config{}
	env.Parse(&cfg)
	fmt.Println(cfg)
	// Output: {/tmp/fakehome 3000 false}
}
Beispiel #18
0
func loadArgs() Args {
	args := Args{
		Listen:      ":8080",
		KVBackend:   "file",
		MSBackend:   "file",
		StoragePath: "/var/lib/guble",
	}

	env.Parse(&args)
	arg.MustParse(&args)
	return args
}
Beispiel #19
0
func ExampleParseRequiredField() {
	type config struct {
		Home         string `env:"HOME"`
		Port         int    `env:"PORT" envDefault:"3000"`
		IsProduction bool   `env:"PRODUCTION"`
		SecretKey    string `env:"SECRET_KEY,required"`
	}
	os.Setenv("HOME", "/tmp/fakehome")
	cfg := config{}
	err := env.Parse(&cfg)
	fmt.Println(err)
	// Output: Required environment variable SECRET_KEY is not set
}
Beispiel #20
0
func ExampleParseMultipleOptions() {
	type config struct {
		Home         string `env:"HOME"`
		Port         int    `env:"PORT" envDefault:"3000"`
		IsProduction bool   `env:"PRODUCTION"`
		SecretKey    string `env:"SECRET_KEY,required,option1"`
	}
	os.Setenv("HOME", "/tmp/fakehome")
	cfg := config{}
	err := env.Parse(&cfg)
	fmt.Println(err)
	// Output: Env tag option option1 not supported.
}
Beispiel #21
0
func TestParsesEnv(t *testing.T) {
	os.Setenv("somevar", "somevalue")
	os.Setenv("othervar", "true")
	os.Setenv("PORT", "8080")
	defer os.Setenv("somevar", "")
	defer os.Setenv("othervar", "")
	defer os.Setenv("PORT", "")

	cfg := Config{}
	assert.NoError(t, env.Parse(&cfg))
	assert.Equal(t, "somevalue", cfg.Some)
	assert.Equal(t, true, cfg.Other)
	assert.Equal(t, 8080, cfg.Port)
}
Beispiel #22
0
func TestParsesEnv(t *testing.T) {
	os.Setenv("somevar", "somevalue")
	os.Setenv("othervar", "true")
	os.Setenv("PORT", "8080")
	os.Setenv("STRINGS", "string1,string2,string3")
	os.Setenv("SEPSTRINGS", "string1:string2:string3")
	os.Setenv("NUMBERS", "1,2,3,4")
	os.Setenv("BOOLS", "t,TRUE,0,1")
	os.Setenv("DURATION", "1s")
	os.Setenv("FLOAT32", "3.40282346638528859811704183484516925440e+38")
	os.Setenv("FLOAT64", "1.797693134862315708145274237317043567981e+308")
	os.Setenv("FLOAT32S", "1.0,2.0,3.0")
	os.Setenv("FLOAT64S", "1.0,2.0,3.0")

	defer os.Setenv("somevar", "")
	defer os.Setenv("othervar", "")
	defer os.Setenv("PORT", "")
	defer os.Setenv("STRINGS", "")
	defer os.Setenv("SEPSTRINGS", "")
	defer os.Setenv("NUMBERS", "")
	defer os.Setenv("BOOLS", "")
	defer os.Setenv("DURATION", "")
	defer os.Setenv("FLOAT32", "")
	defer os.Setenv("FLOAT64", "")
	defer os.Setenv("FLOAT32S", "")
	defer os.Setenv("FLOAT64S", "")

	cfg := Config{}
	assert.NoError(t, env.Parse(&cfg))
	assert.Equal(t, "somevalue", cfg.Some)
	assert.Equal(t, true, cfg.Other)
	assert.Equal(t, 8080, cfg.Port)
	assert.Equal(t, []string{"string1", "string2", "string3"}, cfg.Strings)
	assert.Equal(t, []string{"string1", "string2", "string3"}, cfg.SepStrings)
	assert.Equal(t, []int{1, 2, 3, 4}, cfg.Numbers)
	assert.Equal(t, []bool{true, true, false, true}, cfg.Bools)
	d, _ := time.ParseDuration("1s")
	assert.Equal(t, d, cfg.Duration)
	f32 := float32(3.40282346638528859811704183484516925440e+38)
	assert.Equal(t, f32, cfg.Float32)
	f64 := float64(1.797693134862315708145274237317043567981e+308)
	assert.Equal(t, f64, cfg.Float64)
	assert.Equal(t, []float32{float32(1.0), float32(2.0), float32(3.0)}, cfg.Float32s)
	assert.Equal(t, []float64{float64(1.0), float64(2.0), float64(3.0)}, cfg.Float64s)
}
Beispiel #23
0
func parseConfig() (*config, error) {
	e := &envData{}
	env.Parse(e)
	// Parse the Length
	duration, err := time.ParseDuration(e.Duration)
	if err != nil {
		return nil, err
	}

	cfg := &config{
		duration:   duration,
		awsRegion:  e.AWSRegion,
		s3Path:     e.S3Path,
		s3Bucket:   e.S3Bucket,
		outputFile: e.OutputFile,
		logFile:    e.LogFile,
	}

	return cfg, nil
}
Beispiel #24
0
func TestPassReference(t *testing.T) {
	cfg := Config{}
	assert.Error(t, env.Parse(cfg))
}
Beispiel #25
0
func TestPassAnInvalidPtr(t *testing.T) {
	var thisShouldBreak int
	assert.Error(t, env.Parse(&thisShouldBreak))
}
Beispiel #26
0
func TestParseStructWithoutEnvTag(t *testing.T) {
	cfg := Config{}
	assert.NoError(t, env.Parse(&cfg))
	assert.Empty(t, cfg.NotAnEnv)
}
Beispiel #27
0
// Load from the environment
func Load() Config {
	var config Config
	env.Parse(&config)
	return config
}
Beispiel #28
0
func main() {
	cfg := config{}
	env.Parse(&cfg)
	fmt.Println(cfg)
}
Beispiel #29
0
func TestParsesDefaultConfig(t *testing.T) {
	cfg := Config{}
	assert.NoError(t, env.Parse(&cfg))
	assert.Equal(t, "postgres://localhost:5432/db", cfg.DatabaseURL)
}
Beispiel #30
0
Datei: it.go Projekt: caarlos0/it
// Init the IT framework: loads config, creates the database and startup the
// server.
func (i *IT) Init(server ServerHandlerFn, connectToDatabase db.PoolFn) func(http.ResponseWriter, *http.Request) {
	var cfg base.Config
	env.Parse(&cfg)
	i.db = db.New(connectToDatabase, &cfg)
	return server(i.db.Init())
}