argenv

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2021 License: MIT Imports: 10 Imported by: 1

README

ArgEnv

ArgEnv is a simple package for quickly loading command line parameters, environment variables, and default settings in your app.

The goal is to simplify loading configurable settings in your application.

Usage Documentation

Installation

Install Go. Installation instructions here.

Get the package
$ go get github.com/nibbleshift/argenv
import (
  "github.com/nibbleshif/argenv" // imports as package "argenv"
)
...
Example
package main

import (
	"github.com/davecgh/go-spew/spew"
	"gitlab.com/nibbleshift/argenv"
)

type MySettings struct {
	EthernetDevice string `default: "eth0" description:"Specify NIC to configure"`
	IpAddress      string `default: "127.0.0.1" description:"IP Address to listen on"`
	PortNumber     int    `default: "80" description:"IP Address to listen on"`
	Username       string `default: "root" description:"Default user"`
	Shell          string `default: "/bin/bash" description:"Default Shell"`
}

var settings *MySettings

func main() {
	argEnv := &argenv.ArgEnv{}
	settings = &MySettings{}

	argEnv.Load(settings)
	spew.Dump(settings)
}
Running Example:
go run main.go -ip-address=192.168.100.1 -port-number=8080 \
	-username=steven -shell=/bin/bash -ethernet-device=eth1
Output
(*main.MySettings)(0xc000074190)({
 EthernetDevice: (string) (len=4) "eth1",
 IpAddress: (string) (len=13) "192.168.100.1",
 PortNumber: (int) 8080,
 Username: (string) (len=6) "steven",
 Shell: (string) (len=9) "/bin/bash"
})
Running Example Usage:
go run main.go -h
Output
ArgEnv Usage of /tmp/go-build1573678449/b001/exe/main:
  -ethernet-device string
        Specify NIC to configure (default "eth0")
  -ip-address string
        IP Address to listen on (default "127.0.0.1")
  -port-number int
        IP Address to listen on (default 80)
  -shell string
        Default Shell (default "/bin/bash")
  -username string
        Default user (default "root")
Available Environment Variables:
        ETHERNET_DEVICE
        IP_ADDRESS
        PORT_NUMBER
        USERNAME
        SHELL

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArgEnv

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

ArgEnv represents the object used to process Environment and command line parameters.

func (*ArgEnv) Load

func (e *ArgEnv) Load(o interface{})

Load will scan the structure provided and populate the structure using parameters passed via Environment variables or command line parameters. If no values are found in either location, then default values specified in the 'default' struct tag will be used to populate the structure.

It is important to note that Environment variables will take precedence over command line parameters.

type Entry

type Entry struct {
	Name        string        // Name is the name of a field in a struct
	EnvName     string        // EnvName is the Environment variable name
	FlagName    string        // FlagName is the name of the command line parameter
	Type        string        // Type is the reflect.Type of the field
	Value       reflect.Value // Value is the reflect.Value of the field
	Description string        // Description is extracted from the 'description' struct tag for the field
	Default     string        // Default is extracted from the 'default' struct tag for the field
}

Entry represents a single field in a struct.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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