Skip to content

Commit

Permalink
feat: expose runtime goos and goarch on templates and metadata (#2859)
Browse files Browse the repository at this point in the history
* feat: expose runtime goos and goarch on templates and metadata

* test: fix duplicated map literal in some os/arch

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed Feb 1, 2022
1 parent 0b494f7 commit 9d481d4
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 7 deletions.
22 changes: 16 additions & 6 deletions internal/pipe/metadata/metadata.go
Expand Up @@ -33,6 +33,10 @@ func writeMetadata(ctx *context.Context) error {
Version: ctx.Version,
Commit: ctx.Git.Commit,
Date: ctx.Date,
Runtime: metaRuntime{
Goos: ctx.Runtime.Goos,
Goarch: ctx.Runtime.Goarch,
},
}, "metadata.json")
}

Expand All @@ -51,10 +55,16 @@ func writeJSON(ctx *context.Context, j interface{}, name string) error {
}

type metadata struct {
ProjectName string `json:"project_name"`
Tag string `json:"tag"`
PreviousTag string `json:"previous_tag"`
Version string `json:"version"`
Commit string `json:"commit"`
Date time.Time `json:"date"`
ProjectName string `json:"project_name"`
Tag string `json:"tag"`
PreviousTag string `json:"previous_tag"`
Version string `json:"version"`
Commit string `json:"commit"`
Date time.Time `json:"date"`
Runtime metaRuntime `json:"runtime"`
}

type metaRuntime struct {
Goos string `json:"goos"`
Goarch string `json:"goarch"`
}
4 changes: 4 additions & 0 deletions internal/pipe/metadata/metadata_test.go
Expand Up @@ -27,6 +27,10 @@ func TestRun(t *testing.T) {
Dist: tmp,
ProjectName: "name",
})
ctx.Runtime = context.Runtime{
Goos: "fakeos",
Goarch: "fakearch",
}
ctx.Version = "1.2.3"
ctx.Git = context.GitInfo{
CurrentTag: "v1.2.3",
Expand Down
@@ -1 +1 @@
{"project_name":"name","tag":"v1.2.3","previous_tag":"v1.2.2","version":"1.2.3","commit":"aef34a","date":"2022-01-22T10:12:13Z"}
{"project_name":"name","tag":"v1.2.3","previous_tag":"v1.2.2","version":"1.2.3","commit":"aef34a","date":"2022-01-22T10:12:13Z","runtime":{"goos":"fakeos","goarch":"fakearch"}}
2 changes: 2 additions & 0 deletions internal/tmpl/tmpl.go
Expand Up @@ -52,6 +52,7 @@ const (
timestamp = "Timestamp"
modulePath = "ModulePath"
releaseNotes = "ReleaseNotes"
runtimeK = "Runtime"

// artifact-only keys.
osKey = "Os"
Expand Down Expand Up @@ -102,6 +103,7 @@ func New(ctx *context.Context) *Template {
prerelease: ctx.Semver.Prerelease,
isSnapshot: ctx.Snapshot,
releaseNotes: ctx.ReleaseNotes,
runtimeK: ctx.Runtime,
},
}
}
Expand Down
3 changes: 3 additions & 0 deletions internal/tmpl/tmpl_test.go
Expand Up @@ -3,6 +3,7 @@ package tmpl
import (
"os"
"path/filepath"
"runtime"
"testing"
"text/template"

Expand Down Expand Up @@ -62,6 +63,8 @@ func TestWithArtifact(t *testing.T) {
"v1.2.2": "{{ .PreviousTag }}",
"awesome release": "{{ .TagSubject }}",
"awesome release\n\nanother line": "{{ .TagContents }}",
"runtime: " + runtime.GOOS: "runtime: {{ .Runtime.Goos }}",
"runtime: " + runtime.GOARCH: "runtime: {{ .Runtime.Goarch }}",
} {
tmpl := tmpl
expect := expect
Expand Down
11 changes: 11 additions & 0 deletions pkg/context/context.go
Expand Up @@ -9,6 +9,7 @@ package context
import (
ctx "context"
"os"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -98,6 +99,12 @@ type Context struct {
Deprecated bool
Parallelism int
Semver Semver
Runtime Runtime
}

type Runtime struct {
Goos string
Goarch string
}

// Semver represents a semantic version.
Expand Down Expand Up @@ -129,6 +136,10 @@ func Wrap(ctx ctx.Context, config config.Project) *Context {
Parallelism: 4,
Artifacts: artifact.New(),
Date: time.Now(),
Runtime: Runtime{
Goos: runtime.GOOS,
Goarch: runtime.GOARCH,
},
}
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/context/context_test.go
Expand Up @@ -2,6 +2,7 @@ package context

import (
"os"
"runtime"
"testing"
"time"

Expand All @@ -20,6 +21,8 @@ func TestNew(t *testing.T) {
require.Equal(t, "BAR", ctx.Env["FOO"])
require.Equal(t, "1", ctx.Env["BAR"])
require.Equal(t, 4, ctx.Parallelism)
require.Equal(t, runtime.GOOS, ctx.Runtime.Goos)
require.Equal(t, runtime.GOARCH, ctx.Runtime.Goarch)
}

func TestNewWithTimeout(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions www/docs/customization/templates.md
Expand Up @@ -45,6 +45,8 @@ On fields that support templating, these fields are always available:
| `.PrefixedSummary` | the git summary prefixed with the monorepo config tag prefix (if any) |
| `.TagSubject` | the annotated tag message subject, or the message subject of the commit it points out[^6] |
| `.TagContents` | the annotated tag message, or the message of the commit it points out[^7] |
| `.Runtime.Goos` | equivalent to `runtime.GOOS` |
| `.Runtime.Goarch` | equivalent to `runtime.GOARCH` |

[^1]: The `v` prefix is stripped and it might be changed in `snapshot` and `nightly` builds.
[^2]: Assuming `Tag` is a valid a SemVer, otherwise empty/zeroed.
Expand Down

0 comments on commit 9d481d4

Please sign in to comment.