From 8a1d208b496b42ccf5b18af2d80af6b27dbe3d85 Mon Sep 17 00:00:00 2001 From: Joshua Rich Date: Tue, 28 May 2024 10:45:43 +1000 Subject: [PATCH] build(mage): :rewind: revert back to slog over fmt.Println --- build/magefiles/build.go | 8 +++----- build/magefiles/checks.go | 10 +++++----- build/magefiles/helpers.go | 5 +++-- build/magefiles/package.go | 5 +++-- build/magefiles/preps.go | 7 ++++--- build/magefiles/tests.go | 5 +++-- 6 files changed, 21 insertions(+), 19 deletions(-) diff --git a/build/magefiles/build.go b/build/magefiles/build.go index 5c64eeeb2..860bf957e 100644 --- a/build/magefiles/build.go +++ b/build/magefiles/build.go @@ -7,7 +7,7 @@ package main import ( "errors" - "fmt" + "log/slog" "github.com/magefile/mage/mg" "github.com/magefile/mage/sh" @@ -19,7 +19,7 @@ var ErrBuildFailed = errors.New("build failed") // Full runs all prep steps and then builds the binary. func (Build) Full() error { - fmt.Println("Starting full build.") + slog.Info("Starting full build.") // Make everything nice, neat, and proper mg.Deps(Preps.Tidy) @@ -62,8 +62,6 @@ func buildProject() error { output := "dist/go-hass-agent-" + targetArch - fmt.Println("Running go build...") - fmt.Println("Output binary", output) - fmt.Println("Additional ldflags", ldflags) + slog.Info("Running go build...", "output", output, "ldflags", ldflags) return sh.RunWithV(envMap, "go", "build", "-ldflags="+ldflags, "-o", output) } diff --git a/build/magefiles/checks.go b/build/magefiles/checks.go index 896800fa9..529825a4e 100644 --- a/build/magefiles/checks.go +++ b/build/magefiles/checks.go @@ -6,7 +6,7 @@ package main import ( - "fmt" + "log/slog" "os" "github.com/magefile/mage/mg" @@ -21,13 +21,13 @@ type Checks mg.Namespace // Lint runs various static checkers to ensure you follow The Rules(tm). func (Checks) Lint() error { - fmt.Println("Running linter (go vet)...") + slog.Info("Running linter (go vet)...") if err := sh.RunV("golangci-lint", "run"); err != nil { - fmt.Println("WARN: linter had problems") + slog.Warn("Linter reported issues.", "error", err.Error()) } if FoundOrInstalled("staticcheck", "honnef.co/go/tools/cmd/staticcheck@latest") { - fmt.Println("Running linter (staticcheck)...") + slog.Info("Running linter (staticcheck)...") if err := sh.RunV("staticcheck", "-f", "stylish", "./..."); err != nil { return err } @@ -38,7 +38,7 @@ func (Checks) Lint() error { // Licenses pulls down any dependent project licenses, checking for "forbidden ones". func (Checks) Licenses() error { - fmt.Println("Running go-licenses...") + slog.Info("Running go-licenses...") // Make the directory for the license files err := os.MkdirAll("licenses", os.ModePerm) diff --git a/build/magefiles/helpers.go b/build/magefiles/helpers.go index 48f468526..5101410bc 100644 --- a/build/magefiles/helpers.go +++ b/build/magefiles/helpers.go @@ -8,6 +8,7 @@ package main import ( "errors" "fmt" + "log/slog" "os" "os/exec" "runtime" @@ -55,7 +56,7 @@ func SudoWrap(cmd string, args ...string) error { func FoundOrInstalled(executableName, installURL string) (isInstalled bool) { _, missing := exec.LookPath(executableName) if missing != nil { - fmt.Println("Installing tool", executableName, "from url", installURL) + slog.Info("Installing tool.", "tool", executableName, "url", installURL) err := sh.Run("go", "install", installURL) if err != nil { return false @@ -144,7 +145,7 @@ func GenerateEnv() (map[string]string, error) { targetArch = v } if targetArch != "" && targetArch != runtime.GOARCH { - fmt.Println("Cross compilation request from host arch", runtime.GOARCH, "to target arch", targetArch) + slog.Info("Cross compilation requested.", "host", runtime.GOARCH, "target", targetArch) // Update NFPM_ARCH tro the target arch. envMap["NFPM_ARCH"] = targetArch diff --git a/build/magefiles/package.go b/build/magefiles/package.go index 52ca00974..a9aea5a3b 100644 --- a/build/magefiles/package.go +++ b/build/magefiles/package.go @@ -8,6 +8,7 @@ package main import ( "errors" "fmt" + "log/slog" "slices" "github.com/magefile/mage/mg" @@ -38,7 +39,7 @@ func (Package) Nfpm() error { } for _, pkgformat := range pkgformats { - fmt.Println("Building package with nfpm in format:", pkgformat) + slog.Info("Building package with nfpm.", "format", pkgformat) args := slices.Concat(nfpmBaseArgs, []string{"--packager", pkgformat}) if err := sh.RunWithV(envMap, "nfpm", args...); err != nil { return err @@ -54,7 +55,7 @@ func (Package) FyneCross() error { } if err := fyneCrossCmd("-arch", targetArch); err != nil { - fmt.Println("fyne-cross finished but with errors. Continuing anyway.", "Error was:", err.Error()) + slog.Warn("fyne-cross finished but with errors. Continuing anyway.", "error", err.Error()) } return sh.Copy( "fyne-cross/dist/linux-"+targetArch+"/go-hass-agent-"+targetArch+".tar.xz", diff --git a/build/magefiles/preps.go b/build/magefiles/preps.go index 972f1ef70..90874381d 100644 --- a/build/magefiles/preps.go +++ b/build/magefiles/preps.go @@ -7,6 +7,7 @@ package main import ( "fmt" + "log/slog" "os" "runtime" @@ -24,13 +25,13 @@ var generators = map[string]string{ // Tidy runs go mod tidy to update the go.mod and go.sum files. func (Preps) Tidy() error { - fmt.Println("Running go mod tidy...") + slog.Info("Running go mod tidy...") return sh.Run("go", "mod", "tidy") } // Format prettifies your code in a standard way to prevent arguments over curly braces. func (Preps) Format() error { - fmt.Println("Running go fmt...") + slog.Info("Running go fmt...") return sh.RunV("go", "fmt", "./...") } @@ -42,7 +43,7 @@ func (Preps) Generate() error { } } - fmt.Println("Running go generate...") + slog.Info("Running go generate...") return sh.RunV("go", "generate", "-v", "./...") } diff --git a/build/magefiles/tests.go b/build/magefiles/tests.go index 85e988fea..cdb5ee1ad 100644 --- a/build/magefiles/tests.go +++ b/build/magefiles/tests.go @@ -7,6 +7,7 @@ package main import ( "fmt" + "log/slog" "github.com/magefile/mage/mg" "github.com/magefile/mage/sh" @@ -21,13 +22,13 @@ func (Tests) Test() error { return fmt.Errorf("cannot run test: %w", err) } - fmt.Println("Running go test...") + slog.Info("Running go test...") return sh.RunV("go", "test", "-ldflags="+ldflags, "-coverprofile=coverage.txt", "-v", "./...") } // Benchmark runs go test -bench on the project. func (Tests) Benchmark() error { - fmt.Println("Running go test -bench...") + slog.Info("Running go test -bench...") return sh.RunV("go", "test", "-bench=.", "./...") }