Repository: goreleaser/goreleaser
Pull request #1702 (2020-07-23 14:19:03)

feat: make goos and goarch available in hooks
I was looking for that, and saw the ticket. Figured, I'd try to add it and it seems to work. :)

I'm parsing the relevant values from "target", everything else seemed not feasible. And I didn't want to muck around in env unless necessary.

Resolves: #1518


<!--

Hi, thanks for contributing!

Please make sure you read our CONTRIBUTING guide.

Please fill the fields above:

-->

<!-- If applied, this commit will... -->

<!-- Why is this change being made? -->

<!-- # Provide links to any relevant tickets, URLs or other resources -->

codecov[bot]: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1702?src=pr&el=h1) Report
> Merging [#1702](https://codecov.io/gh/goreleaser/goreleaser/pull/1702?src=pr&el=desc) into [master](https://codecov.io/gh/goreleaser/goreleaser/commit/1fd41adc0d64ebb2b39096d1633c98e7548d8b6e&el=desc) will **increase** coverage by `0.03%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/1702/graphs/tree.svg?width=650&height=150&src=pr&token=3bJvv6b7hN)](https://codecov.io/gh/goreleaser/goreleaser/pull/1702?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #1702      +/-   ##
==========================================
+ Coverage   82.38%   82.42%   +0.03%     
==========================================
  Files          72       72              
  Lines        4134     4143       +9     
==========================================
+ Hits         3406     3415       +9     
  Misses        608      608              
  Partials      120      120              
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/1702?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/build/build.go](https://codecov.io/gh/goreleaser/goreleaser/pull/1702/diff?src=pr&el=tree#diff-cGtnL2J1aWxkL2J1aWxkLmdv) | `100.00% <ø> (ø)` | |
| [internal/pipe/build/build.go](https://codecov.io/gh/goreleaser/goreleaser/pull/1702/diff?src=pr&el=tree#diff-aW50ZXJuYWwvcGlwZS9idWlsZC9idWlsZC5nbw==) | `98.46% <100.00%> (+0.08%)` | :arrow_up: |
| [internal/tmpl/tmpl.go](https://codecov.io/gh/goreleaser/goreleaser/pull/1702/diff?src=pr&el=tree#diff-aW50ZXJuYWwvdG1wbC90bXBsLmdv) | `95.91% <100.00%> (+0.08%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1702?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1702?src=pr&el=footer). Last update [1fd41ad...67c61c1](https://codecov.io/gh/goreleaser/goreleaser/pull/1702?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

caarlos0: Thanks!
till: @caarlos0 No, thank you for goreleaser! 🚀 

Patch:
diff --git a/internal/pipe/build/build.go b/internal/pipe/build/build.go
index c5b2ec47..c7c6bc0d 100644
--- a/internal/pipe/build/build.go
+++ b/internal/pipe/build/build.go
@@ -174,12 +174,23 @@ func buildOptionsForTarget(ctx *context.Context, build config.Build, target stri
 	if err != nil {
 		return nil, err
 	}
+
+	var goos string
+	var goarch string
+
+	if strings.Contains(target, "_") {
+		goos = strings.Split(target, "_")[0]
+		goarch = strings.Split(target, "_")[1]
+	}
+
 	log.WithField("binary", path).Info("building")
 	return &builders.Options{
 		Target: target,
 		Name:   name,
 		Path:   path,
 		Ext:    ext,
+		Os:     goos,
+		Arch:   goarch,
 	}, nil
 }
 
diff --git a/internal/tmpl/tmpl.go b/internal/tmpl/tmpl.go
index a8e45002..aaed97b7 100644
--- a/internal/tmpl/tmpl.go
+++ b/internal/tmpl/tmpl.go
@@ -149,6 +149,8 @@ func buildOptsToFields(opts build.Options) Fields {
 		ext:    opts.Ext,
 		name:   opts.Name,
 		path:   opts.Path,
+		osKey:  opts.Os,
+		arch:   opts.Arch,
 	}
 }
 
diff --git a/pkg/build/build.go b/pkg/build/build.go
index 4c4d56cc..464ac714 100644
--- a/pkg/build/build.go
+++ b/pkg/build/build.go
@@ -28,7 +28,7 @@ func For(lang string) Builder {
 
 // Options to be passed down to a builder.
 type Options struct {
-	Name, Path, Ext, Target string
+	Name, Path, Ext, Target, Os, Arch string
 }
 
 // Builder defines a builder.


Test patch:
diff --git a/internal/pipe/build/build_test.go b/internal/pipe/build/build_test.go
index 2e4070c4..bae79714 100644
--- a/internal/pipe/build/build_test.go
+++ b/internal/pipe/build/build_test.go
@@ -593,6 +593,39 @@ touch "$BAR"`
 	})
 }
 
+func TestBuild_hooksKnowGoosGoarch(t *testing.T) {
+	tmpDir, back := testlib.Mktmp(t)
+	defer back()
+
+	build := config.Build{
+		Lang:   "fake",
+		Goarch: []string{"amd64"},
+		Goos:   []string{"linux"},
+		Binary: "testing-goos-goarch.v{{.Version}}",
+		Targets: []string{
+			"linux_amd64",
+		},
+		Hooks: config.HookConfig{
+			Pre: []config.BuildHook{
+				{Cmd: "touch pre-hook-{{.Arch}}-{{.Os}}", Dir: tmpDir},
+			},
+			Post: config.BuildHooks{
+				{Cmd: "touch post-hook-{{.Arch}}-{{.Os}}", Dir: tmpDir},
+			},
+		},
+	}
+
+	ctx := context.New(config.Project{
+		Builds: []config.Build{
+			build,
+		},
+	})
+	err := runPipeOnBuild(ctx, build)
+	assert.NoError(t, err)
+	assert.FileExists(t, filepath.Join(tmpDir, "pre-hook-amd64-linux"))
+	assert.FileExists(t, filepath.Join(tmpDir, "post-hook-amd64-linux"))
+}
+
 func TestPipeOnBuild_hooksRunPerTarget(t *testing.T) {
 	tmpDir, back := testlib.Mktmp(t)
 	defer back()
@@ -669,6 +702,8 @@ func TestBuildOptionsForTarget(t *testing.T) {
 		Name:   "testbinary",
 		Path:   filepath.Join(tmpDir, "testid_linux_amd64", "testbinary"),
 		Target: "linux_amd64",
+		Os:     "linux",
+		Arch:   "amd64",
 	}, opts)
 }