Documentation ¶
Overview ¶
Copyright 2015 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2015 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2015 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Package configure generates configuration tools based on a struct definition with tags. It can read a configuration for a struct from YAML, environment variables and command line.
Given the struct definition:
type Config struct { StringVar string `env:"TEST_STRING_VAR" cli:"string" yaml:"string"` BoolVar bool `env:"TEST_BOOL_VAR" cli:"bool" yaml:"bool"` IntVar int `env:"TEST_INT_VAR" cli:"int" yaml:"int"` HexVar hexType `env:"TEST_HEX_VAR" cli:"hex" yaml:"hex"` MapVar map[string]string `env:"TEST_MAP_VAR" cli:"map" yaml:"map,flow"` SliceMapVar []map[string]string `env:"TEST_SLICE_MAP_VAR" cli:"slice" yaml:"slice,flow"` }
You can start initializing the struct from YAML, command line or environment:
import ( "os" "github.com/gravitational/configure" ) func main() { var cfg Config // parse environment variables err := configure.ParseEnv(&cfg) // parse YAML err = configure.ParseYAML(&cfg) // parse command line arguments err = configure.ParseCommandLine(&cfg, os.Ars[1:]) }
Copyright 2015 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2015 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2015 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Index ¶
- func IncIP(ip net.IP) net.IP
- func NewCommandLineApp(v interface{}) (*kingpin.Application, error)
- func ParseCommandLine(v interface{}, args []string) error
- func ParseEnv(v interface{}) error
- func ParseYAML(data []byte, cfg interface{}, funcArgs ...ParseOption) error
- type CIDR
- type CLISetter
- type EnvSetter
- type KeyVal
- type KeyValSlice
- type ParseOption
- type StringSetter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewCommandLineApp ¶
func NewCommandLineApp(v interface{}) (*kingpin.Application, error)
NewCommandLineApp generates a command line parsing tool based on the struct that was passed in as a parameter
func ParseCommandLine ¶
ParseCommandLine takes a pointer to a function and attempts to initialize it from environment variables.
func ParseEnv ¶
func ParseEnv(v interface{}) error
ParseEnv takes a pointer to a struct and attempts to initialize it from environment variables.
func ParseYAML ¶
func ParseYAML(data []byte, cfg interface{}, funcArgs ...ParseOption) error
ParseYAML parses yaml-encoded byte string into the struct passed to the function. EnableTemplating() argument allows to treat configuration file as a template for example, it will support {{env "VAR"}} - that will substitute environment variable "VAR" and pass it to YAML file parser
Types ¶
type CIDR ¶
type CIDR struct {
// contains filtered or unexported fields
}
CIDR adds several helper methods over subnet range
func (*CIDR) RelativeIP ¶
RelativeIP returns an IP given an offset from the first IP in the range. offset starts at 0, i.e. c.RelativeIP(0) == c.FirstIP()
type EnvSetter ¶
Setter is an interface that properties of struct can implement to initialize the value of any struct from string
type KeyVal ¶
KeyVal is map that can parse itself from string, represented as a comma-separated list of keys and values "key:val,key:val"
func KeyValParam ¶
func KeyValParam(s kingpin.Settings) *KeyVal
KeyValParam accepts a kingpin setting parameter and returns kingpin-compatible value
type KeyValSlice ¶
KeyValSlice is a list of key value strings
func (*KeyValSlice) Set ¶
func (kv *KeyValSlice) Set(v string) error
Set accepts string with arguments in the form "key:val,key2:val2"
func (*KeyValSlice) SetEnv ¶
func (kv *KeyValSlice) SetEnv(v string) error
SetEnv sets the value from environment variable using json encoding
func (*KeyValSlice) String ¶
func (kv *KeyValSlice) String() string
String returns a string with comma separated key-values: "key:val,key2:val2"
type ParseOption ¶
type ParseOption func(p *parseOptions)
ParseOption is a functional argument type
func EnableTemplating ¶
func EnableTemplating() ParseOption
EnableTemplating allows to treat configuration file as a template for example, it will support {{env "VAR"}} - that will substitute environment variable "VAR" and pass it to YAML file parser
type StringSetter ¶
StringSetter is an interface that allows to set variable from any string