util

package
v0.0.0-...-26d883c Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package util is a general utility package for REPLbot

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckTmuxVersion

func CheckTmuxVersion() error

CheckTmuxVersion checks the version of tmux and returns an error if it's not supported

func FileExists

func FileExists(filenames ...string) bool

FileExists returns true if a file with the given filename exists

func FormatMarkdownCode

func FormatMarkdownCode(s string) string

FormatMarkdownCode formats the given string as a markdown code block

func InStringList

func InStringList(haystack []string, needle string) bool

InStringList returns true if needle is contained in the list of strings

func Quote

func Quote(s string) string

Quote returns a shell-escaped version of the string s. The returned value is a string that can safely be used as one token in a shell command line.

func QuoteCommand

func QuoteCommand(args []string) string

QuoteCommand returns a shell-escaped version of the slice of strings. The returned value is a string that can safely be used as shell command arguments.

func RandomPort

func RandomPort() (int, error)

RandomPort finds a free random, operating system chosen port

func RandomString

func RandomString(length int) string

RandomString returns a random alphanumeric string of the given length

func Run

func Run(command ...string) error

Run is a shortcut running an exec.Command

func RunAll

func RunAll(commands ...[]string) error

RunAll runs all the given commands

func SanitizeNonAlphanumeric

func SanitizeNonAlphanumeric(s string) string

SanitizeNonAlphanumeric replaces all non-alphanumeric characters with an underscore

func StringContainsWait

func StringContainsWait(haystackFn func() string, needle string, maxWait time.Duration) (contains bool)

StringContainsWait is a helper function for tests to check if a string is contained within a haystack within a reasonable amount of time

func StripUnsafe

func StripUnsafe(s string) string

StripUnsafe remove non-printable runes, e.g. control characters in a string that is meant for consumption by terminals that support control characters.

func TempFileName

func TempFileName() string

TempFileName generates a random file name for a file in the temp folder

func WaitUntil

func WaitUntil(fn func() bool, maxWait time.Duration) bool

WaitUntil waits for fn to turn true within a reasonable amount of time

func WaitUntilNot

func WaitUntilNot(fn func() bool, maxWait time.Duration) bool

WaitUntilNot waits for fn to turn false within a reasonable amount of time

Types

type SSHKeyPair

type SSHKeyPair struct {
	PrivateKey string
	PublicKey  string
}

SSHKeyPair represents an SSH key pair

func GenerateSSHKeyPair

func GenerateSSHKeyPair() (pair *SSHKeyPair, err error)

GenerateSSHKeyPair generates an SSH key pair

type Tmux

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

Tmux represents a very special tmux(1) setup, specifially used for dockerbot. It consists of two tmux sessions:

  • session "dockerbot_$id_frame": session with one window and three panes to allow us to resize the terminal of the main pane (.2). The main pane is .2, so that if it exits there is no other pane to take its place, which is easily detectable by the other panes. The main pane (.2) connects to the main session (see below).
  • session "dockerbot_$id_main": main session running the actual container.

func NewTmux

func NewTmux(id string, width, height int) *Tmux

NewTmux creates a new Tmux instance, but does not start the tmux

func (*Tmux) Active

func (s *Tmux) Active() bool

Active checks if the tmux is still active

func (*Tmux) Capture

func (s *Tmux) Capture() (string, error)

Capture returns a string representation of the current terminal

func (*Tmux) Cursor

func (s *Tmux) Cursor() (show bool, x int, y int, err error)

Cursor returns the X and Y position of the cursor

func (*Tmux) MainID

func (s *Tmux) MainID() string

MainID returns the session identifier for the main tmux session

func (*Tmux) Paste

func (s *Tmux) Paste(input string) error

Paste pastes the input into the tmux, as if the user entered it

func (*Tmux) RecordingFile

func (s *Tmux) RecordingFile() string

RecordingFile returns the file name of the recording file. This method can only be called after the session has exited. Before that, the file will not exist.

func (*Tmux) Resize

func (s *Tmux) Resize(width, height int) error

Resize resizes the active pane (.2) to the given size up to the max size

func (*Tmux) SendKeys

func (s *Tmux) SendKeys(keys ...string) error

SendKeys invokes the tmux send-keys command, which is useful for sending control sequences

func (*Tmux) Start

func (s *Tmux) Start(env map[string]string, command ...string) error

Start starts the tmux using the given command and arguments

func (*Tmux) Stop

func (s *Tmux) Stop() error

Stop kills the tmux and its command using the 'quit' command

Jump to

Keyboard shortcuts

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