package
0.8.2
Repository: https://github.com/tomcz/gotools.git
Documentation: pkg.go.dev

# README

env

Populate structs from environment variables using mapstructure.

import (
	"os"
	"testing"
	"github.com/tomcz/gotools/env"
	assert "github.com/stretchr/testify/require"
)

type testCfg struct {
	User  string `mapstructure:"test_user_name"`
	Age   int    `mapstructure:"test_user_age"`
	Admin bool   `mapstructure:"test_is_admin"`
	Port  int    `mapstructure:"port"`
}

func TestEnv(t *testing.T) {
	os.Setenv("test_user_name", "Homer")
	os.Setenv("test_user_age", "42")
	os.Setenv("test_is_admin", "true")

	cfg := &testCfg{
		Port: 8080,
	}

	err := env.PopulateFromEnv(cfg)

	assert.NoError(t, err)
	assert.Equal(t, "Homer", cfg.User)
	assert.Equal(t, 42, cfg.Age)
	assert.Equal(t, true, cfg.Admin)
	assert.Equal(t, 8080, cfg.Port)
}