executor

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2023 License: MIT Imports: 19 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

func MakeOutAndErrWriters

func MakeOutAndErrWriters(hostAddr, hostName string, verbose bool, secrets []string) (outWr, errWr io.Writer)

MakeOutAndErrWriters creates a new StdoutLogWriter and StdoutLogWriter for the given hostAddr.

Types

type ColorizedWriter

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

ColorizedWriter is a writer that colorizes the output based on the hostAddr name.

func NewColorizedWriter

func NewColorizedWriter(wr io.Writer, prefix, hostAddr, hostName string, secrets []string) *ColorizedWriter

NewColorizedWriter creates a new ColorizedWriter with the given hostAddr name.

func (*ColorizedWriter) WithHost

func (s *ColorizedWriter) WithHost(hostAddr, hostName string) *ColorizedWriter

WithHost creates a new StdoutColorWriter with the given hostAddr name.

func (*ColorizedWriter) Write

func (s *ColorizedWriter) Write(p []byte) (n int, err error)

Write writes the given byte slice to stdout with the colorized hostAddr prefix for each line. If the input does not end with a newline, one is added.

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) (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.

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(hostAddr, hostName string) *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, recursive bool) (err error)

Delete doesn't delete anything, just prints the command

func (*Dry) Download

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

Download file from remote server with scp

func (*Dry) Run

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

Run shows the command content, doesn't execute it

func (*Dry) SetSecrets

func (ex *Dry) SetSecrets(secrets []string)

SetSecrets sets secrets for the executor

func (*Dry) Sync

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

Sync doesn't sync anything, just prints the command

func (*Dry) Upload

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

Upload doesn't actually upload, just prints the command

type Interface

type Interface interface {
	SetSecrets(secrets []string)
	Run(ctx context.Context, c string, verbose bool) (out []string, err error)
	Upload(ctx context.Context, local, remote string, mkdir bool) (err error)
	Download(ctx context.Context, remote, local string, mkdir bool) (err error)
	Sync(ctx context.Context, localDir, remoteDir string, del bool) ([]string, error)
	Delete(ctx context.Context, remoteFile string, recursive bool) (err error)
	Close() error
}

Interface is an interface for the executor. Implemented by Remote and Local 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 (*Local) Close

func (l *Local) Close() error

Close does nothing for local

func (*Local) Delete

func (l *Local) Delete(_ context.Context, remoteFile string, recursive bool) (err error)

Delete file or directory

func (*Local) Download

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

Download just copy file from one place to another

func (*Local) Run

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

Run executes command on local hostAddr, inside the shell

func (*Local) SetSecrets

func (l *Local) SetSecrets(secrets []string)

SetSecrets sets the secrets for the remote executor.

func (*Local) Sync

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

Sync directories from src to dst

func (*Local) Upload

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

Upload just copy file from one place to another

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, recursive bool) (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, mkdir bool) (err error)

Download file from remote server with scp

func (*Remote) Run

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

Run command on remote server.

func (*Remote) SetSecrets

func (ex *Remote) SetSecrets(secrets []string)

SetSecrets sets the secrets for the remote executor.

func (*Remote) Sync

func (ex *Remote) Sync(ctx context.Context, localDir, remoteDir string, del bool) ([]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, mkdir bool) (err error)

Upload file to remote server with scp

type StdOutLogWriter

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

StdOutLogWriter is a writer that writes to log with a prefix and a log level.

func NewStdoutLogWriter

func NewStdoutLogWriter(prefix, level string, secrets []string) *StdOutLogWriter

NewStdoutLogWriter creates a new StdOutLogWriter.

func (*StdOutLogWriter) Write

func (w *StdOutLogWriter) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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