remoter

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetHost

func SetHost(host string) options.Option

SetHost is an option to set the remote host address.

func SetInsecureIgnoreHostKey

func SetInsecureIgnoreHostKey(ignore bool) options.Option

SetInsecureIgnoreHostKey is an option to skip host key verification. Use only for testing; it disables MITM protection.

func SetKnownHostsPath

func SetKnownHostsPath(knownHostsPath string) options.Option

SetKnownHostsPath is an option to set the path to the known_hosts file.

func SetPassword

func SetPassword(password string) options.Option

SetPassword is an option to set the SSH password for authentication.

func SetPort

func SetPort(port int) options.Option

SetPort is an option to set the remote SSH port.

func SetPrivateKeyPassphrase

func SetPrivateKeyPassphrase(passphrase string) options.Option

SetPrivateKeyPassphrase is an option to set the passphrase for the private key.

func SetPrivateKeyPath

func SetPrivateKeyPath(privateKeyPath string) options.Option

SetPrivateKeyPath is an option to set the path to the SSH private key file.

func SetTimeout

func SetTimeout(timeout time.Duration) options.Option

SetTimeout is an option to set the SSH connection timeout.

func SetTrustUnknownHost

func SetTrustUnknownHost(trust bool) options.Option

SetTrustUnknownHost is an option to auto-trust and persist an unknown host key on first connection. Subsequent connections still verify the stored host key.

func SetUser

func SetUser(user string) options.Option

SetUser is an option to set the SSH login user.

Types

type IRemoter

type IRemoter interface {
	// Connect establishes an SSH connection; must be called before any other method.
	Connect() error
	// Close terminates the SSH connection.
	Close() error

	// RunCmd executes a command on the remote host and returns combined output (stdout + stderr).
	RunCmd(cmd string) ([]byte, error)
	// RunCmds executes multiple commands in a single remote shell context.
	RunCmds(cmds ...string) ([]byte, error)
	// RunCmdContext executes a command with context, supporting timeout and cancellation.
	RunCmdContext(ctx context.Context, cmd string) ([]byte, error)
	// RunCmdsContext executes multiple commands in a single remote shell context with context support.
	RunCmdsContext(ctx context.Context, cmds ...string) ([]byte, error)

	// UploadFile uploads a local file to the remote host via SFTP.
	UploadFile(localPath, remotePath string) error
	// UploadDir recursively syncs a local directory to the remote host.
	UploadDir(localDir, remoteDir string) error

	// DownloadFile downloads a remote file to the local machine via SFTP.
	DownloadFile(remotePath, localPath string) error

	// PathExists checks whether the given path (file or directory) exists on the remote host.
	PathExists(remotePath string) (bool, error)

	// CrateRemoteDir creates a remote directory recursively if needed.
	CrateRemoteDir(remoteDir string) error
	// DeleteFile deletes a file on the remote host.
	DeleteFile(remotePath string) error
	// DeleteDir recursively deletes a directory on the remote host.
	DeleteDir(remotePath string) error

	// WriteFile writes content to a remote file. Creates the file if it does not exist,
	// overwrites it if it does.
	WriteFile(remotePath string, content []byte, perm os.FileMode) error
	// ReadFile reads the content of a remote file.
	ReadFile(remotePath string) ([]byte, error)
}

IRemoter defines the remote operation interface, wrapping SSH + SFTP capabilities on top of goph (github.com/melbahja/goph).

type Remoter

type Remoter struct {
	Host     string
	Port     int
	User     string
	Password string

	PrivateKeyPath        string
	PrivateKeyPassphrase  string
	KnownHostsPath        string
	TrustUnknownHost      bool
	InsecureIgnoreHostKey bool
	Timeout               time.Duration
	// contains filtered or unexported fields
}

Remoter is the concrete implementation of IRemoter using goph. It supports SSH connections via password, private key, or ssh-agent, and provides SFTP-based file operations.

func NewRemoter

func NewRemoter(opts ...options.Option) *Remoter

NewRemoter creates a new Remoter instance with default port (22) and default timeout (20s). Use option functions to override defaults.

func (*Remoter) Close

func (r *Remoter) Close() error

Close terminates the SSH connection and releases the SFTP client. It is safe to call multiple times.

func (*Remoter) Connect

func (r *Remoter) Connect() error

Connect establishes the SSH connection. It is safe to call multiple times; subsequent calls are no-ops if the connection is already established.

func (*Remoter) CrateRemoteDir

func (r *Remoter) CrateRemoteDir(remoteDir string) error

CrateRemoteDir creates a remote directory. @param remoteDir remote directory path

func (*Remoter) DeleteDir

func (r *Remoter) DeleteDir(remotePath string) error

DeleteDir recursively deletes a directory and all its contents on the remote host. Returns an error if the path is not a directory.

func (*Remoter) DeleteFile

func (r *Remoter) DeleteFile(remotePath string) error

DeleteFile deletes a single file on the remote host. Returns an error if the path is a directory.

func (*Remoter) DownloadFile

func (r *Remoter) DownloadFile(remotePath, localPath string) error

DownloadFile downloads a single remote file to the local machine via SFTP. Local parent directories are created automatically if they do not exist.

func (*Remoter) PathExists

func (r *Remoter) PathExists(remotePath string) (bool, error)

PathExists checks whether a file or directory exists at the given remote path.

func (*Remoter) ReadFile

func (r *Remoter) ReadFile(remotePath string) ([]byte, error)

ReadFile reads the entire content of a remote file via SFTP.

func (*Remoter) RunCmd

func (r *Remoter) RunCmd(cmd string) ([]byte, error)

RunCmd executes a command on the remote host and returns the combined stdout and stderr output. It auto-connects if not already connected.

func (*Remoter) RunCmdContext

func (r *Remoter) RunCmdContext(ctx context.Context, cmd string) ([]byte, error)

RunCmdContext is like RunCmd but accepts a context for timeout and cancellation.

func (*Remoter) RunCmds

func (r *Remoter) RunCmds(cmds ...string) ([]byte, error)

RunCmds executes multiple commands on the remote host in a single shell context. It auto-connects if not already connected.

func (*Remoter) RunCmdsContext

func (r *Remoter) RunCmdsContext(ctx context.Context, cmds ...string) ([]byte, error)

RunCmdsContext is like RunCmds but accepts a context for timeout and cancellation.

func (*Remoter) UploadDir

func (r *Remoter) UploadDir(localDir, remoteDir string) error

UploadDir recursively syncs an entire local directory to the remote host via SFTP. It creates the remote directory tree and uploads all files with their original permissions.

func (*Remoter) UploadFile

func (r *Remoter) UploadFile(localPath, remotePath string) error

UploadFile uploads a single local file to the remote host via SFTP. For directory sync, use UploadDir instead.

func (*Remoter) WriteFile

func (r *Remoter) WriteFile(remotePath string, content []byte, perm os.FileMode) error

WriteFile writes content to a remote file, creating parent directories as needed. It uses an atomic write-via-tempfile approach: writes to a temp file first, then renames it to the target path. Creates the file if absent, overwrites if present.

Jump to

Keyboard shortcuts

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