flow

package
v0.0.0-...-566b0a1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

The flow

Example
package main

import (
	"context"
	"fmt"

	"github.com/ks-tool/k8s-bootstrapper/pkg/flow"
)

func main() {
	act1 := flow.NewAction("my-name1", func(ctx context.Context) (flow.StatusType, error) {
		log := ctx.Value(flow.LogKey).(*flow.Logger)
		log.Println("Hello world")
		return flow.StatusFailed, nil
	})

	act2 := flow.NewAction("my-name2", func(ctx context.Context) (flow.StatusType, error) {
		st, err := ctx.(*flow.Context).
			GetTaskByName("main").
			GetActionStatus("my-name1")
		if st == flow.StatusFailed {
			return flow.StatusSuccess, nil
		}

		return flow.StatusFailed, err
	})

	tasks := flow.NewTask("main")
	tasks.AddAction(act1)
	tasks.AddAction(act2)

	f := flow.New()
	f.AddTask(tasks)

	if err := f.Run(context.Background()); err != nil {
		panic(err)
	}

	st, err := tasks.GetActionStatus("my-name2")
	if err != nil {
		panic(err)
	}
	fmt.Println("status:", st)
}

Documentation

Index

Constants

View Source
const (
	LogKey = "log"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action struct {
	Name string
	Fn   ActionFunc
	// contains filtered or unexported fields
}

func NewAction

func NewAction(name string, action ActionFunc) Action

func (*Action) Run

func (a *Action) Run(ctx context.Context) error

func (*Action) Status

func (a *Action) Status() (StatusType, error)

type ActionFunc

type ActionFunc func(context.Context) (StatusType, error)

type Context

type Context struct {
	context.Context
	// contains filtered or unexported fields
}

func (*Context) GetTaskByName

func (c *Context) GetTaskByName(name string) TaskErrors

func (*Context) Logger

func (c *Context) Logger() *Logger

func (*Context) Value

func (c *Context) Value(key any) any

type Flow

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

func New

func New() *Flow

func (*Flow) AddTask

func (f *Flow) AddTask(t Task) *Flow

func (*Flow) Run

func (f *Flow) Run(ctx context.Context) error

func (*Flow) SetLogLevel

func (f *Flow) SetLogLevel(lvl log.Level)

type Logger

type Logger struct{ *log.Logger }

func (*Logger) Plain

func (l *Logger) Plain(msg string)

type StatusType

type StatusType int
const (
	StatusSkipped StatusType = iota - 1
	StatusPending
	StatusRunning
	StatusSuccess
	StatusFailed
	StatusHasFailed
	StatusUnknown
)

func (StatusType) String

func (t StatusType) String() string

type Task

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

func NewTask

func NewTask(name string) Task

func (*Task) AddAction

func (t *Task) AddAction(act Action)

func (*Task) GetActionStatus

func (t *Task) GetActionStatus(name string) (StatusType, error)

func (*Task) NoExitOnError

func (t *Task) NoExitOnError()

func (*Task) Successful

func (t *Task) Successful() bool

type TaskErrors

type TaskErrors interface {
	GetActionStatus(name string) (StatusType, error)
	Successful() bool
}

Jump to

Keyboard shortcuts

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