yae

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2023 License: Apache-2.0 Imports: 12 Imported by: 1

README


yae (yet another env ) is a env getter that fit my needs. It is a very simple package for storing and retrieving secrets using the system keyring for safer storage of credentials during local development. yae also has support for getting environmental variables with or without a prefix. Right now this only support json and yaml but that could very easily be extended.

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,
		},
	)
	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

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
	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