Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268package rex
import ( "context" "fmt" "strings")
// Session names a session.type Session struct { SessionID string `json:"session_id"` Label string `json:"label"`}
// View is a session's windows, their layouts, and their blocks, as// session.view returns it.type View struct { // Revision goes up with every change to the view. A larger revision is // a newer view, even across a server restart. Revision uint64 `json:"revision"` SessionID string `json:"session_id"` Label string `json:"label"` ActiveWindowID string `json:"active_window_id"` Windows []Window `json:"windows"`}
// ActiveWindow returns the window the session shows, or nil if it has none.func (v *View) ActiveWindow() *Window { return v.Window(v.ActiveWindowID) }
// Window returns the window with the given ID, or nil.func (v *View) Window(id string) *Window { for i := range v.Windows { if v.Windows[i].WindowID == id { return &v.Windows[i] } } return nil}
// WindowIndex returns the position of the window with the given ID in the// window order, or -1.func (v *View) WindowIndex(id string) int { for i := range v.Windows { if v.Windows[i].WindowID == id { return i } } return -1}
// Window is a tab within a session.type Window struct { WindowID string `json:"window_id"` Label string `json:"label"` FocusedBlockID string `json:"focused_block_id"` ZoomedBlockID string `json:"zoomed_block_id"` Layers []Layer `json:"layers"`}
// Block returns the window's block with the given ID, or nil.func (w *Window) Block(id string) *BlockInfo { for i := range w.Layers { for j := range w.Layers[i].Blocks { if b := &w.Layers[i].Blocks[j]; b.BlockID == id { return b } } } return nil}
// Layer is a window's tiled layout, or a floating layer above it.type Layer struct { LayerID string `json:"layer_id"` // Kind is tiled for the window's layout, or floating. Kind string `json:"kind"` // Bounds is the layer's x, y, width, and height, as fractions of the // window. Bounds [4]float64 `json:"bounds"` State string `json:"state"` Layout *LayoutNode `json:"layout"` Blocks []BlockInfo `json:"blocks"`}
// LayoutNode is either a block or a split of two nodes.type LayoutNode struct { // Rect is the node's x, y, width, and height, as fractions of the // window. Rect [4]float64 `json:"rect"` BlockID string `json:"block_id"` Split *Split `json:"split"`}
// Split divides its space between two nodes.type Split struct { SplitID string `json:"split_id"` // Direction is horizontal (side by side) or vertical (one above the // other). Direction string `json:"direction"` Ratio float64 `json:"ratio"` Before *LayoutNode `json:"before"` After *LayoutNode `json:"after"`}
// BlockInfo describes a block in a view.type BlockInfo struct { BlockID string `json:"block_id"` Label string `json:"label"` CreatorName string `json:"creator_name"` Flavor string `json:"flavor"` HostID string `json:"host_id"` // Owner is the client that owns the block, or "" if none does. Owner string `json:"owner"`}
// AttachedSession is a session a connection attached to, with its view.type AttachedSession struct { Session Session `json:"session"` View View `json:"view"`}
// ServerStatus is part of what server.status returns.type ServerStatus struct { PID int `json:"pid"` InstanceID string `json:"instance_id"` HostID string `json:"host_id"` StartedAt string `json:"started_at"` SessionCount int `json:"session_count"` Tailscale struct { State string `json:"state"` DNSName string `json:"dns_name"` } `json:"tailscale"`}
// Whoami is what server.whoami returns.type Whoami struct { Principal struct { Kind string `json:"kind"` Display string `json:"display"` } `json:"principal"` // Account is the operating system account the server runs as, or "". Account string `json:"account"`}
// ServerWhoami returns how the server identified this client, and the// account it runs as.func (c *Conn) ServerWhoami(ctx context.Context) (Whoami, error) { var w Whoami _, err := c.Call(ctx, "server.whoami", nil, &w) return w, err}
// DataTicketRequest asks session.bootstrap for data tickets for every visible// block that a creator made.type DataTicketRequest struct { CreatorName string `json:"creator_name"` DataRefID string `json:"data_ref_id"` Subprotocol string `json:"subprotocol,omitempty"`}
// DataTicket is a one-time ticket for opening a data connection to a block.type DataTicket struct { SessionID string `json:"session_id"` BlockID string `json:"block_id"` DataRefID string `json:"data_ref_id"` Subprotocol string `json:"subprotocol"` Ticket string `json:"ticket"` ExpiresInMS int64 `json:"expires_in_ms"`}
// BootstrapArgs are session.bootstrap's arguments.type BootstrapArgs struct { // EventScope is session or all. EventScope string `json:"event_scope"` // AttachSessionIDs lists the sessions to attach to. Nil lets the server // pick one, or PreferredSessionID if it exists; an empty, non-nil slice // attaches none. AttachSessionIDs []string `json:"attach_session_ids,omitzero"` PreferredSessionID string `json:"preferred_session_id,omitempty"` DataTickets []DataTicketRequest `json:"data_tickets,omitempty"`}
// BootstrapResult is what session.bootstrap returns.type BootstrapResult struct { ClientID string `json:"client_id"` ServerStatus ServerStatus `json:"server_status"` AttachAll struct { Sessions []Session `json:"sessions"` } `json:"attach_all"` // SelectedSession is the session the server picked, when // AttachSessionIDs was nil. It is nil if the server has no sessions. SelectedSession *AttachedSession `json:"selected_session"` // AttachedSessions are the sessions attached from AttachSessionIDs. AttachedSessions []AttachedSession `json:"attached_sessions"` DataTickets []DataTicket `json:"data_tickets"`}
// Bootstrap sets up a new connection in one call: it starts events, lists the// sessions, and attaches. Send it as the first request on a connection, then// apply only events whose seq is higher than the returned seq.func (c *Conn) Bootstrap(ctx context.Context, args BootstrapArgs) (BootstrapResult, uint64, error) { var result BootstrapResult seq, err := c.Call(ctx, "session.bootstrap", args, &result) return result, seq, err}
// SessionView returns a session's current view.func (c *Conn) SessionView(ctx context.Context, sessionID string) (View, uint64, error) { var view View seq, err := c.Call(ctx, "session.view", map[string]string{"session_id": sessionID}, &view) return view, seq, err}
// Version is what server.version returns.type Version struct { RexVersion string `json:"rex_version"` Platform string `json:"platform"` Commit string `json:"commit"` BuiltAt string `json:"built_at"` Dirty bool `json:"dirty"`}
// ServerVersion returns the version of Rex the server runs.func (c *Conn) ServerVersion(ctx context.Context) (Version, error) { var v Version _, err := c.Call(ctx, "server.version", nil, &v) return v, err}
// Attach attaches the connection to a session, so that it receives the// session's events and can open data connections to its blocks, and returns// the session's current view.func (c *Conn) Attach(ctx context.Context, sessionID string) (View, uint64, error) { var result struct { View View `json:"view"` } seq, err := c.Call(ctx, "session.attach", map[string]string{"session_id": sessionID}, &result) return result.View, seq, err}
// FindSession returns the session that query names: its ID, its label, or// the end of its ID, as `rex ls` shows it.func FindSession(sessions []Session, query string) (Session, error) { for _, s := range sessions { if s.SessionID == query { return s, nil } } for _, s := range sessions { if s.Label == query { return s, nil } } var found []Session for _, s := range sessions { if strings.HasSuffix(s.SessionID, query) { found = append(found, s) } } switch len(found) { case 1: return found[0], nil case 0: return Session{}, fmt.Errorf("no session named %q", query) } return Session{}, fmt.Errorf("%q names %d sessions", query, len(found))}