envio

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2024 License: BSD-3-Clause Imports: 9 Imported by: 0

README

envio

envio is a library designed to get/set environment variables to/from go structures.

Installation

envio can be installed like any other Go library through go get:

go get github.com/easysy/envio@latest

Getting Started

package main

import (
	"fmt"
	"os"

	"github.com/easysy/envio"
)

type Type struct {
	// will be got/set by name 'A'
	A string
	// `env:"ENV_B"` - will be got/set by name 'ENV_B'
	B string `env:"ENV_B"`
	// `env:"-"` - will be skipped
	C string `env:"-"`
	// `env:"ENV_D,m"` - will be got/set by name 'ENV_D',
	// mandatory field - if the environment doesn't contain a variable with the specified name,
	// it returns an error: the required variable $name is missing
	D   string `env:"ENV_D,m"`
	Bs  []byte `env:"ENV_BYTES"`
	// `env:"ENV_BYTES_RAW,raw"` - will be got/set by name 'ENV_BYTES_RAW',
	// since in Golang byte and uint8 mean the same thing,
	// to obtain and save an array/slice of bytes, use the 'raw' key in the tag
	BsR []byte `env:"ENV_BYTES_RAW,raw"`
}

func main() {
	in := new(Type)
	in.A = "fa"
	in.B = "fb"
	in.C = "fc"
	in.D = "fd"
	in.Bs = []byte{65, 66, 67}
	in.BsR = []byte{65, 66, 67}

	err := envio.Set(in)
	if err != nil {
		panic(err)
	}

	fmt.Printf("A: %s; ENV_B: %s; ENV_D: %s; ENV_BYTES: %s; ENV_BYTES_RAW: %s\n", os.Getenv("A"), os.Getenv("ENV_B"), os.Getenv("ENV_D"), os.Getenv("ENV_BYTES"), os.Getenv("ENV_BYTES_RAW"))
	// A: fa; ENV_B: fb; ENV_D: fd; ENV_BYTES: 65:66:67; ENV_BYTES_RAW: ABC

	out := new(Type)

	err = envio.Get(out)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%+v\n", out)
	// &{A:fa B:fb C: D:fd Bs:[65 66 67] BsR:[65 66 67]}
}

The library supports pointers to various types.

WARNING! Keep in mind that when using []*byte, the 'raw' flag will be ignored.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotSupportType      = errors.New("cannot support type")
	ErrNilInterface        = errors.New("interface is nil")
	ErrPointerToUnexported = errors.New("cannot set embedded pointer to unexported struct")
)

Functions

func Get

func Get(v any) error

Get gets values from environment variables to the value pointed to by v. If v is nil or not a pointer, Get returns a getter error.

func Set

func Set(v any) error

Set sets values from v to environment variables. If v is nil, Set returns a setter error.

Types

type Getter

type Getter interface {
	GetENV([]byte) error
}

Getter is the interface implemented by types that can themselves get ENVs.

type Setter

type Setter interface {
	SetENV() ([]byte, error)
}

Setter is the interface implemented by types that can themselves set ENVs.

Jump to

Keyboard shortcuts

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