kill toml, git config does what we need

This commit is contained in:
Rick Olson 2014-02-01 12:46:58 -08:00
parent 9f4a8e2365
commit 2e25dcc0f8
4 changed files with 25 additions and 43 deletions

@ -110,8 +110,8 @@ func clientRequest(method, oid string) (*http.Request, Creds, error) {
}
func ObjectUrl(oid string) *url.URL {
c := gitmedia.Config()
u, _ := url.Parse(c.Endpoint)
c := gitmedia.Config
u, _ := url.Parse(c.Endpoint())
u.Path = filepath.Join(u.Path, "/objects/"+oid)
return u
}

@ -15,9 +15,9 @@ func TestObjectUrl(t *testing.T) {
"http://example.com/foo/": "http://example.com/foo/objects/oid",
}
config := gitmedia.Config()
config := gitmedia.Config
for endpoint, expected := range tests {
config.Endpoint = endpoint
config.SetConfig("media.url", endpoint)
assert.Equal(t, expected, ObjectUrl(oid).String())
}
}

@ -9,14 +9,14 @@ type ConfigCommand struct {
}
func (c *ConfigCommand) Run() {
config := core.Config()
config := core.Config
if len(config.Endpoint) > 0 {
core.Print("Endpoint=%s", config.Endpoint)
} else {
for _, remote := range config.Remotes() {
core.Print("Endpoint (%s)=%s", remote, config.RemoteEndpoint(remote))
}
if endpoint := config.Endpoint(); len(endpoint) > 0 {
core.Print("Endpoint=%s", endpoint)
}
for _, remote := range config.Remotes() {
core.Print("Endpoint (%s)=%s", remote, config.RemoteEndpoint(remote))
}
for _, env := range core.Environ() {

@ -1,31 +1,22 @@
package gitmedia
import (
"github.com/pelletier/go-toml"
"os"
"path/filepath"
"strings"
)
type Configuration struct {
Endpoint string
gitConfig map[string]string
remotes []string
}
var config *Configuration
var Config = &Configuration{}
// Config gets the git media configuration for the current repository. It
// reads .gitmedia, which is a toml file.
//
// https://github.com/mojombo/toml
func Config() *Configuration {
if config == nil {
config = &Configuration{}
readToml(config)
func (c *Configuration) Endpoint() string {
if url, ok := c.GitConfig("media.url"); ok {
return url
}
return config
return ""
}
func (c *Configuration) RemoteEndpoint(remote string) string {
@ -37,7 +28,7 @@ func (c *Configuration) RemoteEndpoint(remote string) string {
return url + ".git/info/media"
}
return "<unknown>"
return ""
}
func (c *Configuration) Remotes() []string {
@ -55,6 +46,13 @@ func (c *Configuration) GitConfig(key string) (string, bool) {
return value, ok
}
func (c *Configuration) SetConfig(key, value string) {
if c.gitConfig == nil {
c.loadGitConfig()
}
c.gitConfig[key] = value
}
func (c *Configuration) loadGitConfig() {
uniqRemotes := make(map[string]bool)
@ -79,21 +77,5 @@ func (c *Configuration) loadGitConfig() {
}
}
func readToml(config *Configuration) {
tomlPath := filepath.Join(LocalWorkingDir, ".gitmedia")
stat, _ := os.Stat(tomlPath)
if stat != nil {
readTomlFile(tomlPath, config)
}
}
func readTomlFile(path string, config *Configuration) {
tomlConfig, err := toml.LoadFile(path)
if err != nil {
Panic(err, "Error reading TOML file: %s", path)
}
if endpoint, ok := tomlConfig.Get("endpoint").(string); ok {
config.Endpoint = endpoint
}
func init() {
}