commands

package
v0.0.0-...-6a41818 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: Apache-2.0 Imports: 37 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	ModeMaskUser  fs.FileMode = 0700
	ModeMaskGroup             = 0070
	ModeMaskOther             = 0007
	ModeMaskAll               = ModeMaskUser | ModeMaskGroup | ModeMaskOther

	ModeRead  fs.FileMode = 0444
	ModeWrite             = 0222
	ModeExec              = 0111

	ChmodMask = ModeMaskAll
)
View Source
const (
	EnvHome            = "HOME"
	EnvPWD             = "PWD"
	EnvPath            = "PATH"
	EnvPrompt          = "PS1"
	EnvHostname        = "HOSTNAME"
	EnvUser            = "USER"
	EnvUID             = "UID"
	DefaultColorPrompt = `\033[01;32m\u@\h\033[00m:\033[01;34m\w\033[00m\$ `
	DefaultPrompt      = `\u@\h:\w\$ `
)

Variables

View Source
var (
	ColorBoldBlue  = color.New(color.FgBlue, color.Bold)
	ColorBoldGreen = color.New(color.FgGreen, color.Bold)
	ColorBoldCyan  = fcolor.New(color.FgCyan, color.Bold)
	ColorBoldRed   = color.New(color.FgRed, color.Bold)
)
View Source
var AllBuiltins = make(map[string]ShellBuiltin)

AllBuiltins holds a list of all registered shell builtins

Functions

func Apt

func Apt(virtOS vos.VOS) int

Apt implements a fake apt command.

func BuiltinProcessResolver

func BuiltinProcessResolver(command string) vos.ProcessFunc

BuiltinProcessResolver implemnts vos.ProcessResolver, it returns the builtin command with the given path or nil if none exists.

func BytesToHuman

func BytesToHuman(bytes int64) string
Example
// < 1k is presented directly
fmt.Println(BytesToHuman(512))

// Multiples > 10 are shown without decimal.
fmt.Println(BytesToHuman(23 * 10e8))

// Multiples < 10 are shown with decimal.
fmt.Println(BytesToHuman(5 * 1024))
Output:

512
23G
5.1K

func Cat

func Cat(virtOS vos.VOS) int

Cat implements the POSIX cat command.

https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/

func Cd

func Cd(s *Shell, args []string) int

Cd is the cd shell builtin

func ChmodApplyMode

func ChmodApplyMode(mode string, orig fs.FileMode) (fs.FileMode, error)

func Clear

func Clear(virtOS vos.VOS) int

Clear sends an ANSI clear command if connected to a PTY.

func Curl

func Curl(virtOS vos.VOS) int

Curl implements a curl command.

func Dircolor

func Dircolor(fileInfo os.FileInfo) *fcolor.Color

func Echo

func Echo(virtOS vos.VOS) int

Echo implements a limited echo command.

func Env

func Env(virtOS vos.VOS) int

Env implements the POSIX env command.

https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/env.html

func Exit

func Exit(s *Shell, args []string) int

Exit quits the shell

func Free

func Free(virtOS vos.VOS) int

Free implements a fake free command.

func Grep

func Grep(virtOS vos.VOS) int

Grep implements the POSIX grep command.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html

func Help

func Help(s *Shell, args []string) int

func History

func History(s *Shell, args []string) int

func Hostname

func Hostname(virtOS vos.VOS) int

Hostname implements the Linux command by the same name.

func Id

func Id(virtOS vos.VOS) int

Id implements a the POSIX id command.

https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/id.html

func Ifconfig

func Ifconfig(virtOS vos.VOS) int

Ifconfig implements the ifconfig command.

func Ip

func Ip(virtOS vos.VOS) int

Ip implements the ip command (newer replacemnet for ifconfig)

func Last

func Last(virtOS vos.VOS) int

Last implements a fake last command.

func Ls

func Ls(virtOS vos.VOS) int

Ls implements the UNIX ls command.

func Mail

func Mail(virtOS vos.VOS) int

Mail implements a no-op POSIX mail command.

https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/mailx.html

func Mkdir

func Mkdir(virtOS vos.VOS) int

Mkdir implements a POSIX mkdir command.

https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/mkdir.html

func NewWcCount

func NewWcCount(name string, fd io.Reader) (*wcCount, error)

func Nice

func Nice(virtOS vos.VOS) int

Nice implements a fake POSIX nice command.

https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/nice.html

func NopBuiltin

func NopBuiltin(s *Shell, args []string) int

func Passwd

func Passwd(virtualOS vos.VOS) int

Passwd implements a fake passwd command.

func Ps

func Ps(virtOS vos.VOS) int

Ps implements a fake ps command.

func Pwd

func Pwd(virtOS vos.VOS) int

Pwd implements the UNIX pwd command.

func Reboot

func Reboot(virtOS vos.VOS) int

Reboot terminates the remote connection.

func Reset

func Reset(virtOS vos.VOS) int

Reset sends an ANSI reset command if connected to a PTY.

func Rm

func Rm(virtOS vos.VOS) int

