yae

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2024 License: Apache-2.0 Imports: 14 Imported by: 1

README


yae (Yet Another Env) is an environment getter that fits specific needs for secure and flexible configuration management. It is a simple package for storing and retrieving secrets using the system keyring for safer storage of credentials during local development. yae also supports retrieving environmental variables with or without a prefix. Currently, it supports json and yaml configurations but can easily be extended.

Features

  • Securely store and retrieve secrets using the system keyring.
  • Load configuration from JSON and YAML files.
  • Support for environment variables with or without a prefix.
  • Fallback mechanism to load configuration from environment variables if the file is not found.
  • Debug logging to help trace the loading process.

Installation

To install yae, run the following command:

go get github.com/johnmikee/yae

Usage

package main

import (
	"fmt"
	"os"

	"github.com/johnmikee/yae"
)

type Config struct {
	Foo string `json:"foo"`
	Bar string `json:"bar"`
	Baz int    `json:"baz"`
}

func main() {
	var cfg Config
	err := yae.Get(
		yae.EnvType(yae.DEV),
		&yae.Env{
			Name:         "YAE",
			ConfigStruct: &cfg,
			Type:         yae.JSON,
			Debug:        true,
		},
	)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	fmt.Println("Bar: ", cfg.Bar)
	fmt.Println("Baz: ", cfg.Baz)
	fmt.Println("Foo: ", cfg.Foo)
}

alt text

Configuration Options

The Env struct provides various configuration options:

  • Name: Name of the config file.
  • Debug: Enables debug messages when set to true.
  • Type: Type of the config file (json or yaml).
  • Path: Path to the config file.
  • EnvPrefix: Prefix for environment variable names.
  • ConfigStruct: Struct to store the config values.
  • SkipFields: Fields to skip when loading from environment variables.

Examples

Loading Configuration from File

package main

import (
	"fmt"
	"os"

	"github.com/johnmikee/yae"
)

type Config struct {
	APIKey      string `json:"api_key" yaml:"api_key"`
	DatabaseURL string `json:"database_url" yaml:"database_url"`
}

func main() {
	var cfg Config
	err := yae.Get(
		yae.PROD,
		&yae.Env{
			Name:         "config.json",
			ConfigStruct: &cfg,
			Type:         yae.JSON,
			Debug:        true,
		},
	)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	fmt.Println("APIKey: ", cfg.APIKey)
	fmt.Println("DatabaseURL: ", cfg.DatabaseURL)
}

Loading Configuration from Environment Variables with Prefix

package main

import (
	"fmt"
	"os"

	"github.com/johnmikee/yae"
)

type Config struct {
	APIKey      string `json:"api_key" yaml:"api_key"`
	DatabaseURL string `json:"database_url" yaml:"database_url"`
}

func main() {
	var cfg Config
	err := yae.Get(
		yae.PROD,
		&yae.Env{
			Name:         "config.json",
			ConfigStruct: &cfg,
			Type:         yae.JSON,
			EnvPrefix:    "YAE",
			Debug:        true,
		},
	)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	fmt.Println("APIKey: ", cfg.APIKey)
	fmt.Println("DatabaseURL: ", cfg.DatabaseURL)
}

Handling Fallback to Environment Variables

If the configuration file is not found, yae will automatically fall back to loading configuration from environment variables. This is useful for scenarios where the configuration file is not available, but the necessary environment variables are set.

Debug Logging

Enable debug logging to get detailed information about the configuration loading process. Set the Debug field to true in the Env struct.

env := &yae.Env{
	Name:         "config.json",
	ConfigStruct: &cfg,
	Type:         yae.JSON,
	Debug:        true,
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildDevEnv

func BuildDevEnv(c *Env, secrets *Secrets, skipFields ...string) error

BuildDevEnv fills the values of the struct with the values from the keychain.

func BuildPrompt

func BuildPrompt(text string) string

func Get

func Get(t EnvType, c *Env) error

Get retrieves the configuration based on the specified environment type.

func LoadConfig

func LoadConfig(c *Env) error

LoadConfig loads the config from the file or falls back to environmental variables.

func RemoveKey

func RemoveKey(service, key string) error

RemoveKey removes a key from the keyring.

func SensitiveInputPrompt

func SensitiveInputPrompt(p *Prompter) (string, error)

func UpdateKey

func UpdateKey(service, key, value string) error

UpdateKey updates the value of a key in the keyring.

Types

type ConfigType

type ConfigType string
const (
	JSON ConfigType = "json"
	YAML ConfigType = "yaml"
)
var (
	CUSTOM ConfigType = "" // This will search for whatever custom tag you specify

)

type Env

type Env struct {
	Name         string      // Name of the config file
	Debug        bool        // Print debug messages
	Type         ConfigType  // Type of the config file ("json" or "yaml")
	Path         string      // Path to the config file
	EnvPrefix    string      // Prefix for environment variable names
	ConfigStruct interface{} // Struct to store the config values
	SkipFields   []string    // Fields to skip when loading from env
}

Config holds the configuration parameters for retrieving a config.

func (*Env) GetKeys

func (c *Env) GetKeys() []string

GetKeys returns the keys for the struct.

type EnvType

type EnvType string

EnvType represents the environment type.

const (
	LOCAL EnvType = "local" // Local environment will use the keychain
	DEV   EnvType = "dev"   // Dev environment will use the keychain
	PROD  EnvType = "prod"  // Prod environment will use the config file or env vars
)

type PasswordReader

type PasswordReader interface {
	ReadPassword() (string, error)
}

PasswordReader returns password read from a reader

type Prompter

type Prompter struct {
	Prompt      string
	Interactive bool
	// contains filtered or unexported fields
}

type Secret

type Secret struct {
	Name  string
	Value string
}

type Secrets

type Secrets []Secret

func GetConfig

func GetConfig(service string, args ...string) *Secrets

GetConfig will return a slice of key, values based on the args passed.

func (*Secrets) ToMap

func (s *Secrets) ToMap(skipFields ...string) map[string]string

Optionally make it a map

type StdInPasswordReader

type StdInPasswordReader struct{}

StdInPasswordReader default stdin password reader

func (StdInPasswordReader) ReadPassword

func (pr StdInPasswordReader) ReadPassword() (string, error)

ReadPassword reads password from stdin

Jump to

Keyboard shortcuts

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