Documentation
¶
Overview ¶
Package session abstracts command execution against local or remote hosts.
Index ¶
- type LocalSession
- func (l *LocalSession) Close() error
- func (l *LocalSession) FileExists(ctx context.Context, path string) (bool, error)
- func (l *LocalSession) Host() string
- func (l *LocalSession) ReadFile(ctx context.Context, path string) ([]byte, error)
- func (l *LocalSession) Run(ctx context.Context, cmd string) (string, string, error)
- func (l *LocalSession) RunSudo(ctx context.Context, cmd string) (string, string, error)
- func (l *LocalSession) WriteFile(ctx context.Context, path string, content []byte, mode uint32) error
- type Opts
- type SSHSession
- func (s *SSHSession) Close() error
- func (s *SSHSession) Dial(o Opts) error
- func (s *SSHSession) FileExists(ctx context.Context, path string) (bool, error)
- func (s *SSHSession) Host() string
- func (s *SSHSession) ReadFile(ctx context.Context, path string) ([]byte, error)
- func (s *SSHSession) Run(ctx context.Context, cmd string) (string, string, error)
- func (s *SSHSession) RunSudo(ctx context.Context, cmd string) (string, string, error)
- func (s *SSHSession) WriteFile(ctx context.Context, path string, content []byte, mode uint32) error
- type Session
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LocalSession ¶
type LocalSession struct {
// contains filtered or unexported fields
}
LocalSession executes commands against the local host via /bin/sh.
func NewLocal ¶
func NewLocal() *LocalSession
NewLocal returns a local session bound to the current host.
func NewLocalWithOpts ¶
func NewLocalWithOpts(o Opts) *LocalSession
NewLocalWithOpts honours Opts.Host + Opts.SudoPassword.
func (*LocalSession) Close ¶
func (l *LocalSession) Close() error
Close is a no-op for local sessions.
func (*LocalSession) FileExists ¶
FileExists reports whether path exists.
type Opts ¶
type Opts struct {
Host string // hostname or "localhost"
User string // SSH user
Port int // default 22
KeyFile string // default ~/.ssh/id_ed25519
Password string // optional SSH password (rarely used)
StrictHostKeyCheck bool // if true, verify known_hosts
KnownHostsFile string // default ~/.ssh/known_hosts
Timeout time.Duration // default 30s
SudoPassword string // empty => `sudo -n` (NOPASSWD required)
}
Opts configures a session's transport + auth.
type SSHSession ¶
SSHSession is a remote command pipeline backed by golang.org/x/crypto/ssh. A single Session multiplexes commands over one TCP/TLS connection; callers should Close() when done.
func NewSSH ¶
func NewSSH(host, user string) *SSHSession
NewSSH returns a lightweight SSH descriptor. It does NOT dial — call Dial() (or NewSSHDial) to establish the transport. This two-phase API keeps the zero-value useful for tests that never touch the network.
func NewSSHDial ¶
func NewSSHDial(o Opts) (*SSHSession, error)
NewSSHDial constructs a session from Opts and dials immediately.
func (*SSHSession) Dial ¶
func (s *SSHSession) Dial(o Opts) error
Dial opens the SSH connection with up to 3 attempts (exponential backoff).
func (*SSHSession) FileExists ¶
FileExists tests via `test -e`.
type Session ¶
type Session interface {
// Host returns a human-readable identifier ("localhost" or "user@host").
Host() string
// Run executes cmd in the session context. Returns stdout, stderr, error.
Run(ctx context.Context, cmd string) (stdout, stderr string, err error)
// RunSudo executes cmd with sudo/root privileges.
RunSudo(ctx context.Context, cmd string) (stdout, stderr string, err error)
// WriteFile writes content to path with mode. Uses sudo if needed.
WriteFile(ctx context.Context, path string, content []byte, mode uint32) error
// ReadFile reads the entire contents of path.
ReadFile(ctx context.Context, path string) ([]byte, error)
// FileExists returns true if path exists on the target host.
FileExists(ctx context.Context, path string) (bool, error)
// Close releases the transport (no-op for local sessions).
Close() error
}
Session abstracts command execution + file I/O against local or remote hosts. Implementations: LocalSession (current host) and SSHSession (remote).