Documentation
¶
Overview ¶
Package envflag implements support for setting flags through environment variables. Unlike the package standard flag package, a set of top-level wrapper functions are not provided.
Example ¶
package main
import (
"flag"
"fmt"
"github.com/LiveRamp/gazette/pkg/envflag"
)
func main() {
// Define envflags. Because this example is testable, long descriptive
// names have been chosen to reduce the chances of an environment variable
// by that name actually existing.
dbEndpoint := envflag.CommandLine.String(
"envflagExampleMysql", "MYSQL_SERVICE_ENDPOINT_EXAMPLE", "mysql.example:3306", "MySQL endpoint")
redisEndpoint := envflag.CommandLine.String(
"envflagExampleRedis", "REDIS_SERVICE_ENDPOINT_EXAMPLE", "redis.example:6379", "Redis endpoint")
pingURL := envflag.CommandLine.String(
"ping", "ENVFLAG_EXAMPLE_PING_URL", "http://localhost/ping", "URL to ping")
user := envflag.CommandLine.String(
"user", "ENVFLAG_EXAMPLE_USER", "nobody", "User to act as")
// Parse values from the environment. This is typically done before parsing
// from the command-line arguments so the command-line takes precedence
// over the environment.
envflag.CommandLine.Parse()
// It is the responsibility of the user of envflag to call flag.Parse to
// parse command-line flags.
flag.Parse()
fmt.Println(*dbEndpoint)
fmt.Println(*redisEndpoint)
fmt.Println(*pingURL)
fmt.Println(*user)
}
Output: mysql.example:3306 redis.example:6379 http://localhost/ping nobody
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var CommandLine = NewFlagSet(flag.CommandLine)
CommandLine is the default set of env flags, backed by the default set of command-line flags. The top-level functions of this package are wrappers for the methods of CommandLine.
Functions ¶
This section is empty.
Types ¶
type FlagSet ¶
type FlagSet struct {
// contains filtered or unexported fields
}
FlagSet represents a set of defined env flags.
func NewFlagSet ¶
NewFlagSet returns a new, empty flag set.
func (*FlagSet) Parse ¶
func (fs *FlagSet) Parse()
Parse parses configuration from environment variables and stores non-empty values. It is recommended to call this after all flags are defined and before flag.FlagSet.Parse.
func (*FlagSet) String ¶
String defines a string flag. A string flag is a single string value, parsed either from an environment variable "<envvarName>" or a command-line argument "-<flagName". The return value is the address of a string variable that stores the value of the flag.
This is a general flag creation utility which is why flagName and envvarName are provided separately rather than generated from a common base.
Example ¶
package main
import (
"flag"
"fmt"
"github.com/LiveRamp/gazette/pkg/envflag"
)
func main() {
fs := flag.NewFlagSet("Example", flag.PanicOnError)
efs := envflag.NewFlagSet(fs)
efs.String("callback", "CALLBACK_URL", "http://my.example/callback", "HTTP callback")
fmt.Println(fs.Lookup("callback").Name)
}
Output: callback