pipeline

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2021 License: Apache-2.0 Imports: 1 Imported by: 34

README

go-command-pipeline

Go version Version GitHub downloads Go Report Card Codecov

Small Go utility that executes high-level actions in a pipeline fashion. Especially useful when combined with the Facade design pattern.

Usage

import (
    pipeline "github.com/ccremer/go-command-pipeline"
    "github.com/ccremer/go-command-pipeline/predicate"
)

func main() {
	p := pipeline.NewPipeline()
	p.WithSteps(
		predicate.ToStep("clone repository", CloneGitRepository(), predicate.Not(DirExists("my-repo"))),
		pipeline.NewStep("checkout branch", CheckoutBranch()),
		pipeline.NewStep("pull", Pull()),
	)
	result := p.Run()
	if !result.IsSuccessful() {
		log.Fatal(result.Err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionFunc

type ActionFunc func() Result

ActionFunc is the func that contains your business logic.

type Logger

type Logger interface {
	// Log is expected to write the given message to a logging framework or similar.
	// The name contains the step's name.
	Log(message, name string)
}

Logger is a simple interface that enables logging certain actions with your favourite logging framework.

type Pipeline

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

Pipeline holds and runs intermediate actions, called "steps".

func NewPipeline

func NewPipeline() *Pipeline

NewPipeline returns a new Pipeline instance that doesn't log anything.

func NewPipelineWithLogger

func NewPipelineWithLogger(logger Logger) *Pipeline

NewPipelineWithLogger returns a new Pipeline instance with the given logger that shouldn't be nil.

func (*Pipeline) AddStep

func (p *Pipeline) AddStep(step Step) *Pipeline

AddStep appends the given step to the Pipeline at the end and returns itself.

func (*Pipeline) AsNestedStep

func (p *Pipeline) AsNestedStep(name string) Step

AsNestedStep converts the Pipeline instance into a Step that can be used in other pipelines. The logger and abort handler are passed to the nested pipeline.

func (*Pipeline) Run

func (p *Pipeline) Run() Result

Run executes the pipeline and returns the result. Steps are executed sequentially as they were added to the Pipeline. If a Step returns a Result with a non-nil error, the Pipeline is aborted its Result contains the affected step's error.

func (*Pipeline) WithNestedSteps added in v0.4.0

func (p *Pipeline) WithNestedSteps(name string, steps ...Step) Step

WithNestedSteps is similar to AsNestedStep but it accepts the steps given directly as parameters.

func (*Pipeline) WithSteps

func (p *Pipeline) WithSteps(steps ...Step) *Pipeline

WithSteps appends the given arrway of steps to the Pipeline at the end and returns itself.

type Result

type Result struct {
	// Err contains the step's returned error, nil otherwise.
	Err error
	// Name is an optional identifier for a result.
	// ActionFunc may set this property before returning to help an ResultHandler with further processing.
	Name string
}

Result is the object that is returned after each step and after running a pipeline.

func (Result) IsFailed added in v0.2.0

func (r Result) IsFailed() bool

IsFailed returns true if the contained error is non-nil.

func (Result) IsSuccessful

func (r Result) IsSuccessful() bool

IsSuccessful returns true if the contained error is nil.

type ResultHandler added in v0.3.0

type ResultHandler func(result Result) error

ResultHandler is a func that gets called when a step's ActionFunc has finished with any Result.

type Step

type Step struct {
	// Name describes the step's human-readable name.
	// It has no other uses other than easily identifying a step for debugging or logging.
	Name string
	// F is the ActionFunc assigned to a pipeline Step.
	// This is required.
	F ActionFunc
	// H is the ResultHandler assigned to a pipeline Step.
	// This is optional and it will be called in any case if it is set after F completed.
	// Use cases could be logging, updating a GUI or handle errors while continuing the pipeline.
	// The function may return nil even if the Result contains an error, in which case the pipeline will continue.
	// This function is called before the next step's F is invoked.
	H ResultHandler
}

Step is an intermediary action and part of a Pipeline.

func NewStep

func NewStep(name string, action ActionFunc) Step

NewStep returns a new Step with given name and action.

func (Step) WithResultHandler added in v0.3.0

func (s Step) WithResultHandler(handler ResultHandler) Step

WithResultHandler sets the ResultHandler of this specific step and returns the step itself.

Directories

Path Synopsis
Package parallel extends the command-pipeline core with concurrency steps.
Package parallel extends the command-pipeline core with concurrency steps.
Package predicate provides functions that wrap existing actions but executes them only on conditions ("predicates").
Package predicate provides functions that wrap existing actions but executes them only on conditions ("predicates").

Jump to

Keyboard shortcuts

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