allow configurable media endpoints per remote
This commit is contained in:
parent
2f12609ba9
commit
9f4a8e2365
@ -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)
|
||||
}
|
||||
|
56
config.go
56
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 "<unknown>"
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -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" +
|
||||
|
Loading…
Reference in New Issue
Block a user