Skip to content

Commit

Permalink
feat: add go-format example target
Browse files Browse the repository at this point in the history
Runs `golangci-lint --fix` and `golines`.
  • Loading branch information
odsod committed Mar 8, 2024
1 parent df6d92b commit 714a2b0
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .sage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"go.einride.tech/sage/tools/sggo"
"go.einride.tech/sage/tools/sggolangcilint"
"go.einride.tech/sage/tools/sggolicenses"
"go.einride.tech/sage/tools/sggolines"
"go.einride.tech/sage/tools/sggoreview"
"go.einride.tech/sage/tools/sgmdformat"
"go.einride.tech/sage/tools/sgyamlfmt"
Expand Down Expand Up @@ -51,6 +52,16 @@ func GoLint(ctx context.Context) error {
return sggolangcilint.Run(ctx)
}

func GoLintFix(ctx context.Context) error {
sg.Logger(ctx).Println("fixing Go files...")
return sggolangcilint.Fix(ctx)
}

func GoFormat(ctx context.Context) error {
sg.Logger(ctx).Println("formatting Go files...")
return sggolines.Run(ctx)
}

func GoLicenses(ctx context.Context) error {
sg.Logger(ctx).Println("checking Go licenses...")
return sggolicenses.Check(ctx)
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ format-yaml: $(sagefile)
git-verify-no-diff: $(sagefile)
@$(sagefile) GitVerifyNoDiff

.PHONY: go-format
go-format: $(sagefile)
@$(sagefile) GoFormat

.PHONY: go-licenses
go-licenses: $(sagefile)
@$(sagefile) GoLicenses
Expand All @@ -75,6 +79,10 @@ go-licenses: $(sagefile)
go-lint: $(sagefile)
@$(sagefile) GoLint

.PHONY: go-lint-fix
go-lint-fix: $(sagefile)
@$(sagefile) GoLintFix

.PHONY: go-mod-tidy
go-mod-tidy: $(sagefile)
@$(sagefile) GoModTidy
Expand Down
11 changes: 11 additions & 0 deletions example/.sage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"go.einride.tech/sage/tools/sggo"
"go.einride.tech/sage/tools/sggolangcilint"
"go.einride.tech/sage/tools/sggolicenses"
"go.einride.tech/sage/tools/sggolines"
"go.einride.tech/sage/tools/sggoreview"
"go.einride.tech/sage/tools/sgmdformat"
"go.einride.tech/sage/tools/sgyamlfmt"
Expand Down Expand Up @@ -54,6 +55,16 @@ func GoLint(ctx context.Context) error {
return sggolangcilint.Run(ctx)
}

func GoLintFix(ctx context.Context) error {
sg.Logger(ctx).Println("fixing Go files...")
return sggolangcilint.Fix(ctx)
}

func GoFormat(ctx context.Context) error {
sg.Logger(ctx).Println("formatting Go files...")
return sggolines.Run(ctx)
}

func GoLicenses(ctx context.Context) error {
sg.Logger(ctx).Println("checking Go licenses...")
return sggolicenses.Check(ctx)
Expand Down
53 changes: 53 additions & 0 deletions tools/sggofumpt/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package sggofumpt

import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"

"go.einride.tech/sage/sg"
"go.einride.tech/sage/sgtool"
)

const (
name = "gofumpt"
version = "0.6.0"
)

// Command returns an [*exec.Cmd] for golines.
func Command(ctx context.Context, args ...string) *exec.Cmd {
sg.Deps(ctx, PrepareCommand)
return sg.Command(ctx, sg.FromBinDir(name), args...)
}

func PrepareCommand(ctx context.Context) error {
binDir := sg.FromToolsDir(name, version)
binary := filepath.Join(binDir, name)
hostOS := runtime.GOOS
hostArch := runtime.GOARCH
binURL := fmt.Sprintf(
"https://github.com/mvdan/gofumpt/"+
"releases/download/v%s/gofumpt_v%s_%s_%s",
version,
version,
hostOS,
hostArch,
)
if err := sgtool.FromRemote(
ctx,
binURL,
sgtool.WithDestinationDir(binDir),
sgtool.WithRenameFile(fmt.Sprintf("gofumpt_v%s_%s_%s", version, hostOS, hostArch), name),
sgtool.WithSkipIfFileExists(binary),
sgtool.WithSymlink(binary),
); err != nil {
return fmt.Errorf("unable to download %s: %w", name, err)
}
if err := os.Chmod(binary, 0o755); err != nil {
return fmt.Errorf("unable to make %s command: %w", name, err)
}
return nil
}
15 changes: 15 additions & 0 deletions tools/sggolines/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,28 @@ import (

"go.einride.tech/sage/sg"
"go.einride.tech/sage/sgtool"
"go.einride.tech/sage/tools/sggofumpt"
)

const (
name = "golines"
version = "0.12.2"
)

// Run golines on all Go files in the current git root with gofumpt as default formatter.
func Run(ctx context.Context) error {
sg.Deps(ctx, sggofumpt.PrepareCommand)
return Command(
ctx,
"--base-formatter=gofumpt",
"--ignore-generated",
"--max-len=120",
"--tab-len=1",
"--write-output",
".",
).Run()
}

// Command returns an [*exec.Cmd] for golines.
func Command(ctx context.Context, args ...string) *exec.Cmd {
sg.Deps(ctx, PrepareCommand)
Expand Down

0 comments on commit 714a2b0

Please sign in to comment.