taskrunner

package module
v0.0.26 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2025 License: 0BSD Imports: 15 Imported by: 1

README

Task-Runner

Handling CI-related tasks such as building, testing, and deploying can be automated using bash scripts, but these can quickly get messy. Managing multiple processes, handling cleanup, aborting on failure, adding pretty colored output, and more can lead to complex bash code. That's why Task-Runner was created: a simple, open source tool to replace bash scripts with cleaner, more maintainable code written in Go.

Installation

go get github.com/ocelot-cloud/task-runner

Usage Example

var backendDir = "../backend"
var frontendDir = "../frontend"
var acceptanceTestsDir = "../acceptance"

func TestFrontend() {
    tr.PrintTaskDescription("Testing Integrated Components")
    defer tr.Cleanup() // shuts down the daemon processes at the end
    tr.ExecuteInDir(backendDir, "go build")
    tr.StartDaemon(backendDir, "./backend")
    tr.WaitUntilPortIsReady("8080")

    tr.ExecuteInDir(frontendDir, "npm install")
    tr.StartDaemon(frontendDir, "npm run serve", "VITE_APP_PROFILE=TEST")
    tr.WaitForWebPageToBeReady("http://localhost:8081/")
    tr.ExecuteInDir(acceptanceTestsDir, cypressCommand, "CYPRESS_PROFILE=TEST")
}

The idea is to write simple functions like this and build a CLI tool, e.g., by using cobra, to call these functions. The final use of the CLI tool might look like this:

go build
./my-task-runner test frontend

This approach helps you create a modern and scalable CI infrastructure. Here is an example of how to initialize the tool in the main function:

func main() {
    // Optional. If you enable this, when a process hangs, you can press "CTRL" + "C" which will 
    // call the cleanup function and try to gracefully shut down the process. If that does not work, 
    // it will forcefully exit the program.
    tr.HandleSignals()

    // Optional. Environment variables are applied to each command called.
    tr.DefaultEnvs = []string{"LOG_LEVEL=DEBUG"}

    // Optional. Is called as sub-function whenever tr.Cleanup() is called. 
    // Can be used to add custom post-task cleanup functionality.
    tr.CustomCleanupFunc = MyCustomCleanupFunction

    // Sample usage of cobra library
    if err := rootCmd.Execute(); err != nil {
        tr.ColoredPrintln("\nError during execution: %v\n", err)
        tr.CleanupAndExitWithError()
    }
}

var rootCmd = &cobra.Command{
    Use:   "app",
    Short: "some brief description",
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Println("hello world")
    },
}

Compatibility

The tool has been tested under Linux and Windows. In order to be independent of the Linux tools cp, rm, mkdir and move, which are quite useful for handling files and folders on the system, operating system independent functions have been implemented:

tr.Copy(...)
tr.Remove(...)
tr.MakeDir(...)
tr.Move(...)

Contributing

Please read the Community articles for more information on how to contribute to the project and interact with others.

License

This project is licensed under the 0BSD License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultEnvs []string

Functions

This section is empty.

Types

type Config added in v0.0.24

type Config struct {
	CleanupOnFailure            bool
	ShowCleanupOutput           bool
	CleanupFunc                 func()
	DefaultEnvironmentVariables []string
	// contains filtered or unexported fields
}

type OperatingSystem added in v0.0.24

type OperatingSystem interface {
	BuildCommand(dir, commandStr string) *exec.Cmd
	KillProcessGroup(pid int) error
	SetProcessGroup(cmd *exec.Cmd)
	GetOsOutputs() (stdout, stderr io.Writer)
	GetOsEnvs() []string
}

TODO replace OS references with this interface

type TaskRunner added in v0.0.24

type TaskRunner struct {
	Config *Config
	Log    logger
}

func GetTaskRunner added in v0.0.24

func GetTaskRunner() *TaskRunner

func (*TaskRunner) Cleanup added in v0.0.24

func (t *TaskRunner) Cleanup()

func (*TaskRunner) Copy added in v0.0.24

func (t *TaskRunner) Copy(srcDir, fileName, destFolder string)

func (*TaskRunner) EnableAbortForKeystrokeControlPlusC added in v0.0.24

func (t *TaskRunner) EnableAbortForKeystrokeControlPlusC()

func (*TaskRunner) Execute added in v0.0.24

func (t *TaskRunner) Execute(commandStr string, envs ...string)

func (*TaskRunner) ExecuteInDir added in v0.0.24

func (t *TaskRunner) ExecuteInDir(dir string, commandStr string, envs ...string)

func (*TaskRunner) ExitWithError added in v0.0.24

func (t *TaskRunner) ExitWithError()

func (*TaskRunner) KillProcesses added in v0.0.26

func (t *TaskRunner) KillProcesses(processSubStrings []string)

func (*TaskRunner) MakeDir added in v0.0.24

func (t *TaskRunner) MakeDir(dir string)

func (*TaskRunner) Move added in v0.0.24

func (t *TaskRunner) Move(src, dest string)

func (*TaskRunner) PromptForContinuation added in v0.0.24

func (t *TaskRunner) PromptForContinuation(prompt string)

func (*TaskRunner) Remove added in v0.0.24

func (t *TaskRunner) Remove(paths ...string)

func (*TaskRunner) Rename added in v0.0.24

func (t *TaskRunner) Rename(dir, currentFileName, newFileName string)

func (*TaskRunner) ResetCursor added in v0.0.24

func (t *TaskRunner) ResetCursor()

func (*TaskRunner) StartDaemon added in v0.0.24

func (t *TaskRunner) StartDaemon(dir, commandStr string, envs ...string)

func (*TaskRunner) WaitForWebPageToBeReady added in v0.0.24

func (t *TaskRunner) WaitForWebPageToBeReady(targetUrl string)

func (*TaskRunner) WaitUntilPortIsReady added in v0.0.24

func (t *TaskRunner) WaitUntilPortIsReady(port string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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