gopty

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2025 License: MIT Imports: 14 Imported by: 1

README

gopty

GoDoc

gopty is a cross-platform PTY interface. On *nix platforms we rely on pty and on windows go-winpty (gopty will ship winpty-0.4.3-msvc2015 using go:embed, so there's no need to include winpty binaries)

Example

package main

import (
	"io"
	"log"
	"os"
	"runtime"
	"sync"

	"github.com/admpub/gopty"
)

func main() {

	proc, err := gopty.New(120, 60)
	if err != nil {
		panic(err)
	}
	defer proc.Close()

	var args []string

	if runtime.GOOS == "windows" {
		args = []string{"cmd.exe", "/c", "dir"}
	} else {
		args = []string{"ls", "-lah", "--color"}
	}

	if err := proc.Start(args); err != nil {
		panic(err)
	}

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()

		_, err = io.Copy(os.Stdout, proc)
		if err != nil {
			log.Printf("Error: %v\n", err)
		}
	}()

	if _, err := proc.Wait(); err != nil {
		log.Printf("Wait err: %v\n", err)
	}

	wg.Wait()
}

Documentation

Index

Constants

View Source
const (
	// TextMessage denotes a text data message. The text message payload is
	// interpreted as UTF-8 encoded text data.
	TextMessage = 1

	// BinaryMessage denotes a binary data message.
	BinaryMessage = 2

	// CloseMessage denotes a close control message. The optional message
	// payload contains a numeric code and text. Use the FormatCloseMessage
	// function to format a close message payload.
	CloseMessage = 8

	// PingMessage denotes a ping control message. The optional message payload
	// is UTF-8 encoded text.
	PingMessage = 9

	// PongMessage denotes a pong control message. The optional message payload
	// is UTF-8 encoded text.
	PongMessage = 10
)

The message types are defined in RFC 6455, section 11.8.

Variables

View Source
var (
	ErrProcessNotStarted = errors.New("Process has not been started")
	ErrInvalidCmd        = errors.New("Invalid command")
)

Functions

func Execute

func Execute(command string, resultWriter io.Writer) error

Execute execute command

func GetBash

func GetBash() string

GetBash get bash file

func GetFlagVar

func GetFlagVar() string

GetFlagVar bash flag variable name

func IsDangerCommand added in v0.0.2

func IsDangerCommand(b []byte) bool

func PTY2Websocket

func PTY2Websocket(ws WebsocketWriter, pty interfaces.Console)

PTY2Websocket pty to websocket

func ServeWebsocket

func ServeWebsocket(wsc Websocketer, cols, rows int) error

ServeWebsocket ServeWebsocket(wsc,120,60)

func Start added in v0.1.3

func Start(pty Console) error

func Websocket2PTY

func Websocket2PTY(ws Websocketer, pty interfaces.Console)

Websocket2PTY websocket to pty

Types

type Console

type Console interfaces.Console

Console communication interface

func New

func New(w int, h int) (Console, error)

New creates a new console with initial size

type WebsocketReader

type WebsocketReader interface {
	ReadMessage() (int, []byte, error)
}

WebsocketReader websocket reader

type WebsocketWriter

type WebsocketWriter interface {
	WriteMessage(int, []byte) error
}

WebsocketWriter websocket writer

type Websocketer

type Websocketer interface {
	WebsocketWriter
	WebsocketReader
}

Websocketer websocket interface github.com/admpub/websocket github.com/gorilla/websocket

Directories

Path Synopsis
examples
simple command

Jump to

Keyboard shortcuts

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