Session Operations¶
All session methods are called on a connected *client.Client. See the
Go SDK overview for how to obtain one.
Create¶
Start a new PTY session with the given ID and parameters.
info, err := c.Create("build-1", client.CreateParams{
Shell: "/bin/bash",
Args: []string{"-l"},
Cols: 120,
Rows: 40,
Cwd: "/home/user/project",
Env: []string{"TERM=xterm-256color", "LANG=en_US.UTF-8"},
})
CreateParams fields:
| Field | Type | Description |
|---|---|---|
Shell |
string |
Path to the shell binary. Empty uses the system default. |
Args |
[]string |
Arguments passed to the shell. |
Cols |
int |
Initial terminal width in columns. |
Rows |
int |
Initial terminal height in rows. |
Cwd |
string |
Working directory. Empty uses the daemon's cwd. |
Env |
[]string |
Environment variables in KEY=VALUE format. |
Returns a SessionInfo with the session metadata (ID, state, PID, dimensions,
shell path).
Attach¶
Attach to an existing session to receive its output and capture a terminal snapshot.
result, err := c.Attach("build-1")
if err != nil {
log.Fatal(err)
}
// Session metadata
log.Printf("pid=%d cols=%d rows=%d", result.Session.Pid, result.Session.Cols, result.Session.Rows)
// Terminal snapshot (requires an emulator backend such as charmvt)
replay := result.Snapshot.Replay
AttachResult contains:
Session-- aSessionInfostruct with current metadata.Snapshot-- aSnapshotstruct with aReplaybyte slice containing the unified replay stream (scrollback history followed by viewport state).Replayis nil when no emulator backend is configured ("none").
Detach¶
Stop receiving output from a session without terminating it.
Kill¶
Terminate a session and its underlying PTY process.
To kill all sessions matching a prefix:
result, err := c.KillPrefix("build-")
// result.Killed: []string of terminated session IDs
// result.Errors: map of session ID to error string (best-effort)
Write¶
Send raw bytes to a session's PTY input. This is the low-level input method.
Exec¶
Send a string to a session. By default, a newline is appended.
To suppress the trailing newline:
To send input to multiple sessions at once:
results, err := c.ExecSync("make clean", "build-1", "build-2", "build-3")
for _, r := range results {
if !r.OK {
log.Printf("session %s: %s", r.SessionID, r.Error)
}
}
To broadcast to all sessions matching a prefix:
Resize¶
Change a session's terminal dimensions.
List¶
Retrieve all active sessions.
sessions, err := c.List()
for _, s := range sessions {
log.Printf("%s state=%s pid=%d (%dx%d)", s.ID, s.State, s.Pid, s.Cols, s.Rows)
}
Filter by prefix:
Info¶
Get metadata for a single session.
SessionInfo fields:
| Field | Type | Description |
|---|---|---|
ID |
string |
Unique session identifier |
State |
string |
Lifecycle state (e.g., "running", "exited") |
Pid |
int |
Shell process ID |
Cols |
int |
Terminal width |
Rows |
int |
Terminal height |
Shell |
string |
Shell binary path |