1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-17 20:47:50 +02:00

Default value for repo option.

It's loaded from Git config.
Thanks @caarlos0 for implementing remoteRepo func!
This commit is contained in:
Jorin Vogel 2017-01-14 19:12:20 +01:00
parent 0cda903340
commit 37034012c4
6 changed files with 65 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package defaults
import (
"errors"
"io/ioutil"
"strings"
@ -19,6 +20,13 @@ func (Pipe) Description() string {
// Run the pipe
func (Pipe) Run(ctx *context.Context) (err error) {
if ctx.Config.Repo == "" {
repo, err := remoteRepo()
ctx.Config.Repo = repo
if err != nil {
return errors.New("failed reading repo from Git: " + err.Error())
}
}
if ctx.Config.Build.Main == "" {
ctx.Config.Build.Main = "main.go"
}

View File

@ -18,6 +18,7 @@ func TestFillBasicData(t *testing.T) {
assert.NoError(Pipe{}.Run(ctx))
assert.Equal("goreleaser/releaser", config.Repo)
assert.Equal("main.go", config.Build.Main)
assert.Equal("tar.gz", config.Archive.Format)
assert.Contains(config.Build.Oses, "darwin")

View File

@ -0,0 +1,29 @@
package defaults
import (
"errors"
"os/exec"
"strings"
)
// remoteRepo gets the repo name from the Git config.
func remoteRepo() (result string, err error) {
cmd := exec.Command("git", "config", "--get", "remote.origin.url")
bts, err := cmd.CombinedOutput()
if err != nil {
return "", errors.New(err.Error() + ": " + string(bts))
}
return extractRepoFromURL(string(bts)), nil
}
func extractRepoFromURL(s string) string {
for _, r := range []string{
"git@github.com:",
".git",
"https://github.com/",
"\n",
} {
s = strings.Replace(s, r, "", -1)
}
return s
}

View File

@ -0,0 +1,26 @@
package defaults
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRepoName(t *testing.T) {
assert := assert.New(t)
name, err := remoteRepo()
assert.NoError(err)
assert.Equal("goreleaser/releaser", name)
}
func TestExtractReporFromGitURL(t *testing.T) {
assert := assert.New(t)
url := extractRepoFromURL("git@github.com:goreleaser/releaser.git")
assert.Equal("goreleaser/releaser", url)
}
func TestExtractReporFromHttpsURL(t *testing.T) {
assert := assert.New(t)
url := extractRepoFromURL("https://github.com/goreleaser/releaser.git")
assert.Equal("goreleaser/releaser", url)
}

View File

@ -19,8 +19,5 @@ func (Pipe) Run(ctx *context.Context) (err error) {
if ctx.Config.BinaryName == "" {
return errors.New("missing binary_name")
}
if ctx.Config.Repo == "" {
return errors.New("missing repo")
}
return
}

View File

@ -23,10 +23,6 @@ func TestValidadeMissingBinaryName(t *testing.T) {
assert.Error(t, runPipe("a/b", ""))
}
func TestValidadeMissingRepo(t *testing.T) {
assert.Error(t, runPipe("", "a"))
}
func TestValidadeMinimalConfig(t *testing.T) {
assert.NoError(t, runPipe("a/b", "a"))
assert.NoError(t, runPipe("", "a"))
}