config: introduce NewFromValues

This commit is contained in:
Taylor Blau 2016-05-31 10:38:02 -06:00
parent 2c8d8d810f
commit 05b01a57c9

@ -3,6 +3,7 @@
package config
import (
"bytes"
"fmt"
"os"
"path/filepath"
@ -77,6 +78,29 @@ func NewConfig() *Configuration {
return c
}
// NewFromValues returns a new *config.Configuration instance as if it had
// been read from the .gitconfig specified by "gitconfig" parameter.
//
// NOTE: this method should only be called during testing.
func NewFromValues(gitconfig map[string]string) *Configuration {
config := &Configuration{
gitConfig: make(map[string]string, 0),
}
buf := bytes.NewBuffer([]byte{})
for k, v := range gitconfig {
fmt.Fprintf(buf, "%s=%s\n", k, v)
}
config.readGitConfig(
string(buf.Bytes()),
map[string]bool{},
false,
)
return config
}
func (c *Configuration) Getenv(key string) string {
c.envVarsMutex.Lock()
defer c.envVarsMutex.Unlock()