ssh

package
v0.0.0-...-c480f9e Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package ssh wraps golang.org/x/crypto/ssh + pkg/sftp into a Client that's narrow enough for the deploy orchestrator to use. It is NOT a general-purpose SSH library — Run / RunStream / Upload / Exists / Stat / MkdirAll / RemoveAll are the only operations.

A Client owns one underlying SSH connection and one SFTP subsystem. It is not safe for concurrent use — use one Client per deploy.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is the active SSH + SFTP session.

func Connect

func Connect(ctx context.Context, target Target, opts ConnectOpts) (*Client, error)

Connect dials the target host and opens an SFTP subsystem on top.

func (*Client) Close

func (c *Client) Close() error

Close shuts down the SFTP subsystem then the SSH connection.

func (*Client) Exists

func (c *Client) Exists(ctx context.Context, remotePath string) (bool, error)

Exists returns true if path exists on the remote. Symlinks are followed.

func (*Client) MkdirAll

func (c *Client) MkdirAll(ctx context.Context, remotePath string) error

MkdirAll creates remotePath and all missing parents. SFTP's stdlib has Mkdir but not MkdirAll — we walk the path and create each segment.

func (*Client) RemoveAll

func (c *Client) RemoveAll(ctx context.Context, remotePath string) error

RemoveAll deletes remotePath recursively. Safe for both files and dirs. Used by the rollback / prune flows. Returns nil if path doesn't exist.

func (*Client) Run

func (c *Client) Run(ctx context.Context, cmd string) (*RunResult, error)

Run executes cmd on the remote and buffers its stdout/stderr until exit. Use RunStream for long-running commands whose output should be visible as it arrives.

A non-zero exit code is NOT returned as an error — the caller decides whether that's failure (e.g. health-check) or expected (e.g. test if a file exists).

func (*Client) RunStream

func (c *Client) RunStream(ctx context.Context, cmd string, stdout, stderr io.Writer) (int, error)

RunStream executes cmd and copies stdout/stderr to the provided writers as they arrive. The integer return is the remote exit code; the error is non-nil only for transport-level failures (the caller distinguishes non-zero exit from broken transport).

func (*Client) SFTP

func (c *Client) SFTP() *sftp.Client

SFTP returns the underlying SFTP client for callers that need it directly (rare; prefer the wrapper methods).

func (*Client) Target

func (c *Client) Target() Target

Target returns the parsed destination.

func (*Client) Upload

func (c *Client) Upload(ctx context.Context, localPath, remotePath string) error

Upload streams a local file to the remote host via SFTP. The remote directory is created if it doesn't exist. The function honors context cancellation between chunks.

type ConnectOpts

type ConnectOpts struct {
	IdentityFile   string        // path to private key; ~ expanded; required
	KnownHostsFile string        // path to known_hosts; ~ expanded; default ~/.ssh/known_hosts
	Timeout        time.Duration // dial timeout; default 15s
	KeepAlive      time.Duration // SSH keep-alive interval; default 30s

	// AllowUnknownHosts skips host-key verification. Default false. Only
	// use in tests that target ephemeral containers.
	AllowUnknownHosts bool
}

ConnectOpts configures a new Client.

type RunResult

type RunResult struct {
	ExitCode int
	Stdout   []byte
	Stderr   []byte
}

RunResult is the captured output of a buffered remote command.

type Target

type Target struct {
	User string
	Host string
	Port int
}

Target is a parsed "user@host[:port]" SSH destination.

func ParseTarget

func ParseTarget(s string) (Target, error)

ParseTarget accepts "user@host" or "user@host:port". The port defaults to 22. IPv6 addresses in brackets ("user@[::1]:22") are supported.

func (Target) Addr

func (t Target) Addr() string

Addr returns the dial address ("host:port").

func (Target) String

func (t Target) String() string

String renders the target as "user@host:port".

Jump to

Keyboard shortcuts

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