config

package
v0.2.2-alpha Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: MIT Imports: 9 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 `mapstructure:"server_name"`
	App        App    `mapstructure:"app"`
}

type App struct {
	Host string `mapstructure:"host"`
	Port int    `mapstructure:"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 `mapstructure:"server_name"`
	App        App    `mapstructure:"app"`
}

type App struct {
	Host string `mapstructure:"host"`
	Port int    `mapstructure:"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 `mapstructure:"server_name"`
	App        App    `mapstructure:"app"`
}

type App struct {
	Host string `mapstructure:"host"`
	Port int    `mapstructure:"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

The `mapstructure="server_name"` tag that allows us to bind our environment variable to the fields of the structure.

We can also use the validator tags, to add more validations for example if we want to check if a field is required, we can use the `validate="required"` tag


Package Dependency


Authors


License

The project is licensed under the MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetConfigFromEnv

func GetConfigFromEnv(config interface{}) 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.

func SetEnvsFromFile

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

SetEnvsFromFile reads a file that contains environment variables in the format: 'VAR_NAME=var-value' and sets these variables in the OS's environment.

The projectDirName specifies the folder name working directory The fileNames parameter specifies the names of the files to search.

Types

This section is empty.

Jump to

Keyboard shortcuts

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