executor

package
v1.16.3 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package executor provides an interface for the executor as well as a local and remote implementation. The executor is used to run commands on the local machine or on a remote machine.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connector

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

Connector provides factory methods to create Remote executor. Each executor is connected to a single SSH hostAddr.

func NewConnector

func NewConnector(privateKey string, timeout time.Duration, logs Logs) (res *Connector, err error)

NewConnector creates a new Connector for a given user and private key.

func (*Connector) Connect

func (c *Connector) Connect(ctx context.Context, hostAddr, hostName, user string) (*Remote, error)

Connect connects to a remote hostAddr and returns a remote executer, caller must close.

func (*Connector) String added in v1.10.0

func (c *Connector) String() string

func (*Connector) WithAgent added in v1.8.0

func (c *Connector) WithAgent() *Connector

WithAgent enables ssh agent for authentication.

func (*Connector) WithAgentForwarding added in v1.15.0

func (c *Connector) WithAgentForwarding() *Connector

WithAgentForwarding enables ssh agent forwarding.

type DeleteOpts added in v1.5.0

type DeleteOpts struct {
	Recursive bool     // delete directories recursively
	Exclude   []string // exclude files matching the given patterns
}

DeleteOpts is a struct for delete options.

type Dry

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

Dry is an executor for dry run, just prints commands and files to be copied, synced, deleted. Useful for debugging and testing, doesn't actually execute anything.

func NewDry

func NewDry(logs Logs) *Dry

NewDry creates new executor for dry run

func (*Dry) Close

func (ex *Dry) Close() error

Close doesn't do anything

func (*Dry) Delete

func (ex *Dry) Delete(_ context.Context, remoteFile string, opts *DeleteOpts) (err error)

Delete doesn't delete anything, just prints the command

func (*Dry) Download

func (ex *Dry) Download(_ context.Context, remote, local string, opts *UpDownOpts) (err error)

Download file from remote server with scp

func (*Dry) Run

func (ex *Dry) Run(_ context.Context, cmd string, _ *RunOpts) (out []string, err error)

Run shows the command content, doesn't execute it

func (*Dry) Sync

func (ex *Dry) Sync(_ context.Context, localDir, remoteDir string, opts *SyncOpts) ([]string, error)

Sync doesn't sync anything, just prints the command

func (*Dry) Upload

func (ex *Dry) Upload(_ context.Context, local, remote string, opts *UpDownOpts) (err error)

Upload doesn't actually upload, just prints the command

type Interface

type Interface interface {
	Run(ctx context.Context, c string, opts *RunOpts) (out []string, err error)
	Upload(ctx context.Context, local, remote string, opts *UpDownOpts) (err error)
	Download(ctx context.Context, remote, local string, opts *UpDownOpts) (err error)
	Sync(ctx context.Context, localDir, remoteDir string, opts *SyncOpts) ([]string, error)
	Delete(ctx context.Context, remoteFile string, opts *DeleteOpts) (err error)
	Close() error
}

Interface is an interface for the executor. Implemented by Remote, Local and Dry structs.

type Local

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

Local is a runner for local execution. Similar to remote, but without ssh, just exec on localhost and local copy/delete/sync

func NewLocal added in v1.11.0

func NewLocal(logs Logs) *Local

NewLocal creates new executor for local run

func (*Local) Close

func (l *Local) Close() error

Close does nothing for local

func (*Local) Delete

func (l *Local) Delete(ctx context.Context, remoteFile string, opts *DeleteOpts) (err error)

Delete file or directory

func (*Local) Download

func (l *Local) Download(_ context.Context, src, dst string, opts *UpDownOpts) (err error)

Download just copy file from one place to another

func (*Local) Run

func (l *Local) Run(ctx context.Context, cmd string, _ *RunOpts) (out []string, err error)

Run executes command on local hostAddr, inside the shell

func (*Local) Sync

func (l *Local) Sync(ctx context.Context, src, dst string, opts *SyncOpts) ([]string, error)

Sync directories from src to dst

func (*Local) Upload

func (l *Local) Upload(_ context.Context, src, dst string, opts *UpDownOpts) (err error)

Upload just copy file from one place to another

type LogWriter added in v1.11.0

type LogWriter interface {
	io.Writer
	Printf(format string, v ...any)
	WithHost(hostAddr, hostName string) LogWriter
	WithWriter(wr io.Writer) LogWriter
}

LogWriter is an interface for writing logs. Some implementations support colorization and secrets masking.

type Logs added in v1.11.0

type Logs struct {
	Info LogWriter
	Out  LogWriter
	Err  LogWriter
	// contains filtered or unexported fields
}

Logs is a struct that contains two LogWriters, one for stdout and one for stderr.

func MakeLogs added in v1.11.0

func MakeLogs(verbose, bw bool, secrets []string) Logs

MakeLogs creates a new set of loggers for stdout and stderr and logger for the main info. If verbose is true, the stdout and stderr logger will be colorized. infoLog is always colorized and used to log the main info, like the command that is being executed.

func (Logs) WithHost added in v1.11.0

func (l Logs) WithHost(hostAddr, hostName string) Logs

WithHost creates a new Logs with the given hostAddr name for each LogWriter.

func (Logs) WithSecrets added in v1.11.0

func (l Logs) WithSecrets(secrets []string) Logs

WithSecrets creates a new Logs with the given secrets.

type Remote

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

Remote executes commands on remote server, via ssh. Not thread-safe.

func (*Remote) Close

func (ex *Remote) Close() error

Close connection to remote server.

func (*Remote) Delete

func (ex *Remote) Delete(ctx context.Context, remoteFile string, opts *DeleteOpts) (err error)

Delete file on remote server. Recursively if recursive is true. if a file or directory does not exist, returns nil, i.e. no error.

func (*Remote) Download

func (ex *Remote) Download(ctx context.Context, remote, local string, opts *UpDownOpts) (err error)

Download file from remote server with scp

func (*Remote) Run

func (ex *Remote) Run(ctx context.Context, cmd string, _ *RunOpts) (out []string, err error)

Run command on remote server.

func (*Remote) Sync

func (ex *Remote) Sync(ctx context.Context, localDir, remoteDir string, opts *SyncOpts) ([]string, error)

Sync compares local and remote files and uploads unmatched files, recursively.

func (*Remote) Upload

func (ex *Remote) Upload(ctx context.Context, local, remote string, opts *UpDownOpts) (err error)

Upload file to remote server with scp

type RunOpts added in v1.5.0

type RunOpts struct {
	Verbose bool // print more info to primary stdout
}

RunOpts is a struct for run options.

type SyncOpts added in v1.5.0

type SyncOpts struct {
	Delete   bool     // delete extra files on remote
	Exclude  []string // exclude files matching the given patterns
	Checksum bool     // compare checksums of local and remote files, default is size and modtime
	Force    bool     // overwrite existing files on remote
}

SyncOpts is a struct for sync options.

type UpDownOpts added in v1.5.0

type UpDownOpts struct {
	Mkdir    bool     // create remote directory if it does not exist
	Checksum bool     // compare checksums of local and remote files, default is size and modtime
	Force    bool     // overwrite existing files on remote
	Exclude  []string // exclude files matching the given patterns
}

UpDownOpts is a struct for upload and download options.

Jump to

Keyboard shortcuts

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