dotenv

package
v1.3.22 Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package dotenv provides a `.env` provider.

Index

Examples

Constants

View Source
const Name = "env"

Name of the provider.

Variables

This section is empty.

Functions

func New

func New(override, rawValuebool bool, files ...string) (provider.IProvider, error)

New sets up a new DotEnv provider.

Example

ExampleNew creates a new dotenv instance and loads the .env file.

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/thalesfsp/configurer/dotenv"
	"github.com/thalesfsp/configurer/util"
)

func main() {
	// Instantiate the provider of choice, in this case dotenv.
	de, err := dotenv.New(false, false, "testing.env")
	if err != nil {
		log.Fatalln(err)
	}

	// Load will export to the environment all the variables.
	if _, err := de.Load(context.Background()); err != nil {
		log.Fatalln(err)
	}

	// We can check it here.
	fmt.Println(os.Getenv("TEST_KEY"))

	// Additionally, we can use the config into a struct.
	type Config struct {
		TestKey string `env:"TEST_KEY" json:"testKey"`
	}

	// ExportToStruct can be called from the provider...
	var c1 Config
	if err := de.ExportToStruct(&c1); err != nil {
		log.Fatalln(err)
	}

	// or from the `util` package.
	var c2 Config
	if err := util.Dump(&c2); err != nil {
		log.Fatalln(err)
	}

	// We can check it here.
	fmt.Println(c1.TestKey)
	fmt.Println(c2.TestKey)

}
Output:

TEST_VALUE
TEST_VALUE
TEST_VALUE

Types

type DotEnv

type DotEnv struct {
	*provider.Provider `json:"-" validate:"required"`

	// FilePaths is the list of file paths to load.
	FilePaths []string `json:"filePaths" validate:"required,gte=1"`
}

DotEnv provider definition.

func (*DotEnv) Load

func (d *DotEnv) Load(ctx context.Context, opts ...option.LoadKeyFunc) (map[string]string, error)

Load retrieves the configuration, and exports it to the environment.

func (*DotEnv) Write added in v1.1.32

func (d *DotEnv) Write(ctx context.Context, values map[string]interface{}, opts ...option.WriteFunc) error

Write stores a new secret.

NOTE: Not all providers support writing secrets.

Jump to

Keyboard shortcuts

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