gotools

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2023 License: MIT Imports: 15 Imported by: 5

Documentation

Overview

Provide Go linting, formatting and other basic tooling.

Some additional benefits to using this over calling natively are:

- Uses improved gofumpt over gofmt.

- Uses golines with `mage go:wrap` to automatically wrap long expressions.

- If the non-standard tooling isn't installed, it will automatically go install the required tool on calling, reducing the need to run setup processes.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetGoPath

func GetGoPath() string

GetGoPath returns the GOPATH value.

Types

type Go

type Go mg.Namespace

func (Go) Doctor

func (Go) Doctor()

🏥 Doctor will provide config details.

func (Go) Fix added in v0.0.10

func (Go) Fix() error

🔎 Run golangci-lint and apply any auto-fix.

func (Go) Fmt

func (Go) Fmt() error

✨ Fmt runs gofumpt. Export SKIP_GOLINES=1 to skip golines. Important. Make sure golangci-lint config disables gci, goimports, and gofmt. This will perform all the sorting and other linters can cause conflicts in import ordering.

func (Go) GetModuleName

func (Go) GetModuleName() string

getModuleName returns the name from the module file. Original help on this was: https://stackoverflow.com/a/63393712/68698

func (Go) Init

func (Go) Init() error

⚙️ Init runs all required steps to use this package.

func (Go) Lint

func (Go) Lint() error

🔎 Run golangci-lint without fixing.

func (Go) LintConfig

func (Go) LintConfig() error

🏥 LintConfig will return output of golangci-lint config.

func (Go) Test

func (Go) Test() error

🧪 Run go test. Optional: GOTEST_FLAGS '-tags integration', Or write your own GOTEST env logic. Example of checking based on GOTEST style environment variable:

	if !strings.Contains(strings.ToLower(os.Getenv("GOTESTS")), "slow") {
		t.Skip("GOTESTS should include 'slow' to run this test")
}.
Example
package main

import (
	"os"
	"strings"

	"github.com/pterm/pterm"
	"github.com/sheldonhull/magetools/gotools"
)

func main() {
	pterm.DisableOutput()
	if !strings.Contains(strings.ToLower(os.Getenv("GOTESTS")), "superslow") {
		return
		// t.Skip("GOTESTS should include 'slow' to run this test")
	}
	// Running as mage task
	if err := (gotools.Go{}.Test()); err != nil {
		pterm.Error.Printf("ExampleGo_Test: %v\n", err)
	}

	// Running with GOTEST_FLAGS detection
	os.Setenv("GOTEST_FLAGS", "-tags=integration")
	if err := (gotools.Go{}.Test()); err != nil {
		pterm.Error.Printf("ExampleGo_Test: %v\n", err)
	}

}
Output:

func (Go) TestSum

func (Go) TestSum(path string) error

🧪 Run gotestsum (Params: Path just like you pass to go test, ie ./..., pkg/, etc ). If gotestsum is not installed, it will install it.

- Test outputs junit, json, and coverfiles.

- Test shuffles and adds race flag.

- Test running manually like this from current repo: GOTEST_DISABLE_RACE=1 mage -d magefiles -w . -v go:testsum ./pkg/...

Example
package main

import (
	"os"
	"strings"

	"github.com/pterm/pterm"
	"github.com/sheldonhull/magetools/gotools"
)

func main() {
	pterm.DisableOutput()
	if !strings.Contains(strings.ToLower(os.Getenv("GOTESTS")), "superslow") {
		return
		// t.Skip("GOTESTS should include 'slow' to run this test")
	}
	// Running as mage task
	if err := (gotools.Go{}.TestSum("pkg")); err != nil {
		pterm.Error.Printf("ExampleGo_TestSum: %v\n", err)
	}

	// Running with GOTEST_FLAGS detection
	// os.Setenv("GOTEST_FLAGS", "-tags=integration")
	// if err := (gotools.Go{}.TestSum()); err != nil {
	// 	pterm.Error.Printf("ExampleGo_TestSum: %v", err)
	// }

}
Output:

func (Go) Tidy

func (Go) Tidy() error

🧹 Tidy tidies.

func (Go) Wrap

func (Go) Wrap() error

✨ Wrap runs golines powered by gofumpt.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL