deploy

package
v0.5.0-pre19 Latest Latest
Warning

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

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

Documentation

Overview

Package deploy provides command execution utilities for local and remote deployments.

Package deploy provides command execution utilities for local and remote deployments.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchHost

func MatchHost(target, pattern string) bool

MatchHost checks if the target host matches the SSH config host pattern.

func ResolveSSHTarget

func ResolveSSHTarget(target, user, keyPath string) (string, string, string, string, error)

ResolveSSHTarget resolves SSH connection details using ~/.ssh/config. Returns: hostname, user, keyPath, identityAgent, error.

Types

type CommandBuilder

type CommandBuilder interface {
	// BuildCommand creates a CommandExecutor for running local commands.
	BuildCommand(name string, args ...string) CommandExecutor

	// BuildShellCommand creates a CommandExecutor for running shell commands via sh -c.
	BuildShellCommand(command string) CommandExecutor
}

CommandBuilder defines an interface for building shell commands. This abstraction enables unit testing of SSH/SCP command construction.

type CommandExecutor

type CommandExecutor interface {
	// Run executes the command and returns the combined output (stdout+stderr).
	Run() ([]byte, error)

	// SetStdin sets the stdin for the command.
	SetStdin(stdin []byte)
}

CommandExecutor defines an interface for executing shell commands. This abstraction enables unit testing without real shell execution.

type Executor

type Executor struct {
	Target         string
	SSHUser        string
	SSHKey         string
	IdentityAgent  string
	DryRun         bool
	Logger         Logger
	CommandBuilder CommandBuilder // Injectable command builder for testability
}

Executor handles command execution on local or remote targets.

func NewExecutor

func NewExecutor(target, sshUser, sshKey, identityAgent string, dryRun bool) *Executor

NewExecutor creates a new command executor.

func (*Executor) CopyFile

func (e *Executor) CopyFile(src, dst string) error

CopyFile copies a file to the target.

func (*Executor) IsLocal

func (e *Executor) IsLocal() bool

IsLocal returns true if target is localhost.

func (*Executor) Run

func (e *Executor) Run(command string) (string, error)

Run executes a command.

func (*Executor) RunSudo

func (e *Executor) RunSudo(command string) (string, error)

RunSudo executes a command with sudo.

func (*Executor) SetCommandBuilder

func (e *Executor) SetCommandBuilder(builder CommandBuilder)

SetCommandBuilder sets a custom command builder for dependency injection. This enables unit testing without real shell execution.

func (*Executor) SetLogger

func (e *Executor) SetLogger(logger Logger)

SetLogger sets the debug logger for the executor.

func (*Executor) WriteFile

func (e *Executor) WriteFile(path, content string) error

WriteFile writes content to a file on the target.

type Logger

type Logger interface {
	Debugf(format string, args ...interface{})
}

Logger defines the interface for debug logging.

type MockBuiltCommand

type MockBuiltCommand struct {
	Name    string
	Args    []string
	IsShell bool
}

MockBuiltCommand records details of a built command.

type MockCommandBuilder

type MockCommandBuilder struct {
	// Commands records all commands that were built.
	Commands []MockBuiltCommand
	// NextExecutor is the next executor to return. If nil, creates a default MockCommandExecutor.
	NextExecutor *MockCommandExecutor
	// ExecutorFactory allows creating executors dynamically based on command.
	ExecutorFactory func(name string, args []string) *MockCommandExecutor
}

MockCommandBuilder implements CommandBuilder for testing.

func NewMockCommandBuilder

func NewMockCommandBuilder() *MockCommandBuilder

NewMockCommandBuilder creates a new MockCommandBuilder.

func (*MockCommandBuilder) BuildCommand

func (b *MockCommandBuilder) BuildCommand(name string, args ...string) CommandExecutor

BuildCommand creates a MockCommandExecutor and records the command details.

func (*MockCommandBuilder) BuildShellCommand

func (b *MockCommandBuilder) BuildShellCommand(command string) CommandExecutor

BuildShellCommand creates a MockCommandExecutor for shell commands.

func (*MockCommandBuilder) LastCommand

func (b *MockCommandBuilder) LastCommand() *MockBuiltCommand

LastCommand returns the most recently built command, or nil if none.

func (*MockCommandBuilder) Reset

func (b *MockCommandBuilder) Reset()

Reset clears all recorded commands.

func (*MockCommandBuilder) SetNextExecutor

func (b *MockCommandBuilder) SetNextExecutor(executor *MockCommandExecutor)

SetNextExecutor sets the executor to return for the next Build* call.

type MockCommandExecutor

type MockCommandExecutor struct {
	// Output is the output to return from Run.
	Output []byte
	// Err is the error to return from Run.
	Err error
	// Stdin holds the stdin data that was set.
	Stdin []byte
	// RunCalled indicates whether Run was called.
	RunCalled bool
}

MockCommandExecutor implements CommandExecutor for testing.

func (*MockCommandExecutor) Run

func (m *MockCommandExecutor) Run() ([]byte, error)

Run returns the configured output and error.

func (*MockCommandExecutor) SetStdin

func (m *MockCommandExecutor) SetStdin(stdin []byte)

SetStdin records the stdin data.

type RealCommandBuilder

type RealCommandBuilder struct{}

RealCommandBuilder implements CommandBuilder using exec.Command.

func NewRealCommandBuilder

func NewRealCommandBuilder() *RealCommandBuilder

NewRealCommandBuilder creates a new RealCommandBuilder.

func (*RealCommandBuilder) BuildCommand

func (b *RealCommandBuilder) BuildCommand(name string, args ...string) CommandExecutor

BuildCommand creates a CommandExecutor for the given command and arguments.

func (*RealCommandBuilder) BuildShellCommand

func (b *RealCommandBuilder) BuildShellCommand(command string) CommandExecutor

BuildShellCommand creates a CommandExecutor for shell commands.

type RealCommandExecutor

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

RealCommandExecutor wraps exec.Cmd to implement CommandExecutor.

func (*RealCommandExecutor) Run

func (r *RealCommandExecutor) Run() ([]byte, error)

Run executes the command and returns combined output.

func (*RealCommandExecutor) SetStdin

func (r *RealCommandExecutor) SetStdin(stdin []byte)

SetStdin sets stdin for the command.

type SSHConfig

type SSHConfig struct {
	Host          string
	HostName      string
	User          string
	IdentityFile  string
	IdentityAgent string
	Port          string
}

SSHConfig represents parsed SSH configuration for a host.

func ParseSSHConfig

func ParseSSHConfig(host string) (*SSHConfig, error)

ParseSSHConfig reads and parses ~/.ssh/config for the given host.

func ParseSSHConfigFrom

func ParseSSHConfigFrom(host, configPath string) (*SSHConfig, error)

ParseSSHConfigFrom reads and parses an SSH config file for the given host. If configPath is empty, uses ~/.ssh/config.

Jump to

Keyboard shortcuts

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