env

package
v0.4.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2023 License: MIT Imports: 4 Imported by: 0

README

env

Populate structs from environment variables using mapstructure.

import (
	"os"
	"testing"
	"github.com/tomcz/gotools/env"
	"gotest.tools/v3/assert"
)

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.NilError(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)
}

Documentation

Overview

Package env provide functions to populate structs from environment variables using https://github.com/mitchellh/mapstructure.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PopulateFromEnv

func PopulateFromEnv(target any) error

PopulateFromEnv populates the target using environment variables and mapstructure (https://github.com/mitchellh/mapstructure) mappings. Please note that the target must be a pointer to a struct.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL