1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
package config
import (
"sync"
)
// delayedEnvironment is an implementation of the Environment which wraps the legacy
// behavior of `*config.Configuration.loadGitConfig()`.
//
// It is functionally equivalent to call `cfg.loadGitConfig()` before calling
// methods on the Environment type.
type delayedEnvironment struct {
env Environment
loading sync.Mutex
callback func() Environment
}
// Get is shorthand for calling the e.Load(), and then returning
// `e.env.Get(key)`.
func (e *delayedEnvironment) Get(key string) (string, bool) {
e.Load()
return e.env.Get(key)
}
// Get is shorthand for calling the e.Load(), and then returning
// `e.env.GetAll(key)`.
func (e *delayedEnvironment) GetAll(key string) []string {
e.Load()
return e.env.GetAll(key)
}
// Get is shorthand for calling the e.Load(), and then returning
// `e.env.Bool(key, def)`.
func (e *delayedEnvironment) Bool(key string, def bool) bool {
e.Load()
return e.env.Bool(key, def)
}
// Get is shorthand for calling the e.Load(), and then returning
// `e.env.Int(key, def)`.
func (e *delayedEnvironment) Int(key string, def int) int {
e.Load()
return e.env.Int(key, def)
}
// All returns a copy of all the key/value pairs for the current git config.
func (e *delayedEnvironment) All() map[string][]string {
e.Load()
return e.env.All()
}
// Load reads and parses the .gitconfig by calling ReadGitConfig. It
// also sets values on the configuration instance `g.config`.
//
// If Load has already been called, this method will bail out early,
// and return false. Otherwise it will perform the entire parse and return true.
//
// Load is safe to call across multiple goroutines.
func (e *delayedEnvironment) Load() {
e.loading.Lock()
defer e.loading.Unlock()
if e.env != nil {
return
}
e.env = e.callback()
}
|