features

package
v1.3.191 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package features enables a way to encode enabled features in a flag.FlagSet.

Example
var (
	testFeature = Feature("test-feature")

	// Set of flags which require a specific feature to be enabled.
	dependencies = []Dependency{
		{Flag: "protected", Feature: testFeature},
	}
)

fs := flag.NewFlagSet("feature-flags", flag.PanicOnError)
fs.String("protected", "", `Requires "test-feature" to be enabled to set.`)
Register(fs, []Feature{testFeature})

if err := fs.Parse([]string{"--protected", "foo"}); err != nil {
	fmt.Println(err)
}

err := Validate(fs, dependencies)
if err != nil {
	fmt.Println(err)
} else {
	fmt.Println("Everything is valid!")
}
Output:

flag "protected" requires feature "test-feature" to be provided in --enable-features

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Enabled

func Enabled(fs *flag.FlagSet, name Feature) bool

Enabled retruns true if a feature is enabled. Enable will panic if fs has not been passed to Register or name is an unknown feature.

func Register

func Register(fs *flag.FlagSet, ff []Feature)

Register sets a flag in fs to track enabled features. The list of possible features is enumerated by ff. ff must contain a unique set of case-insensitive features. Register will panic if ff is invalid.

func Validate

func Validate(fs *flag.FlagSet, deps []Dependency) error

Validate returns an error if any flags from deps were used without the corresponding feature being enabled.

If deps references a flag that is not in fs, Validate will panic.

Types

type Dependency

type Dependency struct {
	// Flag must be a flag name from a FlagSet.
	Flag string
	// Feature which must be enabled for Flag to be provided at the command line.
	Feature Feature
}

Dependency marks a Flag as depending on a specific feature being enabled.

type Feature

type Feature string

Feature is an experimental feature. Features are case-insensitive.

Jump to

Keyboard shortcuts

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