mg

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2023 License: Apache-2.0 Imports: 13 Imported by: 1,269

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/magefile/mage/mg"
)

func main() {
	// Deps will run each dependency exactly once, and will run leaf-dependencies before those
	// functions that depend on them (if you put mg.Deps first in the function).

	// Normal (non-serial) Deps runs all dependencies in goroutines, so which one finishes first is
	// non-deterministic. Here we use SerialDeps here to ensure the example always produces the same
	// output.

	mg.SerialDeps(mg.F(Say, "hi"), Bark)
}

func Say(something string) {
	fmt.Println(something)
}

func Bark() {
	mg.Deps(mg.F(Say, "woof"))
}
Output:

hi
woof

Index

Examples

Constants

View Source
const AnsiColorReset = "\033[0m"

AnsiColorReset is an ANSI color code to reset the terminal color.

View Source
const CacheEnv = "MAGEFILE_CACHE"

CacheEnv is the environment variable that users may set to change the location where mage stores its compiled binaries.

View Source
const DebugEnv = "MAGEFILE_DEBUG"

DebugEnv is the environment variable that indicates the user requested debug mode when running mage.

View Source
const EnableColorEnv = "MAGEFILE_ENABLE_COLOR"

EnableColorEnv is the environment variable that indicates the user is using a terminal which supports a color output. The default is false for backwards compatibility. When the value is true and the detected terminal does support colors then the list of mage targets will be displayed in ANSI color. When the value is true but the detected terminal does not support colors, then the list of mage targets will be displayed in the default colors (e.g. black and white).

View Source
const GoCmdEnv = "MAGEFILE_GOCMD"

GoCmdEnv is the environment variable that indicates the go binary the user desires to utilize for Magefile compilation.

View Source
const HashFastEnv = "MAGEFILE_HASHFAST"

HashFastEnv is the environment variable that indicates the user requested to use a quick hash of magefiles to determine whether or not the magefile binary needs to be rebuilt. This results in faster runtimes, but means that mage will fail to rebuild if a dependency has changed. To force a rebuild, run mage with the -f flag.

View Source
const IgnoreDefaultEnv = "MAGEFILE_IGNOREDEFAULT"

IgnoreDefaultEnv is the environment variable that indicates the user requested to ignore the default target specified in the magefile.

View Source
const TargetColorEnv = "MAGEFILE_TARGET_COLOR"

TargetColorEnv is the environment variable that indicates which ANSI color should be used to colorize mage targets. This is only applicable when the MAGEFILE_ENABLE_COLOR environment variable is true. The supported ANSI color names are any of these: - Black - Red - Green - Yellow - Blue - Magenta - Cyan - White - BrightBlack - BrightRed - BrightGreen - BrightYellow - BrightBlue - BrightMagenta - BrightCyan - BrightWhite

View Source
const VerboseEnv = "MAGEFILE_VERBOSE"

VerboseEnv is the environment variable that indicates the user requested verbose mode when running a magefile.

Variables

View Source
var DefaultTargetAnsiColor = ansiColor[Cyan]

DefaultTargetAnsiColor is a default ANSI color for colorizing targets. It is set to Cyan as an arbitrary color, because it has a neutral meaning

Functions

func CacheDir

func CacheDir() string

CacheDir returns the directory where mage caches compiled binaries. It defaults to $HOME/.magefile, but may be overridden by the MAGEFILE_CACHE environment variable.

func CtxDeps added in v1.2.4

func CtxDeps(ctx context.Context, fns ...interface{})

CtxDeps runs the given functions as dependencies of the calling function. Dependencies must only be of type:

func()
func() error
func(context.Context)
func(context.Context) error

Or a similar method on a mg.Namespace type. Or an mg.Fn interface.

The function calling Deps is guaranteed that all dependent functions will be run exactly once when Deps returns. Dependent functions may in turn declare their own dependencies using Deps. Each dependency is run in their own goroutines. Each function is given the context provided if the function prototype allows for it.

func Debug added in v1.2.4

func Debug() bool

Debug reports whether a magefile was run with the debug flag.

func Deps

