commands

package
v0.0.0-...-1fe5492 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PerformOnHosts

func PerformOnHosts(hosts []ssh.ClientInfo, command func(host ssh.ClientInfo, sshClient *ssh.Client) (string, error)) map[string]CommandResult

PerformOnHosts performs the command on all hosts in parallel

Types

type Command

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

Command represents a background command

func (*Command) Cancel

func (c *Command) Cancel() error

Cancel cancels the running command

func (*Command) CreatedAt

func (c *Command) CreatedAt() time.Time

CreatedAt returns the command's creation time

func (*Command) ID

func (c *Command) ID() string

ID returns the command's unique identifier

func (*Command) SetStatusForTest

func (c *Command) SetStatusForTest(status CommandStatus)

SetStatusForTest is a helper method for testing to set the command status This should only be used in tests

func (*Command) SimulateCancellationForTest

func (c *Command) SimulateCancellationForTest()

SimulateCancellationForTest simulates the cancellation process for testing This sets the command to cancelled status after Cancel() is called

func (*Command) Start

func (c *Command) Start() error

Start starts executing the command in the background

func (*Command) Status

func (c *Command) Status() CommandStatus

Status returns the command's current status

func (*Command) ToListItem

func (c *Command) ToListItem() *CommandListItem

ToListItem returns a summary of the command without results (for listing)

func (*Command) ToState

func (c *Command) ToState() *CommandState

ToState returns a safe copy of the command state for serialization

type CommandListItem

type CommandListItem struct {
	ID        string                 `json:"id"`
	Status    CommandStatus          `json:"status"`
	Command   string                 `json:"command"`
	Hosts     []utils.HostIdentifier `json:"hosts"`
	CreatedAt time.Time              `json:"created_at"`
	StartedAt *time.Time             `json:"started_at,omitempty"`
	EndedAt   *time.Time             `json:"ended_at,omitempty"`
}

CommandListItem represents a summary of a command for listing (without results)

type CommandResult

type CommandResult struct {
	Host   string `json:"host"`
	Result string `json:"result"`
	Err    error  `json:"error"`
}

CommandResult is a single result on that host

func (CommandResult) MarshalJSON

func (cr CommandResult) MarshalJSON() ([]byte, error)

MarshalJSON implements custom JSON marshaling to properly handle the error field

type CommandState

type CommandState struct {
	ID        string                   `json:"id"`
	Status    CommandStatus            `json:"status"`
	Command   string                   `json:"command"`
	Hosts     []utils.HostIdentifier   `json:"hosts"`
	Results   map[string]CommandResult `json:"results"`
	CreatedAt time.Time                `json:"created_at"`
	StartedAt *time.Time               `json:"started_at,omitempty"`
	EndedAt   *time.Time               `json:"ended_at,omitempty"`
	Error     string                   `json:"error,omitempty"`
}

CommandState represents the serializable state of a Command

type CommandStatus

type CommandStatus string

CommandStatus represents the current state of a command

const (
	CommandStatusPending   CommandStatus = "pending"
	CommandStatusRunning   CommandStatus = "running"
	CommandStatusCompleted CommandStatus = "completed"
	CommandStatusFailed    CommandStatus = "failed"
	CommandStatusCancelled CommandStatus = "cancelled"
)

type MockRunner

type MockRunner struct {
	Commands          map[string]*Command
	CreateCommandFunc func(commandStr string, hosts []ssh.ClientInfo) *Command
	GetCommandFunc    func(commandID string) (*Command, error)
	GetMostRecentFunc func() (*Command, error)
	ListCommandsFunc  func() []*Command
	CancelAllFunc     func()
}

MockRunner is a mock implementation of Runner for testing purposes

func NewMockRunner

func NewMockRunner() *MockRunner

NewMockRunner creates a new mock runner

func (*MockRunner) CancelAllCommands

func (m *MockRunner) CancelAllCommands()

CancelAllCommands cancels all running commands (mock implementation)

func (*MockRunner) CreateCommand

func (m *MockRunner) CreateCommand(commandStr string, hosts []ssh.ClientInfo) *Command

CreateCommand creates a new command (mock implementation)

func (*MockRunner) GetCommand

func (m *MockRunner) GetCommand(commandID string) (*Command, error)

GetCommand retrieves a command by ID (mock implementation)

func (*MockRunner) GetMostRecentCommand

func (m *MockRunner) GetMostRecentCommand() (*Command, error)

GetMostRecentCommand returns the most recently created command (mock implementation)

func (*MockRunner) ListCommands

func (m *MockRunner) ListCommands() []*Command

ListCommands returns all commands (mock implementation)

type Runner

type Runner interface {
	CreateCommand(commandStr string, hosts []ssh.ClientInfo) *Command
	GetCommand(commandID string) (*Command, error)
	GetMostRecentCommand() (*Command, error)
	ListCommands() []*Command
	CancelAllCommands()
}

Runner is an interface for managing background commands

func NewRunner

func NewRunner() Runner

NewRunner creates a new command runner

Jump to

Keyboard shortcuts

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