Skip to content

Commit

Permalink
Standardise goreleaser, release drafter and justfile (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess committed May 8, 2023
1 parent e34497b commit 26c470b
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 50 deletions.
40 changes: 31 additions & 9 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ concurrency:
group: ${{ github.ref }}
cancel-in-progress: true

env:
GO_VERSION: "1.20"

jobs:
test:
name: Test
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
os:
- ubuntu-latest
- macos-latest
- windows-latest

steps:
- name: Set up Go
- name: Set up Go ${{ env.GO_VERSION }}
uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: ${{ env.GO_VERSION }}

- name: Checkout Code
uses: actions/checkout@v3
Expand All @@ -40,10 +46,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Set up Go
- name: Set up Go ${{ env.GO_VERSION }}
uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: ${{ env.GO_VERSION }}

- name: Checkout Code
uses: actions/checkout@v3
Expand All @@ -57,7 +63,9 @@ jobs:
name: Release
runs-on: ubuntu-latest

needs: [test, lint]
needs:
- test
- lint

if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')

Expand All @@ -70,16 +78,30 @@ jobs:
- name: Fetch Existing Tags
run: git fetch --force --tags

- name: Set up Go
- name: Parse Release Version
id: version
run: |
VERSION=${GITHUB_REF#refs/tags/v}
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Set up Go ${{ env.GO_VERSION }}
uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: ${{ env.GO_VERSION }}

- name: Publish Release Notes
uses: release-drafter/release-drafter@v5
with:
version: ${{ steps.version.outputs.version }}
publish: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4
with:
version: latest
args: release --rm-dist
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HOMEBREW_TAP_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN }}
29 changes: 27 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
linters:
disable-all: true
enable:
- bodyclose
- containedctx
- durationcheck
- errcheck
- errchkjson
- errname
- exhaustive
- exportloopref
- goconst
- gocritic
- gocyclo
- godot
- gofmt
- gofumpt
- goimports
- gomnd
- gosimple
- govet
- ineffassign
- misspell
- nilerr
- nilnil
- nolintlint
- nonamedreturns
- predeclared
- reassign
- revive
- staticcheck
- tagliatelle
- tenv
- thelper
- unconvert
- unparam
- unused
- whitespace

Expand All @@ -23,7 +41,14 @@ linters-settings:
min-complexity: 20

staticcheck:
go: "1.19"
go: "1.20"
checks: ["all"]

errcheck:
check-type-assertions: true
check-blank: true

gosimple:
checks: ["all"]

govet:
Expand Down
18 changes: 11 additions & 7 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ builds:
- -s -w
- -X github.com/FollowTheProcess/tag/cli/cmd.version={{.Version}}
- -X github.com/FollowTheProcess/tag/cli/cmd.commit={{.Commit}}
- -X github.com/FollowTheProcess/tag/cli/cmd.buildDate={{.Date}}
- -X github.com/FollowTheProcess/tag/cli/cmd.builtBy=GoReleaser
env:
- CGO_ENABLED=0
goos:
Expand All @@ -25,7 +27,7 @@ builds:
- arm64

brews:
- goarm: "6"
- goarm: "7"
tap:
owner: "FollowTheProcess"
name: homebrew-tap
Expand All @@ -43,12 +45,14 @@ brews:
"#{bin}/tag --version"
archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
- id: tag
name_template: >-
{{ .ProjectName }}-
{{- .Version }}-
{{- .Os }}-
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
checksum:
name_template: "checksums.txt"
Expand Down
4 changes: 2 additions & 2 deletions cli/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func setup(t *testing.T) (string, func()) {
if err != nil {
t.Fatalf("Could not create temp dir: %v", err)
}
err = os.WriteFile(filepath.Join(tmp, "README.md"), []byte("Hello, version 0.1.0"), 0755)
err = os.WriteFile(filepath.Join(tmp, "README.md"), []byte("Hello, version 0.1.0"), 0o755)
if err != nil {
t.Fatalf("Could not create README.md: %v", err)
}
Expand All @@ -40,7 +40,7 @@ func setup(t *testing.T) (string, func()) {
]
`)

err = os.WriteFile(filepath.Join(tmp, ".tag.toml"), cfg, 0755)
err = os.WriteFile(filepath.Join(tmp, ".tag.toml"), cfg, 0o755)
if err != nil {
t.Fatalf("Could not create .tag.toml: %v", err)
}
Expand Down
14 changes: 7 additions & 7 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
package cmd

import (
"fmt"
"os"

"github.com/FollowTheProcess/tag/cli/app"
"github.com/FollowTheProcess/tag/config"
"github.com/MakeNowJust/heredoc/v2"
"github.com/fatih/color"
"github.com/spf13/cobra"
)

var (
version = "dev" // tag version, set at compile time by ldflags
commit = "" // tag commit hash, set at compile time by ldflags
headerStyle = color.New(color.FgWhite, color.Bold) // Setting header style to use in usage message (usage.go)
tagApp = app.New(os.Stdout, os.Stderr, config.Path) // The tag app instance, initialised once and shared between all files in this pkg
version = "dev" // tag version, set at compile time by ldflags
commit = "" // tag version's commit hash, set at compile time by ldflags
buildDate = "" // tag build date, set at compile time by ldflags
builtBy = "" // tag built by, set at compile time by ldflags
tagApp = app.New(os.Stdout, os.Stderr, config.Path) // The tag app instance, initialised once and shared between all files in this pkg

)

// BuildRootCmd builds and returns the root tag CLI command.
Expand Down Expand Up @@ -51,7 +51,7 @@ func BuildRootCmd() *cobra.Command {

// Set our custom version and usage templates
rootCmd.SetUsageTemplate(usageTemplate)
rootCmd.SetVersionTemplate(fmt.Sprintf(`{{printf "%s %s\n%s %s\n"}}`, headerStyle.Sprint("Version:"), version, headerStyle.Sprint("Commit:"), commit))
rootCmd.SetVersionTemplate(versionTemplate)

// Add subcommands
rootCmd.AddCommand(
Expand Down
20 changes: 12 additions & 8 deletions cli/cmd/usage.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package cmd

import "fmt"
import (
"fmt"

var (
// Custom usage template with the header style applied, here by itself because it looks kind of messy.
usageTemplate = fmt.Sprintf(`%s:{{if .Runnable}}
"github.com/fatih/color"
)

