utils: github.com/juju/utils/shell Index | Files

package shell

import "github.com/juju/utils/shell"

Index

Package Files

bash.go command.go output.go powershell.go renderer.go script.go unix.go win.go wincmd.go

func DumpFileOnErrorScript Uses

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 Uses

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 Uses

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 Uses

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).

type BashRenderer Uses

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

BashRenderer is the shell renderer for bash.

func (BashRenderer) Chmod Uses

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

Chmod implements Renderer.

func (BashRenderer) Chown Uses

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

Chown implements Renderer.

func (BashRenderer) ExeSuffix Uses

func (BashRenderer) ExeSuffix() string

ExeSuffix implements Renderer.

func (BashRenderer) Mkdir Uses

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

Mkdir implements Renderer.

func (BashRenderer) MkdirAll Uses

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

MkdirAll implements Renderer.

func (BashRenderer) Quote Uses

func (BashRenderer) Quote(str string) string

Quote implements Renderer.

func (BashRenderer) RedirectFD Uses

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

RedirectFD implements OutputRenderer.

func (BashRenderer) RedirectOutput Uses

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

RedirectOutput implements OutputRenderer.

func (BashRenderer) RedirectOutputReset Uses

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

RedirectOutputReset implements OutputRenderer.

func (*BashRenderer) RenderScript Uses

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

Render implements ScriptWriter.

func (*BashRenderer) ScriptFilename Uses

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

ScriptFilename implements ScriptWriter.

func (*BashRenderer) ScriptPermissions Uses

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

ScriptPermissions implements ScriptWriter.

func (BashRenderer) Touch Uses

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

Touch implements Renderer.

func (BashRenderer) WriteFile Uses

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

WriteFile implements Renderer.

type CommandRenderer Uses

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 Uses

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 Uses

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 Uses

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

PowershellRenderer is a shell renderer for Windows Powershell.

func (*PowershellRenderer) Chmod Uses

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

Chmod implements Renderer.

func (PowershellRenderer) Chown Uses

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

Chown implements Renderer.

func (*PowershellRenderer) ExeSuffix Uses

func (w *PowershellRenderer) ExeSuffix() string

ExeSuffix implements Renderer.

func (*PowershellRenderer) Mkdir Uses

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

MkDir implements Renderer.

func (*PowershellRenderer) MkdirAll Uses

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

MkdirAll implements Renderer.

func (*PowershellRenderer) Quote Uses

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

Quote implements Renderer.

func (PowershellRenderer) RedirectFD Uses

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

RedirectFD implements OutputRenderer.

func (PowershellRenderer) RedirectOutput Uses

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

RedirectOutput implements OutputRenderer.

func (PowershellRenderer) RedirectOutputReset Uses

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

RedirectOutputReset implements OutputRenderer.

func (*PowershellRenderer) RenderScript Uses

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

Render implements ScriptWriter.

func (*PowershellRenderer) ScriptFilename Uses

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

ScriptFilename implements ScriptWriter.

func (*PowershellRenderer) ScriptPermissions Uses

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

ScriptPermissions implements ScriptWriter.

func (PowershellRenderer) Touch Uses

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

Touch implements Renderer.

func (*PowershellRenderer) WriteFile Uses

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

WriteFile implements Renderer.

type Renderer Uses

type Renderer interface {
    PathRenderer
    CommandRenderer
    OutputRenderer
}

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

func NewRenderer Uses

func NewRenderer(name string) (Renderer, error)

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

type ScriptRenderer Uses

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 Uses

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 Uses

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

WinCmdRenderer is a shell renderer for Windows cmd.exe.

func (*WinCmdRenderer) Chmod Uses

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

Chmod implements Renderer.

func (WinCmdRenderer) Chown Uses

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

Chown implements Renderer.

func (*WinCmdRenderer) ExeSuffix Uses

func (w *WinCmdRenderer) ExeSuffix() string

ExeSuffix implements Renderer.

func (*WinCmdRenderer) Mkdir Uses

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

MkDir implements Renderer.

func (*WinCmdRenderer) MkdirAll Uses

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

MkDirAll implements Renderer.

func (*WinCmdRenderer) Quote Uses

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

Quote implements Renderer.

func (WinCmdRenderer) RedirectFD Uses

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

RedirectFD implements OutputRenderer.

func (WinCmdRenderer) RedirectOutput Uses

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

RedirectOutput implements OutputRenderer.

func (WinCmdRenderer) RedirectOutputReset Uses

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

RedirectOutputReset implements OutputRenderer.

func (*WinCmdRenderer) RenderScript Uses

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

Render implements ScriptWriter.

func (*WinCmdRenderer) ScriptFilename Uses

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

ScriptFilename implements ScriptWriter.

func (*WinCmdRenderer) ScriptPermissions Uses

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

ScriptPermissions implements ScriptWriter.

func (WinCmdRenderer) Touch Uses

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

Touch implements Renderer.

func (*WinCmdRenderer) WriteFile Uses

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

WriteFile implements Renderer.

Package shell imports 12 packages (graph) and is imported by 444 packages. Updated 2017-03-22. Refresh now. Tools for package owners.