func Deps(fns ...interface{})

Deps runs the given functions in parallel, exactly once. Dependencies must only be of type:

func()
func() error
func(context.Context)
func(context.Context) error

Or a similar method on a mg.Namespace type. Or an mg.Fn interface.

This is a way to build up a tree of dependencies with each dependency defining its own dependencies. Functions must have the same signature as a Mage target, i.e. optional context argument, optional error return.

func EnableColor added in v1.10.0

func EnableColor() bool

EnableColor reports whether the user has requested to enable a color output.

func ExitStatus

func ExitStatus(err error) int

ExitStatus queries the error for an exit status. If the error is nil, it returns 0. If the error does not implement ExitStatus() int, it returns 1. Otherwise it retiurns the value from ExitStatus().

func Fatal

func Fatal(code int, args ...interface{}) error

Fatal returns an error that will cause mage to print out the given args and exit with the given exit code.

func Fatalf

func Fatalf(code int, format string, args ...interface{}) error

Fatalf returns an error that will cause mage to print out the given message and exit with the given exit code.

func GoCmd added in v1.3.0

func GoCmd() string

GoCmd reports the command that Mage will use to build go code. By default mage runs the "go" binary in the PATH.

func HashFast added in v1.9.0

func HashFast() bool

HashFast reports whether the user has requested to use the fast hashing mechanism rather than rely on go's rebuilding mechanism.

func IgnoreDefault added in v1.5.0

func IgnoreDefault() bool

IgnoreDefault reports whether the user has requested to ignore the default target in the magefile.

func SerialCtxDeps added in v1.2.4

func SerialCtxDeps(ctx context.Context, fns ...interface{})

SerialCtxDeps is like CtxDeps except it runs each dependency serially, instead of in parallel. This can be useful for resource intensive dependencies that shouldn't be run at the same time.

func SerialDeps added in v1.2.4

func SerialDeps(fns ...interface{})

SerialDeps is like Deps except it runs each dependency serially, instead of in parallel. This can be useful for resource intensive dependencies that shouldn't be run at the same time.

func TargetColor added in v1.10.0

func TargetColor() string

TargetColor returns the configured ANSI color name a color output.

func Verbose

func Verbose() bool

Verbose reports whether a magefile was run with the verbose flag.

Types

type Color added in v1.10.0

type Color int

Color is ANSI color type

const (
	Black Color = iota
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
	BrightBlack
	BrightRed
	BrightGreen
	BrightYellow
	BrightBlue
	BrightMagenta
	BrightCyan
	BrightWhite
)

If you add/change/remove any items in this constant, you will need to run "stringer -type=Color" in this directory again. NOTE: Please keep the list in an alphabetical order.

func (Color) String added in v1.10.0

func (i Color) String() string

type Fn added in v1.11.0

type Fn interface {
	// Name should return the fully qualified name of the function. Usually
	// it's best to use runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name().
	Name() string

	// ID should be an additional uniqueness qualifier in case the name is insufficiently unique.
	// This can be the case for functions that take arguments (mg.F json-encodes an array of the
	// args).
	ID() string

	// Run should run the function.
	Run(ctx context.Context) error
}

Fn represents a function that can be run with mg.Deps. Package, Name, and ID must combine to uniquely identify a function, while ensuring the "same" function has identical values. These are used as a map key to find and run (or not run) the function.

func F added in v1.11.0

func F(target interface{}, args ...interface{}) Fn

F takes a function that is compatible as a mage target, and any args that need to be passed to it, and wraps it in an mg.Fn that mg.Deps can run. Args must be passed in the same order as they are declared by the function. Note that you do not need to and should not pass a context.Context to F, even if the target takes a context. Compatible args are int, bool, string, and time.Duration.

Example
f := func(i int) {
	fmt.Println(i)
}

// we use SerialDeps here to ensure consistent output, but this works with all Deps functions.
SerialDeps(F(f, 5), F(f, 1))
Output:

5
1

type Namespace added in v1.2.4

type Namespace struct{}

Namespace allows for the grouping of similar commands

Jump to

Keyboard shortcuts

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