~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/utils/shell/output.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2015 Canonical Ltd.
 
2
// Licensed under the LGPLv3, see LICENCE file for details.
 
3
 
 
4
package shell
 
5
 
 
6
import (
 
7
        "strconv"
 
8
        "strings"
 
9
)
 
10
 
 
11
// OutputRenderer exposes the Renderer methods that relate to shell output.
 
12
//
 
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
 
23
 
 
24
        // TODO(ericsnow) Add CopyFD and CreateFD?
 
25
 
 
26
        // TODO(ericsnow) Support passing the src FD as an arg?
 
27
 
 
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).
 
31
        //
 
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
 
37
 
 
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
 
42
        // other FDs).
 
43
        //
 
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
 
49
}
 
50
 
 
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
 
57
// false is returned.
 
58
func ResolveFD(name string) (int, bool) {
 
59
        switch strings.ToLower(name) {
 
60
        case "stdout", "out", "":
 
61
                return 1, true
 
62
        case "stderr", "err":
 
63
                return 2, true
 
64
        case "stdin", "in":
 
65
                return 0, true
 
66
        default:
 
67
                fd, err := strconv.ParseUint(name, 10, 64)
 
68
                if err != nil {
 
69
                        return -1, false
 
70
                }
 
71
                return int(fd), true
 
72
        }
 
73
}