runner

package
v1.7.5 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2024 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

package runner

Runner runs the command inside the executor shell

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrArtifactFailed = errors.New("artifact not processed")
View Source
var ErrMutuallyExclusiveVarSet = errors.New("mutually exclusive vars have been set")

Functions

This section is empty.

Types

type ExecutionContext

type ExecutionContext struct {
	Executable *utils.Binary
	Dir        string
	Env        variables.Container
	Envfile    *utils.Envfile
	Variables  variables.Container
	// Quote character to use around a command
	// when passed to another executable, e.g. docker
	Quote string
	// contains filtered or unexported fields
}

ExecutionContext allow you to set up execution environment, variables, binary which will run your task, up/down commands etc.

func DefaultContext

func DefaultContext() *ExecutionContext

DefaultContext creates default ExecutionContext instance

func NewExecutionContext

func NewExecutionContext(executable *utils.Binary, dir string, env variables.Container, envfile *utils.Envfile, up, down, before, after []string, options ...ExecutionContextOption) *ExecutionContext

NewExecutionContext creates new ExecutionContext instance

func (*ExecutionContext) After

func (c *ExecutionContext) After() error

After executes tasks defined to run after every usage of the context

func (*ExecutionContext) Before

func (c *ExecutionContext) Before() error

Before executes tasks defined to run before every usage of the context

func (*ExecutionContext) Down

func (c *ExecutionContext) Down()

Down executes tasks defined to run once after last usage of the context

func (*ExecutionContext) GenerateEnvfile

func (c *ExecutionContext) GenerateEnvfile(env variables.Container) error

GenerateEnvfile processes env and other supplied variables writes them to a `.taskctl` folder in a current directory the file names are generated using the `generated_{Task_Name}_{UNIX_timestamp}.env`.

Note: it will create the directory

Example
package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/Ensono/taskctl/internal/utils"
	"github.com/Ensono/taskctl/pkg/runner"
	"github.com/Ensono/taskctl/pkg/variables"
)

func helpSetupCleanUp() (path string, defereCleanUp func()) {
	tmpDir, _ := os.MkdirTemp(os.TempDir(), "context-envfile")
	path = filepath.Join(tmpDir, "generated_task_123.env")
	return path, func() {
		os.RemoveAll(tmpDir)
	}
}

func main() {
	outputFilePath, cleanUp := helpSetupCleanUp()

	defer cleanUp()

	osEnvVars := variables.FromMap(map[string]string{"TF_VAR_CAPPED_BY_MSFT": "some value"})
	//  "var2": "original222", "!::": "whatever val will never be added", "incld1": "welcome var", "exclude3": "sadgfddf"})
	userEnvVars := variables.FromMap(map[string]string{})
	envVars := osEnvVars.Merge(userEnvVars)
	execContext := runner.NewExecutionContext(nil, "", envVars, utils.NewEnvFile(func(e *utils.Envfile) {
		e.Generate = true
		e.Path = outputFilePath
		e.Exclude = append(e.Exclude, []string{"excld1", "exclude3", "userSuppliedButExcluded"}...)
		e.Modify = append(e.Modify, []utils.ModifyEnv{
			{Pattern: "^(?P<keyword>TF_VAR_)(?P<varname>.*)", Operation: "lower"},
		}...)

	}), []string{}, []string{}, []string{}, []string{})

	_ = execContext.GenerateEnvfile(envVars)

	contents, _ := os.ReadFile(outputFilePath)
	// for the purposes of the test example we need to make sure the map is
	// always displayed in same order of keys, which is not a guarantee with a map
	fmt.Println(string(contents))
}
Output:

TF_VAR_capped_by_msft=some value

func (*ExecutionContext) StartupError

func (c *ExecutionContext) StartupError() error

StartUpError reports whether an error exists on startUp

func (*ExecutionContext) Up

func (c *ExecutionContext) Up() error

Up executes tasks defined to run once before first usage of the context

