Skip to content

Commit

Permalink
feat(cmd/file_templates/go_cli_project): Add Goreleaser & Dockerfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejsika committed Aug 21, 2021
1 parent c8a37dd commit 273c1f9
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 6 deletions.
140 changes: 140 additions & 0 deletions cmd/file_templates/go_cli_project/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,145 @@ import (
func main() {
cmd.Execute()
}
`,
// .goreleaser.yml
".goreleaser.yml": `project_name: {{.ProjectName}}
before:
hooks:
- rm -rf ./dist
- go mod tidy
builds:
-
env:
- CGO_ENABLED=0
mod_timestamp: "{{"{{"}} .CommitTimestamp {{"}}"}}"
flags:
- -trimpath
ldflags:
- -s
- -w
- -X {{.Package}}/version/version.Version=v{{"{{"}}.Version{{"}}"}}
goos:
- windows
- linux
- darwin
goarch:
- amd64
- "386"
- arm
- arm64
goarm:
- 6
- 7
ignore:
- goos: darwin
goarch: "386"
- goos: windows
goarch: "arm"
- goos: windows
goarch: "arm64"
- goos: linux
goarch: arm
goarm: 6
binary: {{.ProjectName}}
archives:
- format: tar.gz
name_template: "{{"{{"}} .ProjectName {{"}}"}}_v{{"{{"}} .Version {{"}}"}}_{{"{{"}} .Os {{"}}"}}_{{"{{"}} .Arch {{"}}"}}"
release:
prerelease: auto
checksum:
name_template: "{{"{{"}} .ProjectName {{"}}"}}_checksums.txt"
algorithm: sha256
brews:
-
name: {{.ProjectName}}
conflicts:
- {{.ProjectName}}-edge
tap:
owner: {{.BrewOrganization}}
name: {{.BrewRepo}}
skip_upload: auto
homepage: https://{{.Package}}
url_template: "https://{{.Package}}/releases/download/{{"{{"}} .Tag {{"}}"}}/{{"{{"}} .ArtifactName {{"}}"}}"
folder: Formula
caveats: "How to use this binary: https://{{.Package}}"
description: "{{.ProjectName}}"
install: |
bin.install "{{.ProjectName}}"
test: |
system "#{bin}/{{.ProjectName}} version"
-
name: {{.ProjectName}}-edge
conflicts:
- {{.ProjectName}}
tap:
owner: {{.BrewOrganization}}
name: {{.BrewRepo}}
skip_upload: false
homepage: https://{{.Package}}
url_template: "https://{{.Package}}/releases/download/{{"{{"}} .Tag {{"}}"}}/{{"{{"}} .ArtifactName {{"}}"}}"
folder: Formula
caveats: "How to use this binary: https://{{.Package}}"
description: "{{.ProjectName}}"
install: |
bin.install "{{.ProjectName}}"
test: |
system "#{bin}/{{.ProjectName}} version"
dockers:
-
goos: linux
goarch: amd64
image_templates:
- "{{.DockerRegistry}}/{{.ProjectName}}:{{"{{"}} .Tag {{"}}"}}"
dockerfile: Dockerfile
ids:
- {{.ProjectName}}
build_flag_templates:
- "--platform=linux/amd64"
- "--label=org.opencontainers.image.created={{"{{"}}.Date{{"}}"}}"
- "--label=org.opencontainers.image.title={{"{{"}}.ProjectName{{"}}"}}"
- "--label=org.opencontainers.image.revision={{"{{"}}.FullCommit{{"}}"}}"
- "--label=org.opencontainers.image.version={{"{{"}}.Version{{"}}"}}"
- "--label=org.label-schema.schema-version=1.0"
- "--label=org.label-schema.version={{"{{"}}.Version{{"}}"}}"
- "--label=org.label-schema.name={{"{{"}}.ProjectName{{"}}"}}"
- "--label=com.github.actions.name={{"{{"}}.ProjectName{{"}}"}}"
- "--label=repository=https://{{.Package}}"
{{ if .Maintainer }}- "--label=maintainer={{.Maintainer}}"{{ end }}
- goos: linux
goarch: arm64
image_templates:
- "{{.DockerRegistry}}/{{.ProjectName}}:{{"{{"}} .Tag {{"}}"}}-arm64v8"
dockerfile: Dockerfile.arm64v8
ids:
- {{.ProjectName}}
build_flag_templates:
- "--platform=linux/arm64"
- "--label=org.opencontainers.image.created={{"{{"}}.Date{{"}}"}}"
- "--label=org.opencontainers.image.title={{"{{"}}.ProjectName{{"}}"}}"
- "--label=org.opencontainers.image.revision={{"{{"}}.FullCommit{{"}}"}}"
- "--label=org.opencontainers.image.version={{"{{"}}.Version{{"}}"}}"
- "--label=org.label-schema.schema-version=1.0"
- "--label=org.label-schema.version={{"{{"}}.Version{{"}}"}}"
- "--label=org.label-schema.name={{"{{"}}.ProjectName{{"}}"}}"
- "--label=com.github.actions.name={{"{{"}}.ProjectName{{"}}"}}"
- "--label=repository=https://{{.Package}}"
{{ if .Maintainer }}- "--label=maintainer={{.Maintainer}}"{{ end }}
`,
// Dockerfile
"Dockerfile": `FROM debian:10-slim
COPY {{.ProjectName}} /
ENTRYPOINT [ "/{{.ProjectName}}" ]
`,
// Dockerfile.arm64v8
"Dockerfile.arm64v8": `FROM arm64v8/debian:10-slim
COPY {{.ProjectName}} /
ENTRYPOINT [ "/{{.ProjectName}}" ]
`,
}
49 changes: 43 additions & 6 deletions cmd/file_templates/go_cli_project/go_cli_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,33 @@ import (
var CmdFlagPathPrefix string
var CmdFlagProjectName string
var CmdFlagPackage string
var CmdFlagMaintainer string
var CmdFlagBrewOrganization string
var CmdFlagBrewRepo string
var CmdFlagDockerRegistry string

type TemplateVariables struct {
ProjectName string
Package string
ProjectName string
Package string
Maintainer string
BrewOrganization string
BrewRepo string
DockerRegistry string
}

var Cmd = &cobra.Command{
Use: "go-cli-project",
Short: "Create Golang CLI project",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
tv := TemplateVariables{
ProjectName: CmdFlagProjectName,
Package: CmdFlagPackage,
Maintainer: CmdFlagMaintainer,
BrewOrganization: CmdFlagBrewOrganization,
BrewRepo: CmdFlagBrewRepo,
DockerRegistry: CmdFlagDockerRegistry,
}
for filename, content := range Files {
_ = content
fullPath := path.Join(CmdFlagPathPrefix, filename)
Expand All @@ -41,10 +57,7 @@ var Cmd = &cobra.Command{
panic(err)
}
defer f.Close()
t.Execute(f, TemplateVariables{
ProjectName: CmdFlagProjectName,
Package: CmdFlagPackage,
})
t.Execute(f, tv)
}
},
}
Expand Down Expand Up @@ -73,4 +86,28 @@ func init() {
"Package {{.Package}}",
)
Cmd.MarkFlagRequired("package")
Cmd.Flags().StringVar(
&CmdFlagMaintainer,
"maintainer",
"SikaLabs Opensource <[email protected]>",
"Package {{.Maintainer}}",
)
Cmd.Flags().StringVar(
&CmdFlagBrewOrganization,
"brew-org",
"sikalabs",
"Package {{.BrewOrganization}}",
)
Cmd.Flags().StringVar(
&CmdFlagBrewRepo,
"brew-repo",
"homebrew-tap",
"Package {{.BrewRepo}}",
)
Cmd.Flags().StringVar(
&CmdFlagDockerRegistry,
"docker-registry",
"homebrew-tap",
"Package {{.DockerRegistry}}",
)
}

0 comments on commit 273c1f9

Please sign in to comment.