workflow

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package workflow provides a structured engine for executing sequential, multi-step CLI operations. It separates the "what" (Steps) from the "how" (Runner), allowing for reusable logic, consistent error handling, and standardized user interaction.

Core Concepts

  • Step: An interface defining a single unit of work. It has a Description, a PreCheck (validation), and an Execute (action) phase.
  • Runner: The engine that orchestrates the execution of Steps. It handles UI presentation, confirmation prompts, and error propagation.
  • PresenterInterface: An abstraction for UI output, allowing the workflow to be tested without writing to actual stdout/stderr.

Usage Pattern

runner := workflow.NewRunner(presenter, assumeYes)
err := runner.Run(ctx, "My Workflow Title",
    &MyStep1{},
    &MyStep2{},
)

PreChecks vs Execution

The Runner executes all PreChecks first. If any PreCheck fails, the workflow aborts immediately. This ensures the environment is safe before any state changes occur. If all PreChecks pass, the Runner (optionally) prompts the user for confirmation before proceeding to the Execute phase of each step.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetValidatedBranchName

func GetValidatedBranchName(
	ctx context.Context,
	branchNameFlag string,
	cfg *config.Config,
	presenter PresenterInterface,
	gitClient *git.GitClient,
	assumeYes bool,
) (string, error)

GetValidatedBranchName is a helper function that can be used by the command before initializing the workflow. It's not a step itself.

Types

type CheckAndPromptStashStep

type CheckAndPromptStashStep struct {
	GitClient *git.GitClient
	Presenter PresenterInterface
	AssumeYes bool
	DidStash  bool // ADDED: This field will track if a stash was performed.
}

CheckAndPromptStashStep checks for a clean workspace and offers to stash changes.

func (*CheckAndPromptStashStep) Description

func (s *CheckAndPromptStashStep) Description() string

Description returns a description of the step.

func (*CheckAndPromptStashStep) Execute

func (s *CheckAndPromptStashStep) Execute(
	_ context.Context,
) error

Execute runs the step.

func (*CheckAndPromptStashStep) PreCheck

func (s *CheckAndPromptStashStep) PreCheck(ctx context.Context) error

PreCheck performs pre-execution checks.

type CheckGoEnvStep added in v0.6.0

type CheckGoEnvStep struct {
	ExecClient *exec.ExecutorClient
	Presenter  PresenterInterface
}

CheckGoEnvStep verifies Go is installed and logs the version.

func (*CheckGoEnvStep) Description added in v0.6.0

func (s *CheckGoEnvStep) Description() string

Description returns the step description.

func (*CheckGoEnvStep) Execute added in v0.6.0

func (s *CheckGoEnvStep) Execute(ctx context.Context) error

Execute runs the step logic.

func (*CheckGoEnvStep) PreCheck added in v0.6.0

func (s *CheckGoEnvStep) PreCheck(_ context.Context) error

PreCheck checks if go is in PATH.

type CheckOnMainBranchStep

type CheckOnMainBranchStep struct {
	GitClient *git.GitClient
	Presenter PresenterInterface
}

CheckOnMainBranchStep verifies that the current branch is the main branch.

func (*CheckOnMainBranchStep) Description

func (s *CheckOnMainBranchStep) Description() string

Description returns a description of the step.

func (*CheckOnMainBranchStep) Execute

func (s *CheckOnMainBranchStep) Execute(
	_ context.Context,
) error

Execute runs the step.

func (*CheckOnMainBranchStep) PreCheck

func (s *CheckOnMainBranchStep) PreCheck(ctx context.Context) error

PreCheck performs pre-execution checks.

type ConfigurePathStep added in v0.6.0

type ConfigurePathStep struct {
	Presenter PresenterInterface
	AssumeYes bool
}

ConfigurePathStep ensures $HOME/go/bin is in .bashrc.

func (*ConfigurePathStep) Description added in v0.6.0

func (s *ConfigurePathStep) Description() string

Description returns the step description.

func (*ConfigurePathStep) Execute added in v0.6.0

func (s *ConfigurePathStep) Execute(_ context.Context) error

Execute runs the step logic.

func (*ConfigurePathStep) PreCheck added in v0.6.0

func (s *ConfigurePathStep) PreCheck(_ context.Context) error

PreCheck performs pre-flight checks.

type CreateAndPushBranchStep

type CreateAndPushBranchStep struct {
	GitClient  *git.GitClient
	BranchName string
}

CreateAndPushBranchStep creates and pushes a new branch.

func (*CreateAndPushBranchStep) Description

func (s *CreateAndPushBranchStep) Description() string

Description returns a description of the step.

func (*CreateAndPushBranchStep) Execute

func (s *CreateAndPushBranchStep) Execute(ctx context.Context) error

Execute runs the step.

