env

package module
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2025 License: GPL-3.0 Imports: 17 Imported by: 15

README

env Library

Works like sh env, but is file based.

Design goals

  • backwards compatible: saved files are compatible with bash env syntax
  • simple: only string key=value pairs
  • concurrent: use file locks
  • global: env functions can be called from anywhere
  • dynamic: automaticly detect env changes by the file mtime

Theory of Operation

The core philosophy of this library is to standardize env in a new way

env configuration files are loaded in this defined order:

  1. /usr/share/doc/foo/foorc # upstream application defaults
  2. /etc/foo.d/ # OS distribution defaults
  3. /etc/default/foo # system admin overrides
  4. ~/.config/foo/foorc # your user settings

function examples

key := env.Get("APIKEY")

if env.True("verbose") {
    fmt.Println("worked")
}

env.Set("LocalPort", "8080")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var APPNAME string

these are normally what are sent from ldflags

View Source
var BUILDTIME string
View Source
var ErrInit error = fmt.Errorf("lib/env has not been initialized")
View Source
var NotInitialized error = errors.New("your application config not initialized")
View Source
var VERSION string

Functions

func AddPath added in v0.0.10

func AddPath(newpath string) bool

adds a path to the ENV

func BadExit

func BadExit(reason string, err error)

func Exit added in v0.0.9

func Exit(reason string, err error)

func FullPath added in v0.0.11

func FullPath(p string) string

for "~/bletchley" returns "/home/turing/bletchley"

func Get

func Get(flag string) string

func GetAppname

func GetAppname() (string, error)

func GetPanic

func GetPanic(flag string) string

func GetUsername

func GetUsername() string

func GoodExit

func GoodExit(reason string)

argv normally sets these callbacks argv has timing set from when os.Args was parsed

func If

func If(key string) bool

func Init

func Init(appname, version, buildtime string, fromargv []string, goodFunc func(string), badFunc func(string, error))

this is an experiment at this point to see how this turns out normally called by "argv" (go.wit.com/lib/protobuf/argvpb)

func InitValid

func InitValid() bool

func LoadAppENV added in v0.0.11

func LoadAppENV(appname string) error

func LoadENV added in v0.0.11

func LoadENV(filename string) error

func LoadEtc added in v0.0.9

func LoadEtc() error

func PrintTable

func PrintTable() string

func RelPath added in v0.0.11

func RelPath(p string) string

for "/home/turing/bletchley" returns "~/bletchley"

func Save

func Save() error

saves your applications config file

func Set

func Set(varname string, newValue string) error

func SetGlobal

func SetGlobal(global string, varname string, newValue string) error

func True

func True(flag string) bool

func Verbose

func Verbose() bool

Types

type Key

