config

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2024 License: MIT Imports: 11 Imported by: 0

README ยถ

๐Ÿ” Config Environments

This package provides convenient methods to manage environment configurations.

It builds upon the functionality of Viper-go, enhancing the handling of environment variables in your application.


Installation

  go get github.com/andresxlp/gosuite@latest

Usage

๐Ÿ”๐Ÿ’ป
Use it when your variables are set in your environment
package main

import (
	"fmt"
	"os"

	"github.com/andresxlp/gosuite/config"
)

type Config struct {
	ServerName string `env:"server_name"`
	App        App    `env:"app"`
}

type App struct {
	Host string `env:"host"`
	Port int    `env:"port"`
}

func main() {
	_ = os.Setenv("SERVER_NAME", "server-test")
	_ = os.Setenv("APP_HOST", "192.168.25.255")
	_ = os.Setenv("APP_PORT", "3001")

	cfg := Config{}

	if err := config.GetConfigFromEnv(&cfg); err != nil {
		fmt.Printf("error setting environment variables in struct \n Error: %v", err)
		return
	}

	fmt.Printf("The config with envs is %+v", cfg)
	// output: The config with envs is {ServerName:server-test App:{Host:192.168.25.255 Port:3001}}

}


๐Ÿ“„โžก๏ธ๐Ÿ’ป
Use it when your variables are in an .env file
SERVER_NAME=server-env
APP_HOST=127.0.0.1
APP_PORT=9000

package main

import (
	"fmt"
	"os"

	"github.com/andresxlp/gosuite/config"
)

type Config struct {
	ServerName string `env:"server_name"`
	App        App    `env:"app"`
}

type App struct {
	Host string `env:"host"`
	Port int    `env:"port"`
}

func main() {
	if err := config.SetEnvsFromFile("server",".develop.env"); err != nil {
		fmt.Printf("error setting variables in environment \n Error: %v", err)
		return
	}

	cfg := Config{}

	if err := config.GetConfigFromEnv(&cfg); err != nil {
		fmt.Printf("error setting environment variables in struct \n Error: %v", err)
		return
	}

	fmt.Printf("The config with envs is %+v", cfg)
	// output: The config with envs is {ServerName:server-env App:{Host:127.0.0.1 Port:9000}}
}

Additional Info

Nested Struct

For this type of struct, for example:

type Config struct {
	ServerName string `env:"server_name"`
	App        App    `env:"app"`
}

type App struct {
	Host string `env:"host"`
	Port int    `env:"port"`
}

The keys in the .env file must be configured as the nested structure as follows:

SERVER_NAME=sever-name
APP_HOST=0.0.0.0
APP_PORT=8080
Tags
  • env: env:"server_name" tag that allows us to bind our environment variable to the fields of the structure.
    • If you want to use a custom tag, you can use the PersonalTagName parameter in the GetConfigFromEnv function to change the default tag.
  • validate: validate:"required" tag that allow use the validator.
  • mod: mod:"default=true" tag that allow use the mold.

Package Dependency


Authors


License

The project is licensed under the MIT License

Documentation ยถ

Index ยถ

Constants ยถ

View Source
const (
	DefaultTag = "env"
)

Variables ยถ

This section is empty.

Functions ยถ

func GetConfigFromEnv ยถ

func GetConfigFromEnv(config interface{}, PersonalTagName ...string) error

GetConfigFromEnv reads the fields of the given struct and looks up environment variables that match their names, associating the values accordingly. The config argument is a pointer to the struct.

By default, we use the DefaultTag, if you want to override this value you can do so using the PersonalTagName parameter.

func SetEnvsFromFile ยถ

func SetEnvsFromFile(projectDirName string, fileNames ...string) error

SetEnvsFromFile reads a file containing environment variables and adds them to the OS's environment.

- projectDirName parameter specifies the folder name working directory.

- fileNames parameter specifies the names of the files to search.

Use only in development environments.

Types ยถ

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher ๐Ÿ‡ป๐Ÿ‡ณ