From 9f4a8e23653d4c1351a9fef7319a62a9f6d7d837 Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Sat, 1 Feb 2014 12:38:29 -0800 Subject: [PATCH] allow configurable media endpoints per remote --- commands/command_config.go | 10 ++++++- config.go | 56 +++++++++++++++++++++++++++++++++++++- integration/tests.go | 2 +- 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/commands/command_config.go b/commands/command_config.go index 379a44e2..3ef6cdf5 100644 --- a/commands/command_config.go +++ b/commands/command_config.go @@ -10,7 +10,15 @@ type ConfigCommand struct { func (c *ConfigCommand) Run() { config := core.Config() - core.Print("Endpoint=%s", config.Endpoint) + + 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)) + } + } + for _, env := range core.Environ() { core.Print(env) } diff --git a/config.go b/config.go index 74361904..e03765ab 100644 --- a/config.go +++ b/config.go @@ -4,10 +4,13 @@ import ( "github.com/pelletier/go-toml" "os" "path/filepath" + "strings" ) type Configuration struct { - Endpoint string + Endpoint string + gitConfig map[string]string + remotes []string } var config *Configuration @@ -25,6 +28,57 @@ func Config() *Configuration { return config } +func (c *Configuration) RemoteEndpoint(remote string) string { + if url, ok := c.GitConfig("remote." + remote + ".mediaUrl"); ok { + return url + } + + if url, ok := c.GitConfig("remote." + remote + ".url"); ok { + return url + ".git/info/media" + } + + return "" +} + +func (c *Configuration) Remotes() []string { + if c.remotes == nil { + c.loadGitConfig() + } + return c.remotes +} + +func (c *Configuration) GitConfig(key string) (string, bool) { + if c.gitConfig == nil { + c.loadGitConfig() + } + value, ok := c.gitConfig[key] + return value, ok +} + +func (c *Configuration) loadGitConfig() { + uniqRemotes := make(map[string]bool) + + c.gitConfig = make(map[string]string) + lines := strings.Split(SimpleExec("git", "config", "-l"), "\n") + for _, line := range lines { + pieces := strings.SplitN(line, "=", 2) + key := pieces[0] + c.gitConfig[key] = pieces[1] + + keyParts := strings.Split(key, ".") + if len(keyParts) > 1 && keyParts[0] == "remote" { + uniqRemotes[keyParts[1]] = true + } + } + + c.remotes = make([]string, len(uniqRemotes)) + i := 0 + for remote, _ := range uniqRemotes { + c.remotes[i] = remote + i += 1 + } +} + func readToml(config *Configuration) { tomlPath := filepath.Join(LocalWorkingDir, ".gitmedia") stat, _ := os.Stat(tomlPath) diff --git a/integration/tests.go b/integration/tests.go index c8c8c80c..153ca04c 100644 --- a/integration/tests.go +++ b/integration/tests.go @@ -44,7 +44,7 @@ func init() { // tests on the git-media repository, which has no actual git-media assets :) allCommands[wd] = map[string]string{ "version": "git-media v" + gitmedia.Version, - "config": "Endpoint=\n" + + "config": "Endpoint (origin)=https://github.com/github/git-media.git/info/media\n" + "LocalWorkingDir=" + wd + "\n" + "LocalGitDir=" + filepath.Join(wd, ".git") + "\n" + "LocalMediaDir=" + filepath.Join(wd, ".git", "media") + "\n" +