func (*CreateAndPushBranchStep) PreCheck

PreCheck performs pre-execution checks.

type EnsureNotMainBranchStep added in v0.4.1

type EnsureNotMainBranchStep struct {
	GitClient *git.GitClient
	Presenter PresenterInterface
}

EnsureNotMainBranchStep ensures the user is NOT on the main branch.

func (*EnsureNotMainBranchStep) Description added in v0.4.1

func (s *EnsureNotMainBranchStep) Description() string

Description returns a description of the step.

func (*EnsureNotMainBranchStep) Execute added in v0.4.1

Execute runs the step.

func (*EnsureNotMainBranchStep) PreCheck added in v0.4.1

func (s *EnsureNotMainBranchStep) PreCheck(ctx context.Context) error

PreCheck performs pre-execution checks.

type EnsureStagedStep added in v0.4.1

type EnsureStagedStep struct {
	GitClient *git.GitClient
	Presenter PresenterInterface
	AssumeYes bool
}

EnsureStagedStep ensures there are staged changes, prompting to add if necessary.

func (*EnsureStagedStep) Description added in v0.4.1

func (s *EnsureStagedStep) Description() string

Description returns a description of the step.

func (*EnsureStagedStep) Execute added in v0.4.1

func (s *EnsureStagedStep) Execute(ctx context.Context) error

Execute runs the step.

func (*EnsureStagedStep) PreCheck added in v0.4.1

func (s *EnsureStagedStep) PreCheck(_ context.Context) error

PreCheck performs pre-execution checks.

type GenerateCommitPromptStep added in v0.4.1

type GenerateCommitPromptStep struct {
	GitClient *git.GitClient
	Presenter PresenterInterface
}

GenerateCommitPromptStep reads the state and outputs the AI prompt.

func (*GenerateCommitPromptStep) Description added in v0.4.1

func (s *GenerateCommitPromptStep) Description() string

Description returns a description of the step.

func (*GenerateCommitPromptStep) Execute added in v0.4.1

Execute runs the step.

func (*GenerateCommitPromptStep) PreCheck added in v0.4.1

PreCheck performs pre-execution checks.

type InstallGoToolsStep added in v0.6.0

type InstallGoToolsStep struct {
	ExecClient *exec.ExecutorClient
	Presenter  PresenterInterface
}

InstallGoToolsStep force-installs the required tools.

func (*InstallGoToolsStep) Description added in v0.6.0

func (s *InstallGoToolsStep) Description() string

Description returns the step description.

func (*InstallGoToolsStep) Execute added in v0.6.0

func (s *InstallGoToolsStep) Execute(ctx context.Context) error

Execute runs the step logic.

func (*InstallGoToolsStep) PreCheck added in v0.6.0

func (s *InstallGoToolsStep) PreCheck(_ context.Context) error

PreCheck performs pre-flight checks.

type PresenterInterface

type PresenterInterface interface {
	Error(format string, a ...any)
	Warning(format string, a ...any)
	Info(format string, a ...any)
	Success(format string, a ...any)
	Detail(format string, a ...any)
	Step(format string, a ...any)
	Header(format string, a ...any)
	Summary(format string, a ...any)
	Newline()
	PromptForConfirmation(prompt string) (bool, error)
	PromptForInput(prompt string) (string, error)
	Advice(format string, a ...any)
}

PresenterInterface defines the UI methods the workflow engine needs. This decouples the engine from the concrete ui.Presenter.

type Runner

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

Runner manages the execution of a series of workflow steps.

func NewRunner

func NewRunner(presenter PresenterInterface, assumeYes bool) *Runner

NewRunner creates a new workflow runner.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, title string, steps ...Step) error

Run executes the entire workflow.

type Step

type Step interface {
	// Description returns a user-friendly string explaining what this step will do.
	// This is used to build the "plan" shown to the user before confirmation.
	Description() string

	// PreCheck runs any prerequisite validations before the main execution.
	// If it returns an error, the entire workflow is aborted.
	PreCheck(ctx context.Context) error

	// Execute performs the primary action of the step.
	Execute(ctx context.Context) error
}

Step represents a single, discrete, and reusable action within a larger workflow.

type UpdateMainBranchStep

type UpdateMainBranchStep struct {
	GitClient *git.GitClient
}

UpdateMainBranchStep updates the main branch from the remote.

func (*UpdateMainBranchStep) Description

func (s *UpdateMainBranchStep) Description() string

Description returns a description of the step.

func (*UpdateMainBranchStep) Execute

func (s *UpdateMainBranchStep) Execute(ctx context.Context) error

Execute runs the step.

func (*UpdateMainBranchStep) PreCheck

func (s *UpdateMainBranchStep) PreCheck(_ context.Context) error

PreCheck performs pre-execution checks.

Jump to

Keyboard shortcuts

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