var headerStyle = color.New(color.FgWhite, color.Bold) // Setting header style to use in usage message (usage.go)

// Custom usage template with the header style applied, here by itself because it looks kind of messy.
var usageTemplate = fmt.Sprintf(`%s:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}}{{end}}{{if gt (len .Aliases) 0}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
%s:
{{.NameAndAliases}}{{end}}{{if .HasExample}}
Expand All @@ -28,6 +33,5 @@ Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`,
headerStyle.Sprint("USAGE"), headerStyle.Sprint("ALIASES"), headerStyle.Sprint("EXAMPLES"), headerStyle.Sprint("AVAILABLE COMMANDS"),
headerStyle.Sprint("FLAGS"), headerStyle.Sprint("GLOBAL FLAGS"))
)
headerStyle.Sprint("USAGE"), headerStyle.Sprint("ALIASES"), headerStyle.Sprint("EXAMPLES"), headerStyle.Sprint("AVAILABLE COMMANDS"),
headerStyle.Sprint("FLAGS"), headerStyle.Sprint("GLOBAL FLAGS"))
15 changes: 15 additions & 0 deletions cli/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cmd

import "fmt"

var versionTemplate = fmt.Sprintf(
`{{printf "%s %s\n%s %s\n%s %s\n%s %s\n"}}`,
headerStyle.Sprint("Version:"),
version,
headerStyle.Sprint("Commit:"),
commit,
headerStyle.Sprint("Build Date:"),
buildDate,
headerStyle.Sprint("Built By:"),
builtBy,
)
2 changes: 2 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func TestRender(t *testing.T) {

// makeConfigFile creates a temporary config file, returning it's path.
func makeConfigFile(t *testing.T) string {
t.Helper()
file, err := os.CreateTemp("", "tag.toml")
if err != nil {
t.Fatalf("CreateTemp returned an error: %v", err)
Expand All @@ -132,6 +133,7 @@ func makeConfigFile(t *testing.T) string {
// makeAltConfigFile creates a temporary config file with alternative TOML syntax
// returning it's path.
func makeAltConfigFile(t *testing.T) string {
t.Helper()
file, err := os.CreateTemp("", "tag.toml")
if err != nil {
t.Fatalf("CreateTemp returned an error: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestExecCommandHelper(t *testing.T) {
}

fmt.Fprint(os.Stdout, os.Getenv("STDOUT"))
i, _ := strconv.Atoi(os.Getenv("EXIT_STATUS"))
i, _ := strconv.Atoi(os.Getenv("EXIT_STATUS")) //nolint: errcheck // Ignore error here
os.Exit(i)
}

Expand Down
18 changes: 5 additions & 13 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,24 @@ tidy:

# Compile the project binary
build: tidy fmt
go build -ldflags="-X {{ VERSION_LDFLAG }}=dev -X {{ COMMIT_LDFLAG }}={{ COMMIT_SHA }}" -o {{ PROJECT_BIN }}/{{ PROJECT_NAME }} {{ PROJECT_ENTRY_POINT }}
mkdir -p {{ PROJECT_BIN }}
goreleaser build --single-target --skip-before --snapshot --clean --output {{ PROJECT_BIN }}/{{ PROJECT_NAME }}

# Run go fmt on all project files
fmt:
go fmt ./...

# Run all project unit tests
test *flags: fmt
gotest -race ./... {{ flags }}

# Run all project benchmarks
bench: fmt
go test ./... -run=None -benchmem -bench .

# Generate and view a CPU/Memory profile
pprof pkg type:
go test ./{{ pkg }} -cpuprofile cpu.pprof -memprofile mem.pprof -bench .
go tool pprof -http=:8000 {{ type }}.pprof
go test -race ./... {{ flags }}

# Lint the project and auto-fix errors if possible
lint: fmt
golangci-lint run --fix

# Calculate test coverage and render the html
cover:
gotest -race -cover -coverprofile={{ COVERAGE_DATA }} ./...
go test -race -cover -covermode=atomic -coverprofile={{ COVERAGE_DATA }} ./...
go tool cover -html={{ COVERAGE_DATA }} -o {{ COVERAGE_HTML }}
open {{ COVERAGE_HTML }}

Expand All @@ -69,4 +61,4 @@ install: uninstall build

# Uninstall the project from your machine
uninstall:
rm -rf $GOBIN/{{ PROJECT_NAME }}
rm -rf $GOBIN/{{ PROJECT_NAME }}
2 changes: 1 addition & 1 deletion replacer/replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ func Replace(path, search, replace string) error {

newContent := bytes.ReplaceAll(contents, []byte(search), []byte(replace))

return os.WriteFile(path, newContent, 0o755)
return os.WriteFile(path, newContent, os.ModePerm)
}

0 comments on commit 26c470b

Please sign in to comment.