type Key struct {
	Var    string `protobuf:"bytes,1,opt,name=var,proto3" json:"var,omitempty"`       // ENV var name `autogenpb:unique` `autogenpb:sort`
	Value  string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`   // ENV value name
	Global string `protobuf:"bytes,3,opt,name=global,proto3" json:"global,omitempty"` // where ENV was defined in application OS settings
	Help   string `protobuf:"bytes,4,opt,name=help,proto3" json:"help,omitempty"`     // text for explaining the ENV key/value
	// contains filtered or unexported fields
}

func (*Key) Descriptor deprecated

func (*Key) Descriptor() ([]byte, []int)

Deprecated: Use Key.ProtoReflect.Descriptor instead.

func (*Key) GetGlobal

func (x *Key) GetGlobal() string

func (*Key) GetHelp

func (x *Key) GetHelp() string

func (*Key) GetValue

func (x *Key) GetValue() string

func (*Key) GetVar

func (x *Key) GetVar() string

func (*Key) ProtoMessage

func (*Key) ProtoMessage()

func (*Key) ProtoReflect

func (x *Key) ProtoReflect() protoreflect.Message

func (*Key) Reset

func (x *Key) Reset()

func (*Key) String

func (x *Key) String() string

type KeyScanner

type KeyScanner struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*KeyScanner) Next

func (it *KeyScanner) Next() *Key

Next() returns the next thing in the array

func (*KeyScanner) Scan

func (it *KeyScanner) Scan() bool

type Keys

type Keys struct {
	Uuid     string `protobuf:"bytes,1,opt,name=uuid,proto3" json:"uuid,omitempty"`       // `autogenpb:uuid:7a8aaf7f-9851-42f0-89eb-434d2e51f5bb`
	Version  string `protobuf:"bytes,2,opt,name=version,proto3" json:"version,omitempty"` // `autogenpb:version:v0.0.1 go.wit.com/lib/env`
	Keys     []*Key `protobuf:"bytes,3,rep,name=keys,proto3" json:"keys,omitempty"`
	Filename string `protobuf:"bytes,4,opt,name=filename,proto3" json:"filename,omitempty"` // can store where the filename is so that saves can be automated
	Init     bool   `protobuf:"varint,5,opt,name=init,proto3" json:"init,omitempty"`        // can store where the filename is so that saves can be automated
	HomeDir  string `protobuf:"bytes,6,opt,name=homeDir,proto3" json:"homeDir,omitempty"`   // always need this. Converts "~" into "/home/turing"
	// contains filtered or unexported fields
}

func NewKeys

func NewKeys() *Keys

func (*Keys) All

func (x *Keys) All() *KeyScanner

func (*Keys) Append

func (x *Keys) Append(y *Key)

a Append() shortcut (that does Clone() with a mutex) notsure if it really works doesn't allow nil

func (*Keys) AppendByVar

func (x *Keys) AppendByVar(y *Key) bool

func (*Keys) Clone

func (x *Keys) Clone(y *Key) *Key

a Clone() shortcut (with a mutex). notsure if it really works

func (*Keys) CloneByVar

func (x *Keys) CloneByVar(y *Key) bool

func (*Keys) Delete

func (x *Keys) Delete(y *Key) bool

func (*Keys) DeleteByVar

func (x *Keys) DeleteByVar(s string) bool

func (*Keys) Descriptor deprecated

func (*Keys) Descriptor() ([]byte, []int)

Deprecated: Use Keys.ProtoReflect.Descriptor instead.

func (*Keys) FindByVar

func (x *Keys) FindByVar(s string) *Key

lookup a Keys by the Var

func (*Keys) FormatJSON

func (v *Keys) FormatJSON() string

human readable JSON

func (*Keys) FormatTEXT

func (v *Keys) FormatTEXT() string

apparently this isn't stable, but it's awesomely better https://protobuf.dev/reference/go/faq/#unstable-text it's brilliant for config files!

func (*Keys) GetFilename

func (x *Keys) GetFilename() string

func (*Keys) GetHomeDir added in v0.0.9

func (x *Keys) GetHomeDir() string

func (*Keys) GetInit

func (x *Keys) GetInit() bool

func (*Keys) GetKeys

func (x *Keys) GetKeys() []*Key

func (*Keys) GetUuid

func (x *Keys) GetUuid() string

func (*Keys) GetVersion

func (x *Keys) GetVersion() string

func (*Keys) InsertByVar

func (x *Keys) InsertByVar(y string) *Key

returns a Key if Var matches, otherwise create

func (*Keys) IterAll

func (x *Keys) IterAll() iter.Seq[*Key]

Iterate 'for x := range' syntax using the awesome golang 1.24 'iter'

func (*Keys) IterByVar

func (x *Keys) IterByVar() iter.Seq[*Key]

'for x := range' syntax using the awesome golang 1.24 'iter'

func (*Keys) Len

func (x *Keys) Len() int

func (*Keys) Marshal

func (v *Keys) Marshal() ([]byte, error)

marshal to wire. This is called winning.

func (*Keys) MarshalJSON

func (v *Keys) MarshalJSON() ([]byte, error)

marshal json

func (*Keys) ProtoMessage

func (*Keys) ProtoMessage()

func (*Keys) ProtoReflect

func (x *Keys) ProtoReflect() protoreflect.Message

func (*Keys) Reset

func (x *Keys) Reset()

func (*Keys) SortByVar

func (x *Keys) SortByVar() *KeyScanner

func (*Keys) SortVar

func (pb *Keys) SortVar()

func (*Keys) String

func (x *Keys) String() string

func (*Keys) Unmarshal

func (v *Keys) Unmarshal(data []byte) error

unmarshal from wire. You have won.

func (*Keys) UnmarshalJSON

func (v *Keys) UnmarshalJSON(data []byte) error

unmarshal json

func (*Keys) UnmarshalTEXT

func (v *Keys) UnmarshalTEXT(data []byte) error

unmarshalTEXT. This reads the .text config file back in after the user edits it

type KeysScanner

type KeysScanner struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*KeysScanner) Next

func (it *KeysScanner) Next() *Keys

Next() returns the next thing in the array

func (*KeysScanner) Scan

func (it *KeysScanner) Scan() bool

Jump to

Keyboard shortcuts

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