goenvirement

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2023 License: MIT Imports: 7 Imported by: 0

README

Goenv

goenvirement

this is an easy to use go package to intract with .env files and manage envirement variables

Usage

install the package via :

go get github.com/Joodek/goenvirement

set your variables in a .env file

APP_URL=http://localhost
APP_PORT=8080

then call the Load function

package main

import (
    "fmt"
    "os"

    "github.com/Joodek/goenvirement"
)

func main(){
    goenvirement.Load()

    fmt.Println(os.Getenv("APP_URL")) // http://localhost
    fmt.Println(os.Getenv("APP_PORT")) // 8080
}

by default , the load function will try to load a file named .env from the current working directory , alternatively you can provide a file path or even multiple files to load


func main(){
    // one file
    goenvirement.Load("path/to/file")

    // multiple files
    goenvirement.Load("path/to/file1","path/to/file2")

  // ...
}

the load function will never override the existing variables, even if they're empty but if you want to them to be overriden , use the Overload function instead


func main(){

    goenvirement.Overload()
   // or
    goenvirement.Overload("path/to/file1","path/to/file2")

  // ...
}

sometimes you may wish to get those variables back, and take controle of what to do with them this is possible by using Read function, it will read the variables and return them as map


func main(){

    envs := goenvirement.Read()
    // or
    envs := goenvirement.Read("path/to/file1","path/to/file2")

    fmt.println(envs["SOME_VARIABLE"])

  // ...
}

In case you have the variables as a string, you can parse them using the Unmarshal function like so :


func main(){

   envs := goenvirement.Unmarshal(
		`APP_URL="http://localhost"
		APP_PORT =8080 `,
	)

	fmt.Println("APP_URL    :   ", envs["APP_URL"])
	fmt.Println("APP_PORT   :   ", envs["APP_PORT"])

  // ...
}

Comments

in all cases , whichever function you use , you can write comments

# this is valid
APP_URL=http://localhost

APP_PORT=8080 # this is also valid

Variables

you can use variables to represent other keys in your envirement, like you do in a bash script , look at the following

# you can use local evirements ,
APP_URL="http://localhost${APP_PORT}" # http://localhost:8080
APP_PORT=8080

# or you may want to use the system envirements as well
USER_CACHE="$HOME/programs/cache" # /home/yassinebenaid/programs/cache

Hint

Unlike other libraries, you don't have to stick with a specific order, this means that you can have somthing similar to this :


KEY_1="value1-$KEY_2"
KEY_2="value2-$KEY_3"
KEY_3="value3-somthing"

and they will all be expanded correctly


func main(){

   goenvirement.Load()

	fmt.Println(os.Getenv("KEY_1")) // value1-value2-value3-somthing
	fmt.Println(os.Getenv("KEY_2")) // value2-value3-somthing

  // ...
}

so far everything looks amazing, but, look at this example :


KEY_1="value1-$KEY_2"
KEY_2="value2-$KEY_3"
KEY_3="value3-$KEY_1"

as you notice here, we are tring to read a key that will never be reached, this example will return the folowing error :

recursion detected : trying to read KEY_2 by KEY_1

Author

yassinebenaid

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append added in v1.0.4

func Append(key string, value string, file string) (err error)

func Load

func Load(files ...string) error

load the envirements variable from files

if no file provided, we fall back to .env file in the current working directory The variables will be loaded and set to the current process env so you can get them via os.Getenv

The existing variables won't be overriden

func Marshal added in v1.0.4

func Marshal(m map[string]string) (string, error)

stringify the envirement variables from m and return them as string

func Overload

func Overload(files ...string) error

load the envirements variable from files

if no file provided, we fall back to .env file in the current working directory The variables will be loaded and set to the current process env so you can get them via os.Getenv

unlike Load , Overload will override the existing variables

func Read

func Read(files ...string) (map[string]string, error)

read envirement variables from files and return them as map if files is empty we fall back to .env file in the current directory

func Unmarshal

func Unmarshal(s string) (map[string]string, error)

read envirement variables from s and return them as map

func Write added in v1.0.4

func Write(m map[string]string, f string) error

write variables from m to f file

Types

This section is empty.

Jump to

Keyboard shortcuts

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