Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add commands to .goreleaser.yml only if requested #562

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ arguments:
unmanaged:
type: boolean
description: If true, stencil will not generate an entrypoint go file for this CLI (`cmd/<name>/<name>.go`), but still will build and distribute it.
buildAsset:
type: boolean
description: If true, the command is added to .goreleaser.yml file, otherwise no asset is build for this command
arch:
type: array
description: List of architectures required for building the asset (by default arm64 and amd64)
items:
type: string
enum:
- arm64
- amd64
os:
type: array
description: List of os types required for building the asset (by default linux and darwin)
items:
type: string
enum:
- linux
- darwin
description: List of CLI commands to generate for this repository
grpcClients:
schema:
Expand Down
30 changes: 23 additions & 7 deletions templates/.goreleaser.yml.tpl
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
{{- if not (stencil.Arg "commands") }}
{{ file.Skip "No commands defined" }}
{{- file.Delete }}
{{- end -}}
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
before:
hooks:
- make dep
builds:
{{- range $cmdName := stencil.Arg "commands" }}
{{- $hasCommands := false }}
{{- range $index, $cmdName := stencil.Arg "commands" }}
{{- $opts := (dict) }}
{{- if kindIs "map" $cmdName }}
{{- $cmdName = (index (keys $cmdName) 0) }}
{{- $opts = (index . $cmdName | default (dict)) }}
{{- end }}
{{- if $opts.buildAsset }}
{{- if eq $index 0 }}
{{- $hasCommands = true }}
builds:
{{- end }}
- main: ./cmd/{{ $cmdName }}
id: &name {{ $cmdName }}
binary: *name
goos:
- linux
- darwin
{{- $defaultOs := (list "linux" "darwin") }}
{{- $osList := ($opts.os | default $defaultOs) }}
{{- range $os := $osList }}
- {{ $os }}
{{- end }}
goarch:
- amd64
- arm64
{{- $defaultArch := (list "arm64" "amd64") }}
{{- $archList := ($opts.arch | default $defaultArch) }}
{{- range $arch := $archList }}
- {{ $arch }}
{{- end }}
ldflags:
- '-w -s -X "github.com/getoutreach/gobox/pkg/app.Version=v{{ "{{" }} .Version {{ "}}" }}"'
{{- if not $opts.delibird }}
Expand All @@ -34,6 +45,11 @@ builds:
{{ (file.Block $blockName) | trim }}
## <</Stencil::Block>>
{{- end }}
{{- end }}
{{- if not $hasCommands }}
{{- file.Delete }}
{{- end -}}