Rm implements a POSIX rm command.

func Rmdir

func Rmdir(virtOS vos.VOS) int

Rmdir implements a POSIX rmdir command.

func RunShell

func RunShell(virtualOS vos.VOS) int

func Scp

func Scp(virtOS vos.VOS) int

Scp implements an SCP command that only uploads.

func Sudo

func Sudo(s *Shell, args []string) int

func Tar

func Tar(virtOS vos.VOS) int

Tar implements a basic tar command.

func Touch

func Touch(virtOS vos.VOS) int

Touch implements a POSIX touch command.

func UidResolver

func UidResolver(virtOS vos.VOS) (resolver func(int) string)

func Uname

func Uname(virtOS vos.VOS) int

Uname implements the POSIX command by the same name.

func Unset

func Unset(s *Shell, args []string) int

func Unzip

func Unzip(virtOS vos.VOS) int

Unzip implements a basic unzip command.

func Uptime

func Uptime(virtOS vos.VOS) int

Uptime implements the UNIX uptime command.

func W

func W(virtOS vos.VOS) int

W implements the UNIX w command.

func Wc

func Wc(virtOS vos.VOS) int

Wc implements the POSIX command by the same name. https://pubs.opengroup.org/onlinepubs/009695399/utilities/wc.html

func Wget

func Wget(virtOS vos.VOS) int

Wget implements a wget command.

func Which

func Which(virtOS vos.VOS) int

Which implements the UNIX which command.

func Whoami

func Whoami(virtOS vos.VOS) int

Whoami implements the POSIX whoami command.

Types

type ColorPrinter

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

func (*ColorPrinter) Init

func (c *ColorPrinter) Init(flags *getopt.Set, virtOS vos.VOS)

Init sets up the flag and virtual OS to determine the color output.

func (*ColorPrinter) ShouldColor

func (c *ColorPrinter) ShouldColor() bool

func (*ColorPrinter) Sprintf

func (c *ColorPrinter) Sprintf(color *color.Color, format string, a ...interface{}) string

type CommandEntry

type CommandEntry struct {
	Names []string
	Proc  vos.ProcessFunc
}

func ListBuiltinCommands

func ListBuiltinCommands() []CommandEntry

type LsColorTest

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

type NoOpCommand

type NoOpCommand struct {
	Name     string
	Use      string
	Short    string
	Stdout   string
	ExitCode int
}

No-op commands.

func (*NoOpCommand) ToCommand

func (c *NoOpCommand) ToCommand() vos.ProcessFunc

Convert the no-op command description to a functioning command.

type Shell

type Shell struct {
	VirtualOS vos.VOS
	Readline  *readline.Instance

	// Set to true to quit the shell
	Quit bool
	// contains filtered or unexported fields
}

func NewShell

func NewShell(virtualOS vos.VOS) (*Shell, error)

func (*Shell) Init

func (s *Shell) Init(username string)

Init sets up the environment similar to login + source ~/.bashrc.

type ShellBuiltin

type ShellBuiltin interface {
	Main(s *Shell, args []string) int
}

type ShellBuiltinFunc

type ShellBuiltinFunc func(s *Shell, args []string) int

func (ShellBuiltinFunc) Main

func (f ShellBuiltinFunc) Main(s *Shell, args []string) int

type SimpleCommand

type SimpleCommand struct {
	// Use holds a one line usage string
	Use string
	// Short holds a sone line description of the command.
	Short string
	// ShowHelp sets whether help is displayed or not.
	// If this is non-nil when Run() is called, then the default help flag isn't
	// added.
	ShowHelp *bool
	// NeverBail skips interacting with stdout/stderr on failure and
	// always runs the callback.
	NeverBail bool
	// contains filtered or unexported fields
}

func (*SimpleCommand) Flags

func (s *SimpleCommand) Flags() *getopt.Set

Flags gets the command's flag set.

func (*SimpleCommand) LogProgramError

func (s *SimpleCommand) LogProgramError(virtOS vos.VOS, err error)

Log a program error to stderr in the form "program name: error message"

func (*SimpleCommand) PrintHelp

func (s *SimpleCommand) PrintHelp(w io.Writer)

PrintHelp writes help for the command to the given writer.

func (*SimpleCommand) Run

func (s *SimpleCommand) Run(virtOS vos.VOS, callback func() int) int

Run the command, if flag parsing was succcessful call the callback.

func (*SimpleCommand) RunE

func (s *SimpleCommand) RunE(virtOS vos.VOS, callback func() error) int

RunE runs the callback and converts the error into a return value and message.

func (*SimpleCommand) RunEachArg

func (s *SimpleCommand) RunEachArg(virtOS vos.VOS, callback func(string) error) int

RunEachArg runs the callback for every supplied arg.

func (*SimpleCommand) RunEachFileOrStdin

func (s *SimpleCommand) RunEachFileOrStdin(virtOS vos.VOS, files []string, callback func(name string, fd io.Reader) error) int

RunEachFileOrStdin runs the callback for every supplied arg, or stdin

Jump to

Keyboard shortcuts

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