task

package module
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: MIT Imports: 42 Imported by: 0

README

Task

Task is a task runner / build tool that aims to be simpler and easier to use than, for example, GNU Make.

Installation | Documentation | Twitter | Mastodon | Discord

说明

这个项目是从 go-task/task 中 fork 过来的。

这个项目我挺喜欢,用来做 scripts 管理很方便,目前在工作中被我用来做一些常用的环境准备。

相比于 ansible,这个项目最吸引我的点有两个:

  1. 依赖很少,一个 3MB 的二进制就能跑!
  2. 基于 yaml 的配置,内容很简洁,没有 ansible 封装的奇奇怪怪的 module,所见即所得

但在使用中也遇到了一些问题,主要还是在 环境变量的传递问题!!!

作者在 Q&A 中也解释了不同 shell 间不能共享环境变量,这在同一层级的任务中其实很好理解。

我的场景是,在一个 task 中通过调用另一个 task,写法如下:


tasks:
  parent_task:
    env:
      MY_ENV: "value from parent"
    cmds:
      - echo "this is parent, and MY_ENV is $MY_ENV"
      - task: child_task
  child_task:
    cmds:
      - 'echo "this is child, and I DO NOT HAS MY_ENV !!!  see: $MY_ENV"'

这非常令人疑惑 😵😵😵 为什么在子 cmd 中的 task 却不能用父 task 的环境变量呢?

所以,我 fork 了一版,增加了一个 feature: 在子 cmd 中,若使用了 task, 则会继承父 task 的环境变量。 按照原有逻辑,子 task 的环境变量继承顺序,因此修改后的环境变量继承逻辑为:

taskfile envs => dotenv envs => parent task envs => task envs。

但是需要注意,我的改动仅实现了在一个 task 的 cmd 中调用了另一个 task 的情况,不包含使用 dep 的方式。

Documentation

Index

Constants

View Source
const (
	// MaximumTaskCall is the max number of times a task can be called.
	// This exists to prevent infinite loops on cyclic dependencies
	MaximumTaskCall = 100
)

Variables

View Source
var ErrPreconditionFailed = errors.New("task: precondition not met")

ErrPreconditionFailed is returned when a precondition fails

Functions

func FilterOutInternal

func FilterOutInternal(task *taskfile.Task) bool

FilterOutInternal removes all tasks that are marked as internal.

func FilterOutNoDesc

func FilterOutNoDesc(task *taskfile.Task) bool

FilterOutNoDesc removes all tasks that do not contain a description.

func InitTaskfile

func InitTaskfile(w io.Writer, dir string) error

InitTaskfile Taskfile creates a new Taskfile

Types

type Executor

type Executor struct {
	Taskfile *taskfile.Taskfile

	Dir         string
	TempDir     string
	Entrypoint  string
	Force       bool
	ForceAll    bool
	Watch       bool
	Verbose     bool
	Silent      bool
	AssumeYes   bool
	Dry         bool
	Summary     bool
	Parallel    bool
	Color       bool
	Concurrency int
	Interval    time.Duration
	AssumesTerm bool

	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer

	Logger      *logger.Logger
	Compiler    compiler.Compiler
	Output      output.Output
	OutputStyle taskfile.Output
	TaskSorter  sort.TaskSorter
	// contains filtered or unexported fields
}

Executor executes a Taskfile

func (*Executor) CompiledTask

func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error)

CompiledTask returns a copy of a task, but replacing variables in almost all properties using the Go template package.

func (*Executor) FastCompiledTask

func (e *Executor) FastCompiledTask(call taskfile.Call) (*taskfile.Task, error)

FastCompiledTask is like CompiledTask, but it skippes dynamic variables.

func (*Executor) GetHash

func (e *Executor) GetHash(t *taskfile.Task) (string, error)

func (*Executor) GetTask

func (e *Executor) GetTask(call taskfile.Call) (*taskfile.Task, error)

GetTask will return the task with the name matching the given call from the taskfile. If no task is found, it will search for tasks with a matching alias. If multiple tasks contain the same alias or no matches are found an error is returned.

func (*Executor) GetTaskList

func (e *Executor) GetTaskList(filters ...FilterFunc) ([]*taskfile.Task, error)

func (*Executor) InterceptInterruptSignals

func (e *Executor) InterceptInterruptSignals()

NOTE(@andreynering): This function intercepts SIGINT and SIGTERM signals so the Task process is not killed immediately and processes running have time to do cleanup work.

func (*Executor) ListTaskNames

func (e *Executor) ListTaskNames(allTasks bool)

ListTaskNames prints only the task names in a Taskfile. Only tasks with a non-empty description are printed if allTasks is false. Otherwise, all task names are printed.

func (*Executor) ListTasks

func (e *Executor) ListTasks(o ListOptions) (bool, error)

ListTasks prints a list of tasks. Tasks that match the given filters will be excluded from the list. The function returns a boolean indicating whether tasks were found and an error if one was encountered while preparing the output.

func (*Executor) Run

func (e *Executor) Run(ctx context.Context, calls ...taskfile.Call) error

Run runs Task

func (*Executor) RunTask

func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error

RunTask runs a task by its name

func (*Executor) Setup

func (e *Executor) Setup() error

func (*Executor) Status

func (e *Executor) Status(ctx context.Context, calls ...taskfile.Call) error

Status returns an error if any the of given tasks is not up-to-date

func (*Executor) ToEditorOutput

func (e *Executor) ToEditorOutput(tasks []*taskfile.Task) (*editors.Taskfile, error)

type FilterFunc

type FilterFunc func(task *taskfile.Task) bool

type ListOptions

type ListOptions struct {
	ListOnlyTasksWithDescriptions bool
	ListAllTasks                  bool
	FormatTaskListAsJSON          bool
}

ListOptions collects list-related options

func NewListOptions

func NewListOptions(list, listAll, listAsJson bool) ListOptions

NewListOptions creates a new ListOptions instance

func (ListOptions) Filters

func (o ListOptions) Filters() []FilterFunc

Filters returns the slice of FilterFunc which filters a list of taskfile.Task according to the given ListOptions

func (ListOptions) ShouldListTasks

func (o ListOptions) ShouldListTasks() bool

ShouldListTasks returns true if one of the options to list tasks has been set to true

func (ListOptions) Validate

func (o ListOptions) Validate() error

Validate validates that the collection of list-related options are in a valid configuration

Jump to

Keyboard shortcuts

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