archives: []
checksum:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(*codegen.File)(# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
before:
hooks:
- make dep<nil>archives: []
checksum:
name_template: 'checksums.txt'
release:
# We handle releasing via semantic-release
disable: true
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
(*codegen.File)(# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
before:
hooks:
- make dep
builds:
- main: ./cmd/cmd1
id: &name cmd1
binary: *name
goos:
- linux
- darwin
goarch:
- arm64
- amd64
ldflags:
- '-w -s -X "github.com/getoutreach/gobox/pkg/app.Version=v{{ .Version }}"'
- '-X "main.HoneycombTracingKey={{ .Env.HONEYCOMB_APIKEY }}"'
- '-X "main.TeleforkAPIKey={{ .Env.TELEFORK_APIKEY }}"'
env:
- CGO_ENABLED=0
## <<Stencil::Block(cmd1AdditionalEnv)>>

## <</Stencil::Block>>
- main: ./cmd/cmd2
id: &name cmd2
binary: *name
goos:
- linux
- darwin
goarch:
- arm64
- amd64
ldflags:
- '-w -s -X "github.com/getoutreach/gobox/pkg/app.Version=v{{ .Version }}"'
- '-X "main.HoneycombTracingKey={{ .Env.HONEYCOMB_APIKEY }}"'
- '-X "main.TeleforkAPIKey={{ .Env.TELEFORK_APIKEY }}"'
env:
- CGO_ENABLED=0
## <<Stencil::Block(cmd2AdditionalEnv)>>

## <</Stencil::Block>>
- main: ./cmd/cmd3-sub1
id: &name cmd3-sub1
binary: *name
goos:
- linux
- darwin
goarch:
- arm64
- amd64
ldflags:
- '-w -s -X "github.com/getoutreach/gobox/pkg/app.Version=v{{ .Version }}"'
- '-X "main.HoneycombTracingKey={{ .Env.HONEYCOMB_APIKEY }}"'
- '-X "main.TeleforkAPIKey={{ .Env.TELEFORK_APIKEY }}"'
env:
- CGO_ENABLED=0
## <<Stencil::Block(cmd3sub1AdditionalEnv)>>

## <</Stencil::Block>>
- main: ./cmd/cmd3-sub2
id: &name cmd3-sub2
binary: *name
goos:
- linux
- darwin
goarch:
- arm64
- amd64
ldflags:
- '-w -s -X "github.com/getoutreach/gobox/pkg/app.Version=v{{ .Version }}"'
- '-X "main.HoneycombTracingKey={{ .Env.HONEYCOMB_APIKEY }}"'
- '-X "main.TeleforkAPIKey={{ .Env.TELEFORK_APIKEY }}"'
env:
- CGO_ENABLED=0
## <<Stencil::Block(cmd3sub2AdditionalEnv)>>

## <</Stencil::Block>>
- main: ./cmd/cmd4_sub1
id: &name cmd4_sub1
binary: *name
goos:
- linux
- darwin
goarch:
- arm64
- amd64
ldflags:
- '-w -s -X "github.com/getoutreach/gobox/pkg/app.Version=v{{ .Version }}"'
- '-X "main.HoneycombTracingKey={{ .Env.HONEYCOMB_APIKEY }}"'
- '-X "main.TeleforkAPIKey={{ .Env.TELEFORK_APIKEY }}"'
env:
- CGO_ENABLED=0
## <<Stencil::Block(cmd4sub1AdditionalEnv)>>

## <</Stencil::Block>>archives: []
checksum:
name_template: 'checksums.txt'
release:
# We handle releasing via semantic-release
disable: true
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(*codegen.File)(# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
before:
hooks:
- make dep
builds:
- main: ./cmd/cmd1
id: &name cmd1
binary: *name
goos:
- linux
goarch:
- arm64
ldflags:
- '-w -s -X "github.com/getoutreach/gobox/pkg/app.Version=v{{ .Version }}"'
- '-X "main.HoneycombTracingKey={{ .Env.HONEYCOMB_APIKEY }}"'
- '-X "main.TeleforkAPIKey={{ .Env.TELEFORK_APIKEY }}"'
env:
- CGO_ENABLED=0
## <<Stencil::Block(cmd1AdditionalEnv)>>

## <</Stencil::Block>>archives: []
checksum:
name_template: 'checksums.txt'
release:
# We handle releasing via semantic-release
disable: true
)
60 changes: 57 additions & 3 deletions templates/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ func TestGRPCServerRPC(t *testing.T) {
"grpc",
},
})
st.Run(true)
st.Run(stenciltest.RegenerateSnapshots())
}

func TestGoreleaserYml(t *testing.T) {
func TestGoreleaserYmlOnlyCommands(t *testing.T) {
st := stenciltest.New(t, ".goreleaser.yml.tpl", libraryTmpls...)
st.Args(map[string]interface{}{
"commands": []interface{}{
Expand All @@ -244,7 +244,61 @@ func TestGoreleaserYml(t *testing.T) {
"cmd4_sub1",
},
})
st.Run(true)
st.Run(stenciltest.RegenerateSnapshots())
}

func TestGoreleaserYmlWithBuildAsset(t *testing.T) {
st := stenciltest.New(t, ".goreleaser.yml.tpl", libraryTmpls...)
st.Args(map[string]interface{}{
"commands": []any{
map[string]any{
"cmd1": map[string]any{
"buildAsset": true,
},
},
map[string]any{
"cmd2": map[string]any{
"buildAsset": true,
},
},
map[string]any{
"cmd3-sub1": map[string]any{
"buildAsset": true,
},
},
map[string]any{
"cmd3-sub2": map[string]any{
"buildAsset": true,
},
},
map[string]any{
"cmd4_sub1": map[string]any{
"buildAsset": true,
},
},
},
})
st.Run(stenciltest.RegenerateSnapshots())
}

func TestGoreleaserYmlWithSpecifiedOrAndArch(t *testing.T) {
st := stenciltest.New(t, ".goreleaser.yml.tpl", libraryTmpls...)
st.Args(map[string]interface{}{
"commands": []any{
map[string]any{
"cmd1": map[string]any{
"buildAsset": true,
"os": []any{
"linux",
},
"arch": []any{
"arm64",
},
},
},
},
})
st.Run(stenciltest.RegenerateSnapshots())
}

func TestRenderGolangcilintYaml(t *testing.T) {
Expand Down