gotaskr

package module
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2024 License: MIT Imports: 10 Imported by: 0

README

gotaskr

Go Reference Docs wiki GitHub GitHub tag (latest SemVer)

Introduction

gotaskr (Go-Task-Runner) is a generic task runner which is invoked via CLI.

The tasks are written in plan Go and can easily be called from the CLI. This is especially usefull for tasks in the CI.

The tasks can be chained and in the end, there is a statistic about the tasks that were executed and their runtime.

There are some inbuilt helpers for often used things for various DevOps tasks.

Features

  • Compilable or directly runnable with go run
  • Tasks are written in plain Go and everything from Go can be used
  • Small footprint and easily extendable
  • Fanzy statistics after the execution
  • Custom Arguments which are named (non-positional) and can be optional
  • Chainable tasks
  • Setup and Teardown methods
  • Output from subprocesses directly visible
  • Inbuilt helpers for various DevOps tasks
  • VSCode Plugin to easily run tasks with a single click
  • Even works in existing go repositories (see build from this repository as an example)

Visual Studio Code Extension

gotaskr has a corresponding Visual Studio Code extension that allows easily running or even debugging tasks directly from Visual Studio Code. It also allows to add arguments when running or debugging them.

Example:

image

See https://marketplace.visualstudio.com/items?itemName=Roemer.gotaskr-vscode for details.

Quick-Start

Create a new go project:

go mod init my-project

Add gotaskr:

go get github.com/roemer/gotaskr

Create a build.go file:

package main

import (
	"os"

	"github.com/roemer/gotaskr"
)

func main() {
	os.Exit(gotaskr.Execute())
}

Now you need to register your tasks you want to be able to run:

func init() {
	gotaskr.Task("My-Task", func() error {
		fmt.Println("Hello from My-Task")
		return nil
	})
}

Now invoke this task by running:

go run . --target My-Task

Enjoy the fancy output and statistics:

------------------------------------------------------------
Running gotaskr
------------------------------------------------------------

=== My-Task ================================================
Hello from My-Task
=== /My-Task ===============================================
Duration: 00:00:00.000530

------------------------------------------------------------
Finished gotaskr at 2023-03-03 10:37:34.496
------------------------------------------------------------

Task                                              Exit Code    Duration
--------------------------------------------------------------------------------
My-Task                                           0            00:00:00.000530
--------------------------------------------------------------------------------
Total                                                          00:00:00.000530

Examples

Have a look at the examples from this repository.

Documentation

Have a look at the wiki for additional information.

Documentation

Overview

Package gotaskr provides the basic methods to register and run tasks. It also provides the main entrypoint for gotaskr.

Index

Constants

This section is empty.

Variables

Tools provides typed access to the various tools supported.

Functions

func AddFollowupTask added in v0.0.8

func AddFollowupTask(taskName ...string)

AddFollowupTask allows to add one or more tasks that should run after the current finished.

func Execute

func Execute() int

Execute is the entry point of gotaskr.

func FinishTimeMeasurement added in v0.3.8

func FinishTimeMeasurement(timeMeasurement *TimeMeasurement)

func GetArgument

func GetArgument(argName string) (string, bool)

GetArgument returns the value of the argument with the given name and also a flag, if the argument was present or not.

func GetArgumentOrDefault added in v0.0.3

func GetArgumentOrDefault(argName string, defaultValue string) (string, bool)

GetArgumentOrDefault returns the value of the argument with the given name or the given default value if the value was not present and also a flag, if the argument was present or not.

func HasArgument

func HasArgument(argName string) bool

HasArgument returns true if an arument was set and false otherwise, regardless of the value.

func MeasureTime added in v0.1.5

func MeasureTime(measurementName string, f func() error) error

func RunTarget

func RunTarget(target string) error

RunTarget runs the given task and all the needed dependencies.

func Setup added in v0.0.25

func Setup(setupFunc func() error)

func TaskSetup added in v0.0.25

func TaskSetup(taskFunc func() error)

func TaskTeardown added in v0.0.25

func TaskTeardown(taskFunc func() error)

func Teardown added in v0.0.25

func Teardown(taskFunc func() error)

Types

type TaskObject

type TaskObject struct {
	// contains filtered or unexported fields
}

TaskObject represents a registered task.

func Task

func Task(name string, taskFunc func() error) *TaskObject

Task registers the given function with the name so it can be executed.

func (*TaskObject) Argument added in v0.0.12

func (taskObject *TaskObject) Argument(argumentName string, argumentDescription string, optional bool) *TaskObject

Argument adds a description for an argument. Will be shown when the help is displayed.

func (*TaskObject) ContinueOnError added in v0.0.7

func (taskObject *TaskObject) ContinueOnError() *TaskObject

ContinueOnError will continue with dependencies or dependees even when the task returned an error.

func (*TaskObject) DeferOnError added in v0.0.7

func (taskObject *TaskObject) DeferOnError() *TaskObject

DeferOnError will continue with dependencies or dependees even when the task returned an error.

func (*TaskObject) DependeeOf added in v0.0.7

func (taskObject *TaskObject) DependeeOf(taskName ...string) *TaskObject

DependeeOf adds dependees in the given order. Duplicate dependees are removed.

func (*TaskObject) DependeeOfTask added in v0.1.3

func (taskObject *TaskObject) DependeeOfTask(task ...*TaskObject) *TaskObject

DependeeOfTask adds dependees in the given order. Duplicate dependees are removed.

func (*TaskObject) DependsOn

func (taskObject *TaskObject) DependsOn(taskName ...string) *TaskObject

DependsOn adds dependencies in the given order. Duplicate dependencies are removed.

func (*TaskObject) DependsOnTask added in v0.1.3

func (taskObject *TaskObject) DependsOnTask(task ...*TaskObject) *TaskObject

DependsOnTask adds dependencies in the given order. Duplicate dependencies are removed.

func (*TaskObject) Description

func (taskObject *TaskObject) Description(description string) *TaskObject

Description sets the description of a task. Will be shown when the help is displayed.

func (*TaskObject) GetName added in v0.1.2

func (taskObject *TaskObject) GetName() string

GetName gets the name of the task.

func (*TaskObject) Then added in v0.0.8

func (taskObject *TaskObject) Then(taskName ...string) *TaskObject

Then adds followup tasks in the given order.

func (*TaskObject) ThenTask added in v0.1.3

func (taskObject *TaskObject) ThenTask(task ...*TaskObject) *TaskObject

Then adds followup tasks in the given order.

type TimeMeasurement added in v0.3.8

type TimeMeasurement struct {
	// contains filtered or unexported fields
}

func StartTimeMeasurement added in v0.3.8

func StartTimeMeasurement(measurementName string) *TimeMeasurement

func (*TimeMeasurement) Duration added in v0.3.10

func (t *TimeMeasurement) Duration() time.Duration

func (*TimeMeasurement) Finish added in v0.3.8

func (t *TimeMeasurement) Finish()

func (*TimeMeasurement) StartTime added in v0.3.10

func (t *TimeMeasurement) StartTime() time.Time

Directories

Path Synopsis
Package argparse is a simple parser for command line arguments.
Package argparse is a simple parser for command line arguments.
examples
Package execr is a wapper to run exec commands.
Package execr is a wapper to run exec commands.
Package goext adds various extensions for the go language.
Package goext adds various extensions for the go language.
Package gttools provides helper methods for various tools.
Package gttools provides helper methods for various tools.

Jump to

Keyboard shortcuts

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