config

package
v1.14.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package config simplifies the declaration of configuration options. Right now the implementation maps them directly command line flags. When combined with test/e2e/framework/viper in a test suite, those flags then can also be read from a config file.

Instead of defining flags one-by-one, developers annotate a structure with tags and then call a single function. This is the same approach as in https://godoc.org/github.com/jessevdk/go-flags, but implemented so that a test suite can continue to use the normal "flag" package.

For example, a file storage/csi.go might define:

var scaling struct {
        NumNodes int  `default:"1" description:"number of nodes to run on"`
        Master string
}
_ = config.AddOptions(&scaling, "storage.csi.scaling")

This defines the following command line flags:

-storage.csi.scaling.numNodes=<int>  - number of nodes to run on (default: 1)
-storage.csi.scaling.master=<string>

All fields in the structure must be exported and have one of the following types (same as in the `flag` package): - bool - time.Duration - float64 - string - int - int64 - uint - uint64 - and/or nested or embedded structures containing those basic types.

Each basic entry may have a tag with these optional keys:

usage:   additional explanation of the option
default: the default value, in the same format as it would
         be given on the command line and true/false for
         a boolean

The names of the final configuration options are a combination of an optional common prefix for all options in the structure and the name of the fields, concatenated with a dot. To get names that are consistent with the command line flags defined by `ginkgo`, the initial character of each field name is converted to lower case.

There is currently no support for aliases, so renaming the fields or the common prefix will be visible to users of the test suite and may breaks scripts which use the old names.

The variable will be filled with the actual values by the test suite before running tests. Beware that the code which registers Ginkgo tests cannot use those config options, because registering tests and options both run before the E2E test suite handles parameters.

Index

Constants

This section is empty.

Variables

View Source
var CommandLine = flag.CommandLine

CommandLine is the flag set that AddOptions adds to. Usually this is the same as the default in the flag package, but can also be something else (for example during testing).

Functions

func AddOptions

func AddOptions(options interface{}, prefix string) bool

AddOptions analyzes the options value and creates the necessary flags to populate it.

The prefix can be used to root the options deeper in the overall set of options, with a dot separating different levels.

The function always returns true, to enable this simplified registration of options: _ = AddOptions(...)

It panics when it encounters an error, like unsupported types or option name conflicts.

Types

This section is empty.

Jump to

Keyboard shortcuts

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