//go:build mage package main import ( "os" "fmt" //"path/filepath" "github.com/magefile/mage/sh" ) const ( binary = "greet" ) var Default = Build // Test runs the go tests. // To enable verbose mode set GO_TEST_VERBOSE=1. // To enable coverage mode set GO_TEST_COVER=1. func Test() error { if err := changeToProjectRoot(); err != nil { return fmt.Errorf("unable to begin tests; %w", err) } goTest := sh.RunCmd("go", "test") args := []string{"."} if os.Getenv("GO_TEST_VERBOSE") == "1" { args = append(args, "-v") } if os.Getenv("GO_TEST_COVER") == "1" { args = append(args, "-cover") } return goTest(args...) } // Lint runs golangci-lint against the code. func Lint() error { if err := changeToProjectRoot(); err != nil { return fmt.Errorf("unable to start the lint tests; %w", err) } return sh.RunV("golangci-lint", "run", "--color", "always") } // Build builds the executable. func Build() error { if err := changeToProjectRoot(); err != nil { return fmt.Errorf("unable to start building the binary; %w", err) } main := "greet.go" return sh.Run("go", "build", "-o", binary, main) } // Clean cleans the workspace. func Clean() error { if err := changeToProjectRoot(); err != nil { return fmt.Errorf("unable to start the cleaning process; %w", err) } if err := sh.Rm(binary); err != nil { return err } if err := sh.Run("go", "clean", "."); err != nil { return err } return nil } func changeToProjectRoot() error { if err := os.Chdir("../.."); err != nil { return fmt.Errorf("unable to change directory; %w", err) } return nil }