Skip to content

EnvPilot is a Go package for managing environment variables with type safety and dynamic reloading, featuring a CLI tool for easy configuration

License

Notifications You must be signed in to change notification settings

Amman30/EnvPilot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EnvPilot

Screenshot_2024-09-11_at_10 32 09_PM-removebg (1)

EnvPilot is a Go package and CLI tool for managing environment variables. With EnvPilot, you can easily set, retrieve, and manage environment variables both programmatically and through the command line.It offers a simple and flexible way to load and access environment variables with support for default values and type safety.

Installation

To install EnvPilot, use the following go get command:

go get github.com/Amman30/[email protected]

CLI Usage

Once EnvPilot is installed, you can use the CLI to manage environment variables. Here are some examples:

Command

 go get  github.com/Amman30/EnvPilot/cmd/[email protected]

Set Environment Variables

To set an environment variable, use the set command. You can specify the variable type and the file to save it to.

Example to set an integer variable:

pilot set MY_VAR=123222 --type int --file .env

Example to set an string variable in .env.example:

pilot set GREETING=Hello --type string --file .env.example

Retrieving Environment Variables

package main

import (
    "fmt"
    "log"

    "github.com/Amman30/EnvPilot/pkg/pilot"
)

func main() {
    // Initialize environment from .env file
    err := pilot.SetEnv(".env")
    if err != nil {
        log.Fatalf("Error initializing environment: %v", err)
    }
}

GetAsString

The GetAsString method retrieves a string value from the environment variables. You can also provide a default value to return if the variable is not found.

Arguments

  • key (string): The environment variable key to retrieve.
  • defaultValue (optional string): A default string value to return if the environment variable is not found. If not provided, an error will be returned if the key is not found.

Example: Retrieve a String

value, err := pilot.Env.GetAsString("KEY", "default_value")
if err != nil {
	log.Fatalf("Error retrieving KEY: %v", err)
}
log.Infof("Value: %s", value)

GetAsInt

The GetAsInt method retrieves a int value from the environment variables. You can also provide a default value to return if the variable is not found.

Arguments

  • key (string): The environment variable key to retrieve.
  • defaultValue (optional int): A default int value to return if the environment variable is not found. If not provided, an error will be returned if the key is not found.

Example: Retrieve a Integer

value, err := pilot.Env.GetAsInt("KEY", 8080)
if err != nil {
	log.Fatalf("Error retrieving KEY: %v", err)
}
log.Infof("Value: %d", value)

GetAsBool

The GetAsBool method retrieves a boolean value from the environment variables. You can also provide a default value to return if the variable is not found.

Arguments

  • key (string): The environment variable key to retrieve.
  • defaultValue (optional bool): A default boolean value to return if the environment variable is not found. If not provided, an error will be returned if the key is not found.

Example: Retrieve a Boolean

value, err := pilot.Env.GetAsBool("KEY", false)
if err != nil {
	log.Fatalf("Error retrieving KEY: %v", err)
}
log.Infof("Value: %t", value)

GetAsFloat

The GetAsFloat method retrieves a float value from the environment variables. You can also provide a default value to return if the variable is not found.

Arguments

  • key (string): The environment variable key to retrieve.
  • defaultValue (optional float64): A default float value to return if the environment variable is not found. If not provided, an error will be returned if the key is not found.

Example: Retrieve a Float

value, err := pilot.Env.GetAsFloat("KEY", 0.1)
if err != nil {
	log.Fatalf("Error retrieving KEY: %v", err)
}
log.Infof("Value: %f", value)

GetAsAny

The GetAsAny method retrieves a value of any type from the environment variables based on the specified target type. It also allows you to provide a default value if the variable is not found or cannot be converted to the requested type.

Arguments

  • key (string): The environment variable key to retrieve.
  • targetType (string): The type to which the value should be converted. Supported types include "string", "int", "bool", and "float".
  • defaultValue (optional): A default value to return if the environment variable is not found or cannot be converted.

Example: Retrieve an Integer

value, err := pilot.Env.GetAsAny("KEY", "int", 8080)
if err != nil {
	log.Fatalf("Error retrieving KEY: %v", err)
}
log.Infof("Value: %d", value.(int))

Dynamic Configuration Reloading

EnvPilot includes support for dynamic configuration reloading using the fsnotify package. This allows your application to automatically reload environment, making it easier to adapt to configuration updates without restarting the application.

To enable dynamic reloading, simply ensure that fsnotify is included in your project dependencies. The package will monitor the specified file path for changes and reload the configuration as needed.

License

This project is licensed under the MIT License.

About

EnvPilot is a Go package for managing environment variables with type safety and dynamic reloading, featuring a CLI tool for easy configuration

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages