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