shell

package
v3.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2024 License: LGPL-3.0 Imports: 12 Imported by: 14

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DumpFileOnErrorScript

func DumpFileOnErrorScript(filename string) string

DumpFileOnErrorScript returns a bash script that may be used to dump the contents of the specified file to stderr when the shell exits with an error.

func NewPSEncodedCommand

func NewPSEncodedCommand(script string) (string, error)

NewPSEncodedCommand converts the given string to a UTF16-LE, base64 encoded string, suitable for execution using powershell.exe -EncodedCommand. This can be used on local systems, as well as remote systems via WinRM.

func ResolveFD

func ResolveFD(name string) (int, bool)

ResolveFD converts the file descriptor name to the corresponding int. "stdout" and "out" match stdout (FD 1). "stderr" and "err" match stderr (FD 2), "stdin" and "in" match stdin (FD 0). All positive integers match. If there should be an upper bound then the caller should check it on the result. If the provided name is empty then the result defaults to stdout. If the name is not recognized then false is returned.

func WriteScript

func WriteScript(renderer ScriptWriter, name, dirname string, script []string) []string

WriteScript returns a sequence of shell commands that write the provided shell commands to a file. The filename is composed from the given directory name and name, and the appropriate suffix for a shell script is applied. The script content is prefixed with any necessary content related to shell scripts (e.g. a shbang line). The file's permissions are set to those appropriate for a script (e.g. 0755).

Types

type BashRenderer

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

BashRenderer is the shell renderer for bash.

func (BashRenderer) Chmod

func (ur BashRenderer) Chmod(path string, perm os.FileMode) []string

Chmod implements Renderer.

func (BashRenderer) Chown

func (ur BashRenderer) Chown(path, owner, group string) []string

Chown implements Renderer.

func (BashRenderer) ExeSuffix

func (BashRenderer) ExeSuffix() string

ExeSuffix implements Renderer.

func (BashRenderer) Mkdir

func (ur BashRenderer) Mkdir(dirname string) []string

Mkdir implements Renderer.

func (BashRenderer) MkdirAll

func (ur BashRenderer) MkdirAll(dirname string) []string

MkdirAll implements Renderer.

func (BashRenderer) Quote

func (BashRenderer) Quote(str string) string

Quote implements Renderer.

func (BashRenderer) RedirectFD

func (ur BashRenderer) RedirectFD(dst, src string) []string

RedirectFD implements OutputRenderer.

func (BashRenderer) RedirectOutput

func (ur BashRenderer) RedirectOutput(filename string) []string

RedirectOutput implements OutputRenderer.

func (BashRenderer) RedirectOutputReset

func (ur BashRenderer) RedirectOutputReset(filename string) []string

RedirectOutputReset implements OutputRenderer.

func (*BashRenderer) RenderScript

func (*BashRenderer) RenderScript(commands []string) []byte

Render implements ScriptWriter.

func (*BashRenderer) ScriptFilename

func (ur *BashRenderer) ScriptFilename(name, dirname string) string

ScriptFilename implements ScriptWriter.

func (*BashRenderer) ScriptPermissions

func (ur *BashRenderer) ScriptPermissions() os.FileMode

ScriptPermissions implements ScriptWriter.

func (BashRenderer) Touch

func (ur BashRenderer) Touch(path string, timestamp *time.Time) []string

Touch implements Renderer.

func (BashRenderer) WriteFile

func (ur BashRenderer) WriteFile(filename string, data []byte) []string

WriteFile implements Renderer.

type CommandRenderer

type CommandRenderer interface {
	// Chown returns a shell command for changing the ownership of
	// a file or directory. The copies the behavior of os.Chown,
	// though it also supports names in addition to ints.
	Chown(name, user, group string) []string

	// Chmod returns a shell command that sets the given file's
	// permissions. The result is equivalent to os.Chmod.
	Chmod(path string, perm os.FileMode) []string

	// WriteFile returns a shell command that writes the provided
	// content to a file. The command is functionally equivalent to
	// ioutil.WriteFile with permissions from the current umask.
	WriteFile(filename string, data []byte) []string

	// Mkdir returns a shell command for creating a directory. The
	// command is functionally equivalent to os.MkDir using permissions
	// appropriate for a directory.
	Mkdir(dirname string) []string

	// MkdirAll returns a shell command for creating a directory and
	// all missing parent directories. The command is functionally
	// equivalent to os.MkDirAll using permissions appropriate for
	// a directory.
	MkdirAll(dirname string) []string

	// Touch returns a shell command that updates the atime and ctime
	// of the named file. If the provided timestamp is nil then the
	// current time is used. If the file does not exist then it is
	// created. If UTC is desired then Time.UTC() should be called
	// before calling Touch.
	Touch(filename string, timestamp *time.Time) []string
}

