fenv

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2021 License: MIT Imports: 8 Imported by: 3

README

GoDoc

fenv - environment variable names for flagsets

This project is archived.*

v1.0.0 is the final release.

I am currently using this library in over 40 programs so but I am using other things in newer programs so no need to keep this project active.

The tests has failiures so it might have fringe bugs.

Documentation

Overview

Example (FlagSet)

Use with fenv.EnvSet and flag.FlagSet

package main

import (
	"flag"
	"fmt"
	"log"

	"github.com/go-pa/fenv"
)

func main() {
	var s1, s2 string

	fs := flag.NewFlagSet("example", flag.ContinueOnError)
	es := fenv.NewEnvSet(fs, fenv.Prefix("my_"))

	fs.StringVar(&s1, "test1", "", "")
	fs.StringVar(&s2, "test2", "", "")
	es.Var(&s2, "other", "")

	// es.Parse() or es.ParseEnv() to parse a custom environment
	if err := es.ParseEnv(map[string]string{
		"MY_TEST1": "v1",
		"MY_TEST2": "v2",
		"OTHER":    "v2.other",
	}); err != nil {
		log.Fatal(err)
	}
	if err := fs.Parse([]string{}); err != nil {
		log.Fatal(err)
	}

	fmt.Println(s1, s2)
}
Output:

v1 v2.other
Example (Package)

Package level usage

package main

import (
	"flag"
	"fmt"
	"os"
	"strings"

	"github.com/go-pa/fenv"
)

func main() {
	var s1, s2, s3 string
	fenv.CommandLinePrefix("HELLO_") // default env var name prefix

	flag.StringVar(&s1, "flag.1", "", "") // env var FLAG_1 is automatically added

	flag.StringVar(&s2, "flag2", "", "")
	fenv.Var(&s2, "alt-name", "") // registers env names ALT_NAME and FLAG2.

	flag.StringVar(&s3, "flag3", "", "")
	fenv.Var(&s3) // excludes the var from being parsed as an enviroment variable.

	os.Setenv("HELLO_FLAG_1", "v1")
	os.Setenv("ALT_NAME", "v2.alt")
	os.Setenv("HELLO_FLAG_2", "v2")

	fmt.Println("before Parse()")
	fenv.VisitAll(func(e fenv.EnvFlag) {
		// don't print go test flags in example
		if !strings.HasPrefix(e.Flag.Name, "test.") {
			fmt.Printf("%s:%s\n", e.Flag.Name, e.Name)

		}
	})

	fenv.MustParse() // call fenv.Parse() before flag.Parse()
	flag.Parse()

	fmt.Println("after Parse()")
	fenv.VisitAll(func(e fenv.EnvFlag) {
		// don't print go test flags in example
		if !strings.HasPrefix(e.Flag.Name, "test.") {
			fmt.Printf("%s:%s\n", e.Flag.Name, e.Name)
		}
	})

	fmt.Println("values", s1, s2, flag.Parsed())
}
Output:

before Parse()
flag.1:
flag2:
after Parse()
flag.1:HELLO_FLAG_1
flag2:ALT_NAME
values v1 v2.alt true

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrAlreadyParsed = errors.New("the envset is already parsed")

ErrAlreadyParsed is returned by EnvSet.Parse() if the EnvSet already was parsed.

View Source
var ErrMultipleSet = errors.New("multiple errors encountered when calling flag.Set()")

ErrMultipleSet is returned by EnvSet.Parse() if the ContinueOnError is enabled and more than one flag failed to be set.

Functions

func CommandLinePrefix

func CommandLinePrefix(prefix ...string)

CommandLinePrefix sets the prefix used by the package level env set functions.

func MustParse

func MustParse()

func OSEnv

func OSEnv() map[string]string

OSEnv returns os.Environ() as a env.

func Parse

func Parse() error

func ParseSet

func ParseSet(fs *flag.FlagSet, opt ...Option) error

func Parsed

func Parsed() bool

func Var

func Var(v interface{}, names ...string)

func VisitAll

func VisitAll(fn func(e EnvFlag))

Types

type EnvFlag

type EnvFlag struct {
	// the associated flag.Flag
	Flag *flag.Flag

	// the environment variable name which the value for flag parsing was
	// extracted from. This field is set regardless if setting the flag succeeded or not
	// succeeds or fails.
	Name string

	// Value is the value of the environmet variable Name.
	Value string

	// all the environment variable names mapped associated with the flag.
	Names []string

	// Values for all corresponding Names which were present when EnvSet.Parse() was called.
	Env map[string]string

	// IsSet is true if the flag has been set by the owning EnvSet.Parse() function or by the associated FlagSet.
	IsSet bool

	// IsSelfSet is true if the flag value was successfully set by the
	// associated EnvSet.Parse() function.
	IsSelfSet bool

	// error caused by flag.Set when the envset tried to set it
	Err error
}

EnvFlag is used bu the EnvSet.Visit* funcs.

type EnvSet

type EnvSet struct {
	// contains filtered or unexported fields
}

EnvSet adds environment variable support for flag.FlagSet.

func NewEnvSet

func NewEnvSet(fs *flag.FlagSet, opt ...Option) *EnvSet

NewEnvSet returns a *EnvSet

func (*EnvSet) Flag

func (s *EnvSet) Flag(flagName string, names ...string)

func (*EnvSet) Parse

func (s *EnvSet) Parse() error

func (*EnvSet) ParseEnv

func (s *EnvSet) ParseEnv(env map[string]string) error

func (*EnvSet) Parsed

func (s *EnvSet) Parsed() bool

Parsed reports whether s.Parse has been called.

func (*EnvSet) Var

func (s *EnvSet) Var(value interface{}, names ...string)

Var enables associattion with environment variable names other than the default auto generated ones

If no name argument is supplied the variable will be excluded from environment pasrsing and the EnvSet.VisitAll method. The special name value emtpy string "" will be translated to the automatically generated environment variable name. This function will panic if given an

func (*EnvSet) VisitAll

func (s *EnvSet) VisitAll(fn func(e EnvFlag))

Visit visits all non exluded EnvFlags in the flagset

type FlagError

type FlagError EnvFlag

FlagError

func (FlagError) Error

func (f FlagError) Error() string

type Option

type Option func(e *EnvSet)

func ContinueOnError

func ContinueOnError() Option

func Prefix

func Prefix(prefix ...string) Option

Jump to

Keyboard shortcuts

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