func TestParseSimpleConfig(t *testing.T) {
	var conf struct {
		Name string
		Log  struct {
			Path string
		}
	}

	err := envconfig.Init(&conf)
	require.Equal(t, "envconfig: keys NAME, name not found", err.Error())

	os.Setenv("NAME", "foobar")
	err = envconfig.Init(&conf)
	require.Equal(t, "envconfig: keys LOG_PATH, log_path not found", err.Error())

	os.Setenv("LOG_PATH", "/var/log/foobar")
	err = envconfig.Init(&conf)
	require.Nil(t, err)

	require.Equal(t, "foobar", conf.Name)
	require.Equal(t, "/var/log/foobar", conf.Log.Path)

	// Clean up at the end of the test - some tests share the same key and we don't values to be seen by those tests
	os.Setenv("NAME", "")
	os.Setenv("LOG_PATH", "")
}
func TestParseWrongValues(t *testing.T) {
	var conf struct{ OK bool }
	os.Setenv("OK", "foobar")
	err := envconfig.Init(&conf)
	require.Equal(t, `strconv.ParseBool: parsing "foobar": invalid syntax`, err.Error())

	var conf2 struct{ Port int }
	os.Setenv("PORT", "foobar")
	err = envconfig.Init(&conf2)
	require.Equal(t, `strconv.ParseInt: parsing "foobar": invalid syntax`, err.Error())

	var conf3 struct{ Port uint }
	os.Setenv("PORT", "foobar")
	err = envconfig.Init(&conf3)
	require.Equal(t, `strconv.ParseUint: parsing "foobar": invalid syntax`, err.Error())

	var conf4 struct{ Port float32 }
	os.Setenv("PORT", "foobar")
	err = envconfig.Init(&conf4)
	require.Equal(t, `strconv.ParseFloat: parsing "foobar": invalid syntax`, err.Error())

	var conf5 struct{ Data []byte }
	os.Setenv("DATA", "foobar")
	err = envconfig.Init(&conf5)
	require.Equal(t, "illegal base64 data at input byte 4", err.Error())
}
func TestParseDefaultVal(t *testing.T) {
	var conf struct {
		MySQL struct {
			Master struct {
				Address string `envconfig:"default=localhost"`
				Port    int    `envconfig:"default=3306"`
			}
			Timeout      time.Duration `envconfig:"default=1m,myTimeout"`
			LocalTimeout time.Duration `envconfig:"myTimeout2,default=1m"`
		}
	}

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "localhost", conf.MySQL.Master.Address)
	require.Equal(t, 3306, conf.MySQL.Master.Port)
	require.Equal(t, time.Minute*1, conf.MySQL.Timeout)

	os.Setenv("myTimeout", "2m")
	os.Setenv("myTimeout2", "20m")

	err = envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "localhost", conf.MySQL.Master.Address)
	require.Equal(t, 3306, conf.MySQL.Master.Port)
	require.Equal(t, time.Minute*2, conf.MySQL.Timeout)
	require.Equal(t, time.Minute*20, conf.MySQL.LocalTimeout)
}
func TestParseSliceConfig(t *testing.T) {
	var conf struct {
		Names  []string
		Ports  []int
		Shards []struct {
			Name string
			Addr string
		}
	}

	os.Setenv("NAMES", "foobar,barbaz")
	os.Setenv("PORTS", "900,100")
	os.Setenv("SHARDS", "{foobar,localhost:2929},{barbaz,localhost:2828}")

	err := envconfig.Init(&conf)
	require.Nil(t, err)

	require.Equal(t, 2, len(conf.Names))
	require.Equal(t, "foobar", conf.Names[0])
	require.Equal(t, "barbaz", conf.Names[1])
	require.Equal(t, 2, len(conf.Ports))
	require.Equal(t, 900, conf.Ports[0])
	require.Equal(t, 100, conf.Ports[1])
	require.Equal(t, 2, len(conf.Shards))
	require.Equal(t, "foobar", conf.Shards[0].Name)
	require.Equal(t, "localhost:2929", conf.Shards[0].Addr)
	require.Equal(t, "barbaz", conf.Shards[1].Name)
	require.Equal(t, "localhost:2828", conf.Shards[1].Addr)
}
Пример #5
0
func TestParseOptionalConfig(t *testing.T) {
	var conf struct {
		Name    string        `envconfig:"optional"`
		Flag    bool          `envconfig:"optional"`
		Timeout time.Duration `envconfig:"optional"`
		Port    int           `envconfig:"optional"`
		Port2   uint          `envconfig:"optional"`
		Delta   float32       `envconfig:"optional"`
		DeltaV  float64       `envconfig:"optional"`
		Slice   []string      `envconfig:"optional"`
		Struct  struct {
			A string
			B int
		} `envconfig:"optional"`
	}

	os.Setenv("NAME", "")
	os.Setenv("FLAG", "")
	os.Setenv("TIMEOUT", "")
	os.Setenv("PORT", "")
	os.Setenv("PORT2", "")
	os.Setenv("DELTA", "")
	os.Setenv("DELTAV", "")
	os.Setenv("SLICE", "")
	os.Setenv("STRUCT", "")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "", conf.Name)
}
func TestInvalidDurationConfig(t *testing.T) {
	var conf struct {
		Timeout time.Duration
	}

	os.Setenv("TIMEOUT", "foo")

	err := envconfig.Init(&conf)
	require.NotNil(t, err)
}
func TestInvalidSliceElementValueKind(t *testing.T) {
	var conf struct {
		Foo []interface{}
	}

	os.Setenv("FOO", "lalala")

	err := envconfig.Init(&conf)
	require.Equal(t, "envconfig: kind interface not supported", err.Error())
}
Пример #8
0
// initialize config and turns it to map
func initializeConfig() map[string]string {
	err := envconfig.Init(&conf)
	if err != nil {
		log.Panic("Error on env config initialize! ", err.Error())
	}

	return map[string]string{
		"dbConnection": conf.DB.Connection,
	}
}
func TestUnmarshaler(t *testing.T) {
	var conf struct {
		LogMode logMode
	}

	os.Setenv("LOGMODE", "file")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, logFile, conf.LogMode)

	var conf2 struct {
		LogMode *logMode
	}

	err = envconfig.Init(&conf2)
	require.Nil(t, err)
	require.Equal(t, logFile, *conf2.LogMode)
}
Пример #10
0
func ExampleInit() {
	var conf struct {
		MySQL struct {
			Host     string
			Port     int
			Database struct {
				User     string
				Password string
				Name     string
			}
			Params struct {
				Charset string `envconfig:"-"`
			}
		}
		Log struct {
			Path   string `envconfig:"default=/var/log/mylog.log"`
			Rotate bool   `envconfig:"logRotate"`
		}
		NbWorkers int
		Timeout   time.Duration
		Cassandra struct {
			SSLCert string
			SSLKey  string
		}
	}

	os.Setenv("MYSQL_HOST", "localhost")
	os.Setenv("MYSQL_PORT", "3306")
	os.Setenv("MYSQL_DATABASE_USER", "root")
	os.Setenv("MYSQL_DATABASE_PASSWORD", "foobar")
	os.Setenv("MYSQL_DATABASE_NAME", "default")
	os.Setenv("logRotate", "true")
	os.Setenv("NBWORKERS", "10")
	os.Setenv("TIMEOUT", "120s")
	os.Setenv("CASSANDRA_SSL_CERT", "/etc/cassandra/ssl.crt")
	os.Setenv("CASSANDRA_SSL_KEY", "/etc/cassandra/ssl.key")

	if err := envconfig.Init(&conf); err != nil {
		fmt.Printf("err=%s\n", err)
	}

	fmt.Println(conf.MySQL.Database.User)
	fmt.Println(conf.Log.Rotate)
	fmt.Println(conf.Timeout)
	fmt.Println(conf.Log.Path)
	fmt.Println(conf.Cassandra.SSLCert)
	fmt.Println(conf.Cassandra.SSLKey)
	// Output:
	// root
	// true
	// 2m0s
	// /var/log/mylog.log
	// /etc/cassandra/ssl.crt
	// /etc/cassandra/ssl.key
}
func TestParseEmptyTag(t *testing.T) {
	var conf struct {
		Name string `envconfig:""`
	}

	os.Setenv("NAME", "foobar")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "foobar", conf.Name)
}
func TestParseMapType(t *testing.T) {
	var conf struct {
		Map myMapType
	}

	os.Setenv("MAP", "a")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, 1, conf.Map["a"])
}
func TestParseCustomNameConfig(t *testing.T) {
	var conf struct {
		Name string `envconfig:"customName"`
	}

	os.Setenv("customName", "foobar")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "foobar", conf.Name)
}
func TestParseSkippableConfig(t *testing.T) {
	var conf struct {
		Flag bool `envconfig:"-"`
	}

	os.Setenv("FLAG", "true")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, false, conf.Flag)
}
func TestParseBoolConfig(t *testing.T) {
	var conf struct {
		DoIt bool
	}

	os.Setenv("DOIT", "true")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, true, conf.DoIt)
}
func TestParseBytesConfig(t *testing.T) {
	var conf struct {
		Data []byte
	}

	os.Setenv("DATA", "Rk9PQkFS")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, []byte("FOOBAR"), conf.Data)
}
func TestInitPointerToAPointer(t *testing.T) {
	type Conf struct {
		Name string
	}
	var tmp *Conf

	os.Setenv("NAME", "foobar")

	err := envconfig.Init(&tmp)
	require.Nil(t, err)
	require.Equal(t, "foobar", tmp.Name)
}
func TestDurationConfig(t *testing.T) {
	var conf struct {
		Timeout time.Duration
	}

	os.Setenv("TIMEOUT", "1m")

	err := envconfig.Init(&conf)
	require.Nil(t, err)

	require.Equal(t, time.Minute*1, conf.Timeout)
}
func TestUnexportedField(t *testing.T) {
	var conf struct {
		name string
	}

	os.Setenv("NAME", "foobar")

	err := envconfig.Init(&conf)
	require.Equal(t, envconfig.ErrUnexportedField, err)

	err = envconfig.InitWithOptions(&conf, envconfig.Options{AllowUnexported: true})
	require.Equal(t, nil, err)
}
func TestParseStructSliceWrongValue(t *testing.T) {
	var conf struct {
		Shards []struct {
			Name string
			Port int32
		}
	}

	os.Setenv("SHARDS", "{foobar,barbaz}")

	err := envconfig.Init(&conf)
	require.Equal(t, `strconv.ParseInt: parsing "barbaz": invalid syntax`, err.Error())
}
func TestParseStructSliceWrongData(t *testing.T) {
	var conf struct {
		Shards []struct {
			Name string
			Addr string
		}
	}

	os.Setenv("SHARDS", "foobar")

	err := envconfig.Init(&conf)
	require.Equal(t, "envconfig: struct token has 1 fields but struct has 2", err.Error())
}
func TestParseOptionalStruct(t *testing.T) {
	var conf struct {
		Master struct {
			Name string
		} `envconfig:"optional"`
	}

	os.Setenv("MASTER_NAME", "")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, "", conf.Master.Name)
}
func TestParseFloatConfig(t *testing.T) {
	var conf struct {
		Delta  float32
		DeltaV float64
	}

	os.Setenv("DELTA", "0.02")
	os.Setenv("DELTAV", "400.20000000001")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, float32(0.02), conf.Delta)
	require.Equal(t, float64(400.20000000001), conf.DeltaV)
}
func TestSliceTypeWithUnmarshaler(t *testing.T) {
	var conf struct {
		Data sliceWithUnmarshaler
	}

	os.Setenv("DATA", "1.2.3")

	err := envconfig.Init(&conf)
	require.Nil(t, err)
	require.Equal(t, 3, len(conf.Data))
	require.Equal(t, 1, conf.Data[0])
	require.Equal(t, 2, conf.Data[1])
	require.Equal(t, 3, conf.Data[2])
}
Пример #25
0
// initialize config and turns it to map
func initializeConfig() map[string]string {
	err := envconfig.Init(&conf)
	if err != nil {
		log.Panic("Error on env config initialize! ", err.Error())
	}

	return map[string]string{
		"botToken":                conf.Bot.Token,
		"botMode":                 conf.Bot.Mode,
		"quoteSource":             conf.Quote.Source,
		"weatherApiKey":           conf.Weather.Api.Key,
		"attachmentAdventureTime": conf.Attachment.Adventure.Time,
		"magicCardPath":           conf.MagicCard.Path,
	}
}
func TestAllPointerConfig(t *testing.T) {
	var conf struct {
		Name   *string
		Port   *int
		Delta  *float32
		DeltaV *float64
		Hosts  *[]string
		Shards *[]*struct {
			Name *string
			Addr *string
		}
		Master *struct {
			Name *string
			Addr *string
		}
		Timeout *time.Duration
	}

	os.Setenv("NAME", "foobar")
	os.Setenv("PORT", "9000")
	os.Setenv("DELTA", "40.01")
	os.Setenv("DELTAV", "200.00001")
	os.Setenv("HOSTS", "localhost,free.fr")
	os.Setenv("SHARDS", "{foobar,localhost:2828},{barbaz,localhost:2929}")
	os.Setenv("MASTER_NAME", "master")
	os.Setenv("MASTER_ADDR", "localhost:2727")
	os.Setenv("TIMEOUT", "1m")

	err := envconfig.Init(&conf)
	require.Nil(t, err)

	require.Equal(t, "foobar", *conf.Name)
	require.Equal(t, 9000, *conf.Port)
	require.Equal(t, float32(40.01), *conf.Delta)
	require.Equal(t, 200.00001, *conf.DeltaV)
	require.Equal(t, 2, len(*conf.Hosts))
	require.Equal(t, "localhost", (*conf.Hosts)[0])
	require.Equal(t, "free.fr", (*conf.Hosts)[1])
	require.Equal(t, 2, len(*conf.Shards))
	require.Equal(t, "foobar", *(*conf.Shards)[0].Name)
	require.Equal(t, "localhost:2828", *(*conf.Shards)[0].Addr)
	require.Equal(t, "barbaz", *(*conf.Shards)[1].Name)
	require.Equal(t, "localhost:2929", *(*conf.Shards)[1].Addr)
	require.Equal(t, "master", *conf.Master.Name)
	require.Equal(t, "localhost:2727", *conf.Master.Addr)
	require.Equal(t, time.Minute*1, *conf.Timeout)
}
Пример #27
0
func main() {
	if err := envconfig.Init(&config); err != nil {
		log.Println("Can't start:", err.Error())
	}

	log.Println("Configuration:")
	log.Println(config)

	mux := http.NewServeMux()
	mux.Handle("/", &ipHandler{})

	addr := config.Addr
	if len(addr) == 0 {
		addr = ":9004"
	}
	http.ListenAndServe(addr, mux)
}
func TestNestedUnexportedField(t *testing.T) {
	var conf struct {
		Foo struct {
			Bar struct {
				baz string
			}
		}
	}

	os.Setenv("FOO_BAR_BAZ", "foobar")

	err := envconfig.Init(&conf)
	require.Equal(t, envconfig.ErrUnexportedField, err)

	err = envconfig.InitWithOptions(&conf, envconfig.Options{AllowUnexported: true})
	require.Equal(t, nil, err)
}
Пример #29
0
func (a *App) Init() {
	// init
	err := envconfig.Init(&a.Config)
	if err != nil {
		log.Println("err: on config reading:", err.Error())
		os.Exit(1)
	}

	// router
	a.router = mux.NewRouter()

	// read templates
	if err := a.initTemplates(); err != nil {
		log.Println("err: can't read templates:", err.Error())
		os.Exit(1)
	}

	// open pg connection
	a.db.Init(a.Config.DB)
}
func TestParseIntegerConfig(t *testing.T) {
	var conf struct {
		Port    int
		Long    uint64
		Version uint8
	}

	timestamp := time.Now().UnixNano()

	os.Setenv("PORT", "80")
	os.Setenv("LONG", fmt.Sprintf("%d", timestamp))
	os.Setenv("VERSION", "2")

	err := envconfig.Init(&conf)
	require.Nil(t, err)

	require.Equal(t, 80, conf.Port)
	require.Equal(t, uint64(timestamp), conf.Long)
	require.Equal(t, uint8(2), conf.Version)
}