CommandRenderer provides methods that may be used to generate shell commands for a variety of shell and filesystem operations.

type OutputRenderer

type OutputRenderer interface {
	// RedirectFD returns a shell command that redirects the src
	// file descriptor to the dst one.
	RedirectFD(dst, src string) []string

	// RedirectOutput will cause all subsequent output from the shell
	// (or script) to go be appended to the given file. Only stdout is
	// redirected (use RedirectFD to redirect stderr or other FDs).
	//
	// The file should already exist (so a call to Touch may be
	// necessary before calling RedirectOutput). If the file should have
	// specific permissions or a specific owner then Chmod and Chown
	// should be called before calling RedirectOutput.
	RedirectOutput(filename string) []string

	// RedirectOutputReset will cause all subsequent output from the
	// shell (or script) to go be written to the given file. The file
	// will be reset (truncated to 0) before anything is written. Only
	// stdout is redirected (use RedirectFD to redirect stderr or
	// other FDs).
	//
	// The file should already exist (so a call to Touch may be
	// necessary before calling RedirectOutputReset). If the file should
	// have specific permissions or a specific owner then Chmod and
	// Chown should be called before calling RedirectOutputReset.
	RedirectOutputReset(filename string) []string
}

OutputRenderer exposes the Renderer methods that relate to shell output.

The methods all accept strings to identify their file descriptor arguments. While the interpretation of these values is up to the renderer, it will likely conform to the result of calling ResolveFD. If an FD arg is not recognized then no commands will be returned. Unless otherwise specified, the default file descriptor is stdout (FD 1). This applies to the empty string.

type PathRenderer

type PathRenderer interface {
	filepath.Renderer

	// Quote generates a new string with quotation marks and relevant
	// escape/control characters properly escaped. The resulting string
	// is wrapped in quotation marks such that it will be treated as a
	// single string by the shell.
	Quote(str string) string

	// ExeSuffix returns the filename suffix for executable files.
	ExeSuffix() string
}

A PathRenderer generates paths that are appropriate for a given shell environment.

type PowershellRenderer

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

PowershellRenderer is a shell renderer for Windows Powershell.

func (*PowershellRenderer) Chmod

func (pr *PowershellRenderer) Chmod(path string, perm os.FileMode) []string

Chmod implements Renderer.

func (PowershellRenderer) Chown

func (w PowershellRenderer) Chown(path, owner, group string) []string

Chown implements Renderer.

func (*PowershellRenderer) ExeSuffix

func (w *PowershellRenderer) ExeSuffix() string

ExeSuffix implements Renderer.

func (*PowershellRenderer) Mkdir

func (pr *PowershellRenderer) Mkdir(dirname string) []string

MkDir implements Renderer.

func (*PowershellRenderer) MkdirAll

func (pr *PowershellRenderer) MkdirAll(dirname string) []string

MkdirAll implements Renderer.

func (*PowershellRenderer) Quote

func (pr *PowershellRenderer) Quote(str string) string

Quote implements Renderer.

func (PowershellRenderer) RedirectFD

func (w PowershellRenderer) RedirectFD(dst, src string) []string

RedirectFD implements OutputRenderer.

func (PowershellRenderer) RedirectOutput

func (w PowershellRenderer) RedirectOutput(filename string) []string

RedirectOutput implements OutputRenderer.

func (PowershellRenderer) RedirectOutputReset

func (w PowershellRenderer) RedirectOutputReset(filename string) []string

RedirectOutputReset implements OutputRenderer.

func (*PowershellRenderer) RenderScript

func (w *PowershellRenderer) RenderScript(commands []string) []byte

Render implements ScriptWriter.

func (*PowershellRenderer) ScriptFilename

func (pr *PowershellRenderer) ScriptFilename(name, dirname string) string

ScriptFilename implements ScriptWriter.

