config: demote Fetcher, introduce Environment
Previously, to fetch data out of the `*config.Configuration` type, a reference
to a `Fetcher` was used, a-la:
```
cfg.Env.Get(...)
```
This is quite convenient, however, it forces the LFS client to implement
several methods more than once. Consider the interface:
```
type Fetcher interface {
Get(key string) (val string)
Bool(key string, def bool) (val bool)
// et. al.
}
```
In order to return typed information from a configuration instance, _each_
`Fetcher` must implement its own `N` methods for `Int`, `Bool`, etc.
To remedy this, the `Environment` type was introduced. It instead _has_ a
`Fetcher`, and defines its own type conversions, like so:
```
type Environment struct {
f Fetcher
}
func (e *Environment) Bool(key string, def bool) (val bool) { }
func (e *Environment) Int(key string, def int) (val int) { }
// et. al.
```
Now, the `config.Configuration` type holds a reference to an `Environment`, and
all type conversion methods are defined only once, saving time, and enforcing
consistency across multiple sources.
2016-08-04 20:19:02 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// An Environment adds additional behavior to a Fetcher, such a type conversion,
|
|
|
|
// and default values.
|
|
|
|
//
|
|
|
|
// `Environment`s are the primary way to communicate with various configuration
|
|
|
|
// sources, such as the OS environment variables, the `.gitconfig`, and even
|
|
|
|
// `map[string]string`s.
|
2016-08-15 19:17:11 +00:00
|
|
|
type Environment interface {
|
|
|
|
// Get is shorthand for calling `e.Fetcher.Get(key)`.
|
|
|
|
Get(key string) (val string, ok bool)
|
|
|
|
|
2017-04-12 21:29:11 +00:00
|
|
|
// Get is shorthand for calling `e.Fetcher.GetAll(key)`.
|
|
|
|
GetAll(key string) (vals []string)
|
|
|
|
|
2016-08-15 19:17:11 +00:00
|
|
|
// Bool returns the boolean state associated with a given key, or the
|
|
|
|
// value "def", if no value was associated.
|
|
|
|
//
|
|
|
|
// The "boolean state associated with a given key" is defined as the
|
|
|
|
// case-insensitive string comparison with the following:
|
|
|
|
//
|
|
|
|
// 1) true if...
|
|
|
|
// "true", "1", "on", "yes", or "t"
|
|
|
|
// 2) false if...
|
|
|
|
// "false", "0", "off", "no", "f", or otherwise.
|
|
|
|
Bool(key string, def bool) (val bool)
|
|
|
|
|
|
|
|
// Int returns the int value associated with a given key, or the value
|
|
|
|
// "def", if no value was associated.
|
|
|
|
//
|
|
|
|
// To convert from a the string value attached to a given key,
|
|
|
|
// `strconv.Atoi(val)` is called. If `Atoi` returned a non-nil error,
|
|
|
|
// then the value "def" will be returned instead.
|
|
|
|
//
|
|
|
|
// Otherwise, if the value was converted `string -> int` successfully,
|
|
|
|
// then it will be returned wholesale.
|
|
|
|
Int(key string, def int) (val int)
|
2016-11-10 00:33:43 +00:00
|
|
|
|
2017-04-12 21:29:11 +00:00
|
|
|
// All returns a copy of all the key/value pairs for the current
|
|
|
|
// environment.
|
|
|
|
All() map[string][]string
|
2016-08-15 19:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type environment struct {
|
|
|
|
// Fetcher is the `environment`'s source of data.
|
config: demote Fetcher, introduce Environment
Previously, to fetch data out of the `*config.Configuration` type, a reference
to a `Fetcher` was used, a-la:
```
cfg.Env.Get(...)
```
This is quite convenient, however, it forces the LFS client to implement
several methods more than once. Consider the interface:
```
type Fetcher interface {
Get(key string) (val string)
Bool(key string, def bool) (val bool)
// et. al.
}
```
In order to return typed information from a configuration instance, _each_
`Fetcher` must implement its own `N` methods for `Int`, `Bool`, etc.
To remedy this, the `Environment` type was introduced. It instead _has_ a
`Fetcher`, and defines its own type conversions, like so:
```
type Environment struct {
f Fetcher
}
func (e *Environment) Bool(key string, def bool) (val bool) { }
func (e *Environment) Int(key string, def int) (val int) { }
// et. al.
```
Now, the `config.Configuration` type holds a reference to an `Environment`, and
all type conversion methods are defined only once, saving time, and enforcing
consistency across multiple sources.
2016-08-04 20:19:02 +00:00
|
|
|
Fetcher Fetcher
|
|
|
|
}
|
|
|
|
|
2016-08-15 19:17:11 +00:00
|
|
|
// EnvironmentOf creates a new `Environment` initialized with the givne
|
config: demote Fetcher, introduce Environment
Previously, to fetch data out of the `*config.Configuration` type, a reference
to a `Fetcher` was used, a-la:
```
cfg.Env.Get(...)
```
This is quite convenient, however, it forces the LFS client to implement
several methods more than once. Consider the interface:
```
type Fetcher interface {
Get(key string) (val string)
Bool(key string, def bool) (val bool)
// et. al.
}
```
In order to return typed information from a configuration instance, _each_
`Fetcher` must implement its own `N` methods for `Int`, `Bool`, etc.
To remedy this, the `Environment` type was introduced. It instead _has_ a
`Fetcher`, and defines its own type conversions, like so:
```
type Environment struct {
f Fetcher
}
func (e *Environment) Bool(key string, def bool) (val bool) { }
func (e *Environment) Int(key string, def int) (val int) { }
// et. al.
```
Now, the `config.Configuration` type holds a reference to an `Environment`, and
all type conversion methods are defined only once, saving time, and enforcing
consistency across multiple sources.
2016-08-04 20:19:02 +00:00
|
|
|
// `Fetcher`, "f".
|
2016-08-15 19:17:11 +00:00
|
|
|
func EnvironmentOf(f Fetcher) Environment {
|
|
|
|
return &environment{f}
|
config: demote Fetcher, introduce Environment
Previously, to fetch data out of the `*config.Configuration` type, a reference
to a `Fetcher` was used, a-la:
```
cfg.Env.Get(...)
```
This is quite convenient, however, it forces the LFS client to implement
several methods more than once. Consider the interface:
```
type Fetcher interface {
Get(key string) (val string)
Bool(key string, def bool) (val bool)
// et. al.
}
```
In order to return typed information from a configuration instance, _each_
`Fetcher` must implement its own `N` methods for `Int`, `Bool`, etc.
To remedy this, the `Environment` type was introduced. It instead _has_ a
`Fetcher`, and defines its own type conversions, like so:
```
type Environment struct {
f Fetcher
}
func (e *Environment) Bool(key string, def bool) (val bool) { }
func (e *Environment) Int(key string, def int) (val int) { }
// et. al.
```
Now, the `config.Configuration` type holds a reference to an `Environment`, and
all type conversion methods are defined only once, saving time, and enforcing
consistency across multiple sources.
2016-08-04 20:19:02 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 19:17:11 +00:00
|
|
|
func (e *environment) Get(key string) (val string, ok bool) {
|
config: demote Fetcher, introduce Environment
Previously, to fetch data out of the `*config.Configuration` type, a reference
to a `Fetcher` was used, a-la:
```
cfg.Env.Get(...)
```
This is quite convenient, however, it forces the LFS client to implement
several methods more than once. Consider the interface:
```
type Fetcher interface {
Get(key string) (val string)
Bool(key string, def bool) (val bool)
// et. al.
}
```
In order to return typed information from a configuration instance, _each_
`Fetcher` must implement its own `N` methods for `Int`, `Bool`, etc.
To remedy this, the `Environment` type was introduced. It instead _has_ a
`Fetcher`, and defines its own type conversions, like so:
```
type Environment struct {
f Fetcher
}
func (e *Environment) Bool(key string, def bool) (val bool) { }
func (e *Environment) Int(key string, def int) (val int) { }
// et. al.
```
Now, the `config.Configuration` type holds a reference to an `Environment`, and
all type conversion methods are defined only once, saving time, and enforcing
consistency across multiple sources.
2016-08-04 20:19:02 +00:00
|
|
|
return e.Fetcher.Get(key)
|
|
|
|
}
|
|
|
|
|
2017-04-12 21:29:11 +00:00
|
|
|
func (e *environment) GetAll(key string) []string {
|
|
|
|
return e.Fetcher.GetAll(key)
|
|
|
|
}
|
|
|
|
|
2016-08-15 19:17:11 +00:00
|
|
|
func (e *environment) Bool(key string, def bool) (val bool) {
|
2016-08-05 22:31:39 +00:00
|
|
|
s, _ := e.Fetcher.Get(key)
|
|
|
|
if len(s) == 0 {
|
config: demote Fetcher, introduce Environment
Previously, to fetch data out of the `*config.Configuration` type, a reference
to a `Fetcher` was used, a-la:
```
cfg.Env.Get(...)
```
This is quite convenient, however, it forces the LFS client to implement
several methods more than once. Consider the interface:
```
type Fetcher interface {
Get(key string) (val string)
Bool(key string, def bool) (val bool)
// et. al.
}
```
In order to return typed information from a configuration instance, _each_
`Fetcher` must implement its own `N` methods for `Int`, `Bool`, etc.
To remedy this, the `Environment` type was introduced. It instead _has_ a
`Fetcher`, and defines its own type conversions, like so:
```
type Environment struct {
f Fetcher
}
func (e *Environment) Bool(key string, def bool) (val bool) { }
func (e *Environment) Int(key string, def int) (val int) { }
// et. al.
```
Now, the `config.Configuration` type holds a reference to an `Environment`, and
all type conversion methods are defined only once, saving time, and enforcing
consistency across multiple sources.
2016-08-04 20:19:02 +00:00
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToLower(s) {
|
|
|
|
case "true", "1", "on", "yes", "t":
|
|
|
|
return true
|
|
|
|
case "false", "0", "off", "no", "f":
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 19:17:11 +00:00
|
|
|
func (e *environment) Int(key string, def int) (val int) {
|
2016-08-05 22:31:39 +00:00
|
|
|
s, _ := e.Fetcher.Get(key)
|
|
|
|
if len(s) == 0 {
|
config: demote Fetcher, introduce Environment
Previously, to fetch data out of the `*config.Configuration` type, a reference
to a `Fetcher` was used, a-la:
```
cfg.Env.Get(...)
```
This is quite convenient, however, it forces the LFS client to implement
several methods more than once. Consider the interface:
```
type Fetcher interface {
Get(key string) (val string)
Bool(key string, def bool) (val bool)
// et. al.
}
```
In order to return typed information from a configuration instance, _each_
`Fetcher` must implement its own `N` methods for `Int`, `Bool`, etc.
To remedy this, the `Environment` type was introduced. It instead _has_ a
`Fetcher`, and defines its own type conversions, like so:
```
type Environment struct {
f Fetcher
}
func (e *Environment) Bool(key string, def bool) (val bool) { }
func (e *Environment) Int(key string, def int) (val int) { }
// et. al.
```
Now, the `config.Configuration` type holds a reference to an `Environment`, and
all type conversion methods are defined only once, saving time, and enforcing
consistency across multiple sources.
2016-08-04 20:19:02 +00:00
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
|
|
|
i, err := strconv.Atoi(s)
|
|
|
|
if err != nil {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
|
|
|
return i
|
|
|
|
}
|
2016-11-10 00:33:43 +00:00
|
|
|
|
2017-04-12 21:29:11 +00:00
|
|
|
func (e *environment) All() map[string][]string {
|
2016-11-10 00:33:43 +00:00
|
|
|
return e.Fetcher.All()
|
|
|
|
}
|