You've already forked goreleaser
							
							
				mirror of
				https://github.com/goreleaser/goreleaser.git
				synced 2025-10-30 23:58:09 +02:00 
			
		
		
		
	Default value for repo option.
It's loaded from Git config. Thanks @caarlos0 for implementing remoteRepo func!
This commit is contained in:
		| @@ -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" | ||||
| 	} | ||||
|   | ||||
| @@ -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") | ||||
|   | ||||
							
								
								
									
										29
									
								
								pipeline/defaults/remote.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								pipeline/defaults/remote.go
									
									
									
									
									
										Normal 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 | ||||
| } | ||||
							
								
								
									
										26
									
								
								pipeline/defaults/remote_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								pipeline/defaults/remote_test.go
									
									
									
									
									
										Normal 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) | ||||
| } | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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")) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user