env

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2025 License: AGPL-3.0 Imports: 8 Imported by: 0

README

Environment Variables

The Dracory framework utilizes environment variables for configuration. The env package provides utilities for loading and accessing these variables.

Loading Environment Variables

Environment variables can be loaded from a .env file using the env.Initialize() function. This function attempts to load variables from a .env file in the current directory.

import "github.com/dracory/base/env"

func main() {
    env.Initialize() // Loads from .env file
}

Accessing Environment Variables

The env package provides two functions for accessing environment variables:

  • env.Value(key string) string: Retrieves the value of an environment variable.
  • env.Must(key string) string: Retrieves the value of an environment variable. If the variable is not set, the program will panic.
import "github.com/dracory/base/env"

func main() {
    apiKey := env.Value("API_KEY")
    dbPassword := env.Must("DB_PASSWORD")
    fmt.Println("API Key:", apiKey)
    fmt.Println("DB Password:", dbPassword)
}

Special Prefixes

The env.Value() function supports special prefixes for processing environment variable values:

  • base64:: Decodes the value as a base64 encoded string.
  • obfuscated:: Deobfuscates the value using the envenc package.
import "github.com/dracory/base/env"

func main() {
    decodedValue := env.Value("BASE64_VALUE") // If BASE64_VALUE is "base64:SGVsbG8gV29ybGQh", it will return "Hello World!"
    obfuscatedValue := env.Value("OBFUSCATED_VALUE") // If OBFUSCATED_VALUE is "obfuscated:some_obfuscated_string", it will deobfuscate the string
    fmt.Println("Decoded Value:", decodedValue)
    fmt.Println("Obfuscated Value:", obfuscatedValue)
}

Documentation

Overview

Package env contains functions to interact with environment variables.

The package is imported like this:

import "github.com/gouniverse/base/env"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool added in v0.2.0

func Bool(key string) bool

Bool returns the value for an environment key

Any of the following values are considered true: "true", "True", "TRUE", "T", "t", "1" Any of the following values are considered false: "false", "False", "FALSE", "F", "f", "0" Any other value returns the default false.

Parameters:

  • key: The environment key

Returns:

  • The value for the environment key, false if not set

func BoolDefault added in v0.2.0

func BoolDefault(key string, defaultValue bool) bool

BoolDefault returns the value for an environment key with a default value

Any of the following values are considered true: "true", "True", "TRUE", "T", "t", "1" Any of the following values are considered false: "false", "False", "FALSE", "F", "f", "0" Any other value returns the default value.

Parameters:

  • key: The environment key
  • defaultValue: The default value

Returns:

  • The value for the environment key

func Initialize

func Initialize(envFilePath ...string)

Initialize initializes the environment variables

Business logic: - Loads .env file (by default, no need to provide path) - Loads env files from the provided paths, if any

Parameters: - envFilePath: slice of strings representing the paths to the .env files to load

Returns: - None

func Must

func Must(key string) string

Must retrieves the value of an environment variable, panicking if not set.

func Value

func Value(key string) string

Value returns the value for an environment key

If the value is not found, or if the value cannot be processed, returns an empty string.

If you want a default value, use ValueOrDefault. If you want an error, use ValueOrError. If you want a panic, use ValueOrPanic.

Parameters:

  • key: The environment key

Returns:

  • The value for the environment key

func ValueOrDefault added in v0.2.0

func ValueOrDefault(key string, defaultValue string) string

ValueOr returns the value for an environment key with a default value

Parameters:

  • key: The environment key
  • defaultValue: The default value

Returns:

  • The value for the environment key

func ValueOrError added in v0.2.0

func ValueOrError(key string) (string, error)

func ValueOrPanic added in v0.2.0

func ValueOrPanic(key string) string

func VaultLoad

func VaultLoad(options struct {
	Password      string
	VaultFilePath string
	VaultContent  string
}) error

EncInitialize initializes environment variables from a vault file or content using the provided password. It takes an options struct containing the password and either a vault file path or vault content. It returns an error if the initialization fails.

Types

This section is empty.

Jump to

Keyboard shortcuts

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