1
// Copyright 2015 Canonical Ltd.
2
// Licensed under the LGPLv3, see LICENCE file for details.
11
// OutputRenderer exposes the Renderer methods that relate to shell output.
13
// The methods all accept strings to identify their file descriptor
14
// arguments. While the interpretation of these values is up to the
15
// renderer, it will likely conform to the result of calling ResolveFD.
16
// If an FD arg is not recognized then no commands will be returned.
17
// Unless otherwise specified, the default file descriptor is stdout
18
// (FD 1). This applies to the empty string.
19
type OutputRenderer interface {
20
// RedirectFD returns a shell command that redirects the src
21
// file descriptor to the dst one.
22
RedirectFD(dst, src string) []string
24
// TODO(ericsnow) Add CopyFD and CreateFD?
26
// TODO(ericsnow) Support passing the src FD as an arg?
28
// RedirectOutput will cause all subsequent output from the shell
29
// (or script) to go be appended to the given file. Only stdout is
30
// redirected (use RedirectFD to redirect stderr or other FDs).
32
// The file should already exist (so a call to Touch may be
33
// necessary before calling RedirectOutput). If the file should have
34
// specific permissions or a specific owner then Chmod and Chown
35
// should be called before calling RedirectOutput.
36
RedirectOutput(filename string) []string
38
// RedirectOutputReset will cause all subsequent output from the
39
// shell (or script) to go be written to the given file. The file
40
// will be reset (truncated to 0) before anything is written. Only
41
// stdout is redirected (use RedirectFD to redirect stderr or
44
// The file should already exist (so a call to Touch may be
45
// necessary before calling RedirectOutputReset). If the file should
46
// have specific permissions or a specific owner then Chmod and
47
// Chown should be called before calling RedirectOutputReset.
48
RedirectOutputReset(filename string) []string
51
// ResolveFD converts the file descriptor name to the corresponding int.
52
// "stdout" and "out" match stdout (FD 1). "stderr" and "err" match
53
// stderr (FD 2), "stdin" and "in" match stdin (FD 0). All positive
54
// integers match. If there should be an upper bound then the caller
55
// should check it on the result. If the provided name is empty then
56
// the result defaults to stdout. If the name is not recognized then
58
func ResolveFD(name string) (int, bool) {
59
switch strings.ToLower(name) {
60
case "stdout", "out", "":
67
fd, err := strconv.ParseUint(name, 10, 64)