type ExecutionContextOption

type ExecutionContextOption func(c *ExecutionContext)

ExecutionContextOption is a functional option to configure ExecutionContext

func WithQuote

func WithQuote(quote string) ExecutionContextOption

WithQuote is functional option to set Quote for ExecutionContext

type Opts

type Opts func(*TaskRunner)

Opts is a task runner configuration function.

func WithContexts

func WithContexts(contexts map[string]*ExecutionContext) Opts

WithContexts adds provided contexts to task runner

func WithVariables

func WithVariables(variables variables.Container) Opts

WithVariables adds provided variables to task runner

type Runner

type Runner interface {
	Run(t *task.Task) error
	Cancel()
	Finish()
}

Runner describes tasks runner interface

type TaskCompiler

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

TaskCompiler compiles tasks into jobs for executor

func NewTaskCompiler

func NewTaskCompiler() *TaskCompiler

NewTaskCompiler create new TaskCompiler instance

func (*TaskCompiler) CompileCommand

func (tc *TaskCompiler) CompileCommand(
	taskName string,
	command string,
	executionCtx *ExecutionContext,
	dir string,
	timeout *time.Duration,
	stdin io.Reader,
	stdout, stderr io.Writer,
	env, vars variables.Container,
) (*executor.Job, error)

CompileCommand compiles command into Job

func (*TaskCompiler) CompileTask

func (tc *TaskCompiler) CompileTask(t *task.Task, executionContext *ExecutionContext, stdin io.Reader, stdout, stderr io.Writer, env, vars variables.Container) (*executor.Job, error)

CompileTask compiles task into Job (linked list of commands) executed by Executor

type TaskRunner

type TaskRunner struct {
	Executor executor.Executor
	DryRun   bool

	Stdin          io.Reader
	Stdout, Stderr io.Writer
	OutputFormat   string
	// contains filtered or unexported fields
}

TaskRunner struct holds the properties and methods for running the tasks inside the given executor

func NewTaskRunner

func NewTaskRunner(opts ...Opts) (*TaskRunner, error)

NewTaskRunner creates new TaskRunner instance

func (*TaskRunner) Cancel

func (r *TaskRunner) Cancel()

Cancel cancels execution

func (*TaskRunner) Finish

func (r *TaskRunner) Finish()

Finish makes cleanup tasks over contexts

func (*TaskRunner) Run

func (r *TaskRunner) Run(t *task.Task) error

Run run provided task. TaskRunner first compiles task into linked list of Jobs, then passes those jobs to Executor

Env on the runner is global to all tasks it is built using the dotenv output only for now

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/Ensono/taskctl/pkg/output"
	"github.com/Ensono/taskctl/pkg/runner"

	taskpkg "github.com/Ensono/taskctl/pkg/task"
)

func main() {
	t := taskpkg.FromCommands("t1", "go doc github.com/Ensono/taskctl/pkg/runner.Runner")
	ob := output.NewSafeWriter(&bytes.Buffer{})
	r, err := runner.NewTaskRunner(func(tr *runner.TaskRunner) {
		tr.Stdout = ob
	})
	if err != nil {
		return
	}
	err = r.Run(t)
	if err != nil {
		fmt.Println(err, t.ExitCode, t.ErrorMessage())
	}
	fmt.Println(ob.String())
	// indentation is important with the matched output here
	
Output:

func (*TaskRunner) SetContexts

func (r *TaskRunner) SetContexts(contexts map[string]*ExecutionContext) *TaskRunner

SetContexts sets task runner's contexts

func (*TaskRunner) SetVariables

func (r *TaskRunner) SetVariables(vars variables.Container) *TaskRunner

SetVariables sets task runner's variables

func (*TaskRunner) WithVariable

func (r *TaskRunner) WithVariable(key, value string) *TaskRunner

WithVariable adds variable to task runner's variables list. It creates new instance of variables container.

Jump to

Keyboard shortcuts

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