hooks

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package hooks provides a pluggable system for checking and installing dependencies.

The hook system allows commands to declare external dependencies (like CLI tools, libraries, or services) and automatically check for their presence. If dependencies are missing, hooks can optionally install them with user consent.

This is useful for ensuring the runtime environment meets all requirements before executing operations that depend on external tools.

Example usage:

runner := hooks.NewRunner()
runner.Register(dockerHook)
if err := runner.Run(ctx, hooks.ModeAuto); err != nil {
    return fmt.Errorf("dependency check failed: %w", err)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DockerHook

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

DockerHook implements hooks.Hook for Docker availability checking and installation.

This hook ensures Docker is installed and running before attempting to create Docker-based model instances. If Docker is not available, it attempts automatic installation (Ubuntu only).

func (*DockerHook) Check

func (h *DockerHook) Check(ctx context.Context) error

Check verifies if Docker is installed and running.

This method checks:

  • Docker CLI is available
  • Docker daemon is responsive

Parameters:

  • ctx: Context for cancellation

Returns:

  • error if Docker is not available

func (*DockerHook) Install

func (h *DockerHook) Install(ctx context.Context) error

Install attempts to install Docker.

This method:

  • Detects the operating system
  • Installs Docker if on Ubuntu
  • Returns error if OS is not supported

Parameters:

  • ctx: Context for cancellation

Returns:

  • error if installation fails

func (*DockerHook) Interactive

func (h *DockerHook) Interactive() bool

Interactive indicates whether user confirmation is recommended.

Docker installation can be intrusive (system packages), so we recommend user confirmation before proceeding.

Returns:

  • true (user confirmation recommended)

func (*DockerHook) Message

func (h *DockerHook) Message() string

Message returns a description of what this hook does.

Returns:

  • Human-readable hook description

func (*DockerHook) Name

func (h *DockerHook) Name() string

Name returns the hook identifier.

Returns:

  • Hook name

type DockerImageHook

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

DockerImageHook implements hooks.Hook for Docker image availability checking.

This hook ensures the required Docker image is available locally before attempting to create a container. If the image is not present, it pulls the image from the registry.

func (*DockerImageHook) Check

func (h *DockerImageHook) Check(ctx context.Context) error

Check verifies if the Docker image exists locally.

Parameters:

  • ctx: Context for cancellation

Returns:

  • error if image is not available locally

func (*DockerImageHook) Install

func (h *DockerImageHook) Install(ctx context.Context) error

Install pulls the Docker image from the registry.

This method uses PTY to capture Docker's native progress output, including progress bars for each layer being downloaded.

Parameters:

  • ctx: Context for cancellation

Returns:

  • error if pull fails

func (*DockerImageHook) Interactive

func (h *DockerImageHook) Interactive() bool

Interactive indicates whether user confirmation is recommended.

Image pulling is usually automatic and expected, so no confirmation needed.

Returns:

  • false (no user confirmation needed)

func (*DockerImageHook) Message

func (h *DockerImageHook) Message() string

Message returns a description of what this hook does.

Returns:

  • Human-readable hook description

func (*DockerImageHook) Name

func (h *DockerImageHook) Name() string

Name returns the hook identifier.

Returns:

  • Hook name including image name

type DockerInstaller

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

DockerInstaller handles Docker installation and image management.

Currently only supports Ubuntu Linux systems. Other Linux distributions (Debian, CentOS, RHEL, Fedora, Arch) and other operating systems (macOS, Windows) are not supported for automatic Docker installation.

Features:

  • Automatic Docker installation on Ubuntu
  • Docker image checking and pulling with progress streaming
  • Context-aware cancellation support (Ctrl+C handling)

func NewDockerInstaller

func NewDockerInstaller(eventCh chan<- string) *DockerInstaller

NewDockerInstaller creates a new Docker installer.

Parameters:

  • eventCh: Channel for sending progress events (can be nil)

Returns:

  • Configured Docker installer

func (*DockerInstaller) CheckDocker

func (d *DockerInstaller) CheckDocker() (bool, error)

CheckDocker checks if Docker is installed and running.

This method verifies that:

  • Docker CLI is available
  • Docker daemon is responsive

Returns:

  • true if Docker is available
  • error if check fails

func (*DockerInstaller) CheckDockerImage

func (d *DockerInstaller) CheckDockerImage(ctx context.Context, imageName string) (bool, error)

CheckDockerImage checks if a Docker image exists locally.

Parameters:

  • ctx: Context for cancellation
  • imageName: Docker image name

Returns:

  • true if image exists locally
  • error if check fails

func (*DockerInstaller) InstallDocker

func (d *DockerInstaller) InstallDocker() error

InstallDocker attempts to install Docker.

This method:

  • Detects the operating system
  • Installs Docker using the appropriate method
  • Streams installation progress via events

Returns:

  • error if installation fails

func (*DockerInstaller) PullDockerImage

func (d *DockerInstaller) PullDockerImage(ctx context.Context, imageName string) error

PullDockerImage pulls a Docker image.

This method uses PTY to capture Docker's progress output, including progress bars and download status for each layer.

Parameters:

  • ctx: Context for cancellation
  • imageName: Docker image to pull

Returns:

  • error if pull fails

type Hook

type Hook interface {
	// Name returns the dependency name for identification
	Name() string

	// Check verifies if the dependency is satisfied
	// Returns nil if satisfied, error if missing or misconfigured
	Check(ctx context.Context) error

	// Install attempts to install or configure the dependency
	// Should be idempotent (safe to call multiple times)
	Install(ctx context.Context) error

	// Message returns a description of what this hook does
	// Used to inform users before installation
	Message() string

	// Interactive indicates whether user confirmation is recommended
	// If true, the hook will prompt in ModeInteractive
	Interactive() bool
}

Hook defines the interface for dependency checks and installation.

Implementations should:

  • Check: Return nil if the dependency is satisfied, error otherwise
  • Install: Install the dependency and return nil on success
  • Message: Return a human-readable description of what will be installed
  • Interactive: Return true if user confirmation is recommended

func NewDockerHook

func NewDockerHook(eventCh chan<- string) Hook

NewDockerHook creates a new Docker hook.

Parameters:

  • eventCh: Channel for sending progress events

Returns:

  • Hook instance

func NewDockerImageHook

func NewDockerImageHook(imageName string, eventCh chan<- string) Hook

NewDockerImageHook creates a new Docker image hook.

Parameters:

  • imageName: Docker image to check/pull
  • eventCh: Channel for sending progress events

Returns:

  • Hook instance

type Mode

type Mode string

Mode determines how hooks behave when dependencies are missing.

const (
	// ModeAuto automatically installs missing dependencies without prompting
	ModeAuto Mode = "auto"

	// ModeInteractive prompts the user before installing dependencies
	ModeInteractive Mode = "interactive"

	// ModeCheck only checks for dependencies without attempting installation
	ModeCheck Mode = "check"
)

type Runner

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

Runner executes a collection of hooks in sequence.

It checks each registered hook and optionally installs missing dependencies based on the configured mode.

func NewRunner

func NewRunner() *Runner

NewRunner creates a new hook runner with no hooks registered.

func (*Runner) Register

func (r *Runner) Register(hook Hook)

Register adds a hook to the runner.

Hooks are executed in registration order. Dependencies should be registered before the tools that depend on them.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, mode Mode) error

Run executes all registered hooks according to the specified mode.

The behavior depends on the mode:

  • ModeAuto: Automatically installs missing dependencies
  • ModeInteractive: Prompts user before installing
  • ModeCheck: Only checks, never installs

Returns an error if any hook fails or if required dependencies are missing.

Jump to

Keyboard shortcuts

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