func (*PowershellRenderer) ScriptPermissions

func (w *PowershellRenderer) ScriptPermissions() os.FileMode

ScriptPermissions implements ScriptWriter.

func (PowershellRenderer) Touch

func (w PowershellRenderer) Touch(path string, timestamp *time.Time) []string

Touch implements Renderer.

func (*PowershellRenderer) WriteFile

func (pr *PowershellRenderer) WriteFile(filename string, data []byte) []string

WriteFile implements Renderer.

type Renderer

type Renderer interface {
	PathRenderer
	CommandRenderer
	OutputRenderer
}

Renderer provides all the functionality needed to generate shell- compatible paths and commands.

func NewRenderer

func NewRenderer(name string) (Renderer, error)

NewRenderer returns a Renderer for the given shell, OS, or distro name.

type ScriptRenderer

type ScriptRenderer interface {
	// RenderScript generates the content of a shell script for the
	// provided shell commands.
	RenderScript(commands []string) []byte
}

A ScriptRenderer provides the functionality necessary to render a sequence of shell commands into the content of a shell script.

type ScriptWriter

type ScriptWriter interface {
	ScriptRenderer

	// Chmod returns a shell command that sets the given file's
	// permissions. The result is equivalent to os.Chmod.
	Chmod(path string, perm os.FileMode) []string

	// WriteFile returns a shell command that writes the provided
	// content to a file. The command is functionally equivalent to
	// ioutil.WriteFile with permissions from the current umask.
	WriteFile(filename string, data []byte) []string

	// ScriptFilename generates a filename appropriate for a script
	// from the provided file and directory names.
	ScriptFilename(name, dirname string) string

	// ScriptPermissions returns the permissions appropriate for a script.
	ScriptPermissions() os.FileMode
}

A ScriptWriter provides the functionality necessarily to render and write a sequence of shell commands to a shell script that is ready to be run.

type WinCmdRenderer

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

WinCmdRenderer is a shell renderer for Windows cmd.exe.

func (*WinCmdRenderer) Chmod

func (wcr *WinCmdRenderer) Chmod(path string, perm os.FileMode) []string

Chmod implements Renderer.

func (WinCmdRenderer) Chown

func (w WinCmdRenderer) Chown(path, owner, group string) []string

Chown implements Renderer.

func (*WinCmdRenderer) ExeSuffix

func (w *WinCmdRenderer) ExeSuffix() string

ExeSuffix implements Renderer.

func (*WinCmdRenderer) Mkdir

func (wcr *WinCmdRenderer) Mkdir(dirname string) []string

MkDir implements Renderer.

func (*WinCmdRenderer) MkdirAll

func (wcr *WinCmdRenderer) MkdirAll(dirname string) []string

MkDirAll implements Renderer.

func (*WinCmdRenderer) Quote

func (wcr *WinCmdRenderer) Quote(str string) string

Quote implements Renderer.

func (WinCmdRenderer) RedirectFD

func (w WinCmdRenderer) RedirectFD(dst, src string) []string

RedirectFD implements OutputRenderer.

func (WinCmdRenderer) RedirectOutput

func (w WinCmdRenderer) RedirectOutput(filename string) []string

RedirectOutput implements OutputRenderer.

func (WinCmdRenderer) RedirectOutputReset

func (w WinCmdRenderer) RedirectOutputReset(filename string) []string

RedirectOutputReset implements OutputRenderer.

func (*WinCmdRenderer) RenderScript

func (w *WinCmdRenderer) RenderScript(commands []string) []byte

Render implements ScriptWriter.

func (*WinCmdRenderer) ScriptFilename

func (wcr *WinCmdRenderer) ScriptFilename(name, dirname string) string

ScriptFilename implements ScriptWriter.

func (*WinCmdRenderer) ScriptPermissions

func (w *WinCmdRenderer) ScriptPermissions() os.FileMode

ScriptPermissions implements ScriptWriter.

func (WinCmdRenderer) Touch

func (w WinCmdRenderer) Touch(path string, timestamp *time.Time) []string

Touch implements Renderer.

func (*WinCmdRenderer) WriteFile

func (wcr *WinCmdRenderer) WriteFile(filename string, data []byte) []string

WriteFile implements Renderer.

Jump to

Keyboard shortcuts

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