Categorygithub.com/wfscheper/magelib
modulepackage
0.7.0
Repository: https://github.com/wfscheper/magelib.git
Documentation: pkg.go.dev

# README

Magelib

magelib is a collection of mage targets. Currently, magelib intended for go development. Other languages may be added when needed.

Usage

Follow the usual method for importing mage targets:

// mage:import
_ "github.com/wfscheper/magelib"

Configuration

magelib has several configurable options:

Executable name

If your project produces a binary, then set the magelib.ExeName variable to the name of your executable. Setting this is best done in an init function:

func init() {
    magelib.ExecName = "mycommand"
}

Main package

If your main package is not at the root of your project, then set the magelib.MainPackage variable to the full path of your main package. Setting this is best done in an init function:

func init() {
    magelib.MainPackage = "example.com/foo/cmd/foo"
}

Dry run mode

Some magelib targets support a "dry run" mode, where they only report what would be done. This is best set via an environment variable:

MAGELIB_DRY_RUN=true mage

Project Versioning

magelib uses gotagger to version projects based on their commit history and tags. This requires using [conventional commits]. If your project does not wish to adopt conventional commits, or if you want direct control over your project's version, then set the magelib.ProjectVersion variable. Setting this is best done in an init function:

func init() {
    magelib.ProjectVersion = "1.2.3"
}

For situations where the full git history is not available, you can also set the project version via the MAGELIB_VERSION environment variable.

MAGELIB_VERSION=1.2.3

Ignore go modules

For magelib projects that are not themselves written in golang, you will want to disable strict module versioning rules by setting the magelib.IgnoreModules variable. Setting this is best done in an init function:

func init() {
    magelib.IgnoreModules = true
}

Changelog Management

magelib uses stentor for changelog management.

Tools

magelib itself uses several external tools:

As a user of magelib, you control the version of the built-in tools, and you can add your own tools via the ProjecTools variable.

As a convenience, magelib exposes tool function factories for the built-in tools, as well as a generic go tool factory.

An example setting the version of golangci-lint, gotestsum, and a custom tool rice

const moduleRice = "github.com/GeertJohan/go.rice/rice"

var getRice = magelib.GetGoTool(moduleRice, "rice", "v1.0.0")

func init() {
    // map module import path to tool build function
    magelib.ProjectTools = magelib.ToolMap{
        magelib.ModuleGolangciLint: magelib.GetGolangciLint("v1.26.0"),
        magelib.ModuleGotestsum:    magelib.GetGotestsum("v0.4.1"),
        moduleRice:                 getRice,
    }
}

Custom target deps

You can add your own mage targets as dependencies of the built-in targets via the "<target>Deps" slices.

An example of building rice as a go:generate dependency. This example uses the getRice tool function defined in the pervious section.

func init() {
    magelib.GenerateDeps = []interface{}{
        func(ctx context.Context) error { return getRice(ctx) },
    }
}

Additionally, the go:generate target has a "rebuild" function that provides a hook for determining if the target needs to run or not.

func init() {
    magelib.GenerateRebuild = func(ctx context.Context) (bool, error) {
        return target.Dir(
            filepath.Join("internal", "templates", "rice-box.go"),
            filepath.Join("internal", "templates", "templates"),
        )
    }
}

Complete example

// +build mage

package main

import (
    "context"

    "github.com/magefile/mage/mg"

    // mage:import
    "github.com/wfscheper/magelib"
)

var (
    // Default mage target
    Default = All

    getGolangciLint = magelib.GetGolangciLint("v1.32.2")
    getGoreleaser   = magelib.GetGoreleaser("v0.146.0")
    getGotestsum    = magelib.GetGotestsum("v0.6.0")
)

func init() {
    magelib.ExeName = "example"
    magelib.MainPackage = "example.com/example/cmd/example"

    magelib.LintDeps = []interface{}{
        func(ctx context.Context) error { return getGolangciLint(ctx) },
    }
    magelib.ReleaseDeps = []interface{}{
        func(ctx context.Context) error { return getGoreleaser(ctx) },
    }
    magelib.TestDeps = []interface{}{
        func(ctx context.Context) error { return getGotestsum(ctx) },
    }

    magelib.ProjectTools = magelib.ToolMap{
        magelib.ModuleGolangciLint: getGolangciLint,
        magelib.ModuleGoreleaser:   getGoreleaser,
        magelib.ModuleGotestsum:    getGotestsum,
    }
}

// All runs format, lint, build, and test targets
func All(ctx context.Context) {
    mg.SerialCtxDeps(ctx, magelib.Go.Format, magelib.Go.Lint, magelib.Go.Build, magelib.Go.Test)
}

# Functions

Changelog reports the changelog update for the current release.
GetGolangciLint returns a ToolFunc that uses `go get` to install a specific version of golangci-lint .
GetGoreleaser returns a ToolFunc that uses `go get` to install a specific versino of goreleaser.
GetGotagger returns a ToolFunc that uses `go get` to install a specific versin of gotagger.
GetGotestsum returns a ToolFunc that uses `go get` to install a specific version of gotestsum.
GetGoTool returns a ToolFunc that uses `go install` to install a specific version of a module as name.
GetStentor returns a ToolFunc that uses `go get` to install a specific version of stentor.
Say prints the formatted string to stdout.
Version reports the current project version.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

BuildDeps is a slice of targets that are dependencies for go:build.
ChangelogDeps is a slice of targets that are dependencies for the changelog target.
CleanPaths are additional paths to delete when running the clean target.
DryRun makes co-operating targets only report what they would do.
ExeName sets the filename of a CLI executable.
GenerateDeps is a slice of targets that are dependencies for go:generate.
GenerateRebuild is a function that returns whether go:generate needs to run or not.
IgnoreModules tells the Version target to ignore the presence of go.mod files.
LintDeps is a slice of targets that are dependencies for go:lint.
MainPackage sets the full package name of the main package This is needed if main.go isn't at the root.
ProjectTools maps tool names to a ToolFunc that installs the tool.
ProjectVersion is the version of the project.
ReleaseDeps is a slice of targets that are dependencies for go:release.
TestDeps is a slice of targets that are dependencies for go:test.
TestTimeout sets the duration tests are allowed to run.
VersionDeps is a slice of targets that are dependencies for the version target.

# Type aliases

No description provided by the author
ToolFunc is a function that installs a tool.
No description provided by the author
Tools is the naespace for all build tool targets.