~ubuntu-branches/ubuntu/trusty/juju-core/trusty-proposed

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/utils/trivial.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-29 11:40:20 UTC
  • mfrom: (23.1.1 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20140129114020-ejieitm8smtt5vln
Tags: 1.17.1-0ubuntu2
d/tests/local-provider: Don't fail tests if ~/.juju is present as its
created by the juju version command. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
        "io/ioutil"
12
12
        "os"
13
13
        "strings"
 
14
        "unicode"
14
15
 
15
16
        "launchpad.net/goyaml"
16
17
)
61
62
        return `'` + strings.Replace(s, `'`, `'"'"'`, -1) + `'`
62
63
}
63
64
 
 
65
// CommandString flattens a sequence of command arguments into a
 
66
// string suitable for executing in a shell, escaping slashes,
 
67
// variables and quotes as necessary; each argument is double-quoted
 
68
// if and only if necessary.
 
69
func CommandString(args ...string) string {
 
70
        var buf bytes.Buffer
 
71
        for i, arg := range args {
 
72
                needsQuotes := false
 
73
                var argBuf bytes.Buffer
 
74
                for _, r := range arg {
 
75
                        if unicode.IsSpace(r) {
 
76
                                needsQuotes = true
 
77
                        } else if r == '"' || r == '$' || r == '\\' {
 
78
                                needsQuotes = true
 
79
                                argBuf.WriteByte('\\')
 
80
                        }
 
81
                        argBuf.WriteRune(r)
 
82
                }
 
83
                if i > 0 {
 
84
                        buf.WriteByte(' ')
 
85
                }
 
86
                if needsQuotes {
 
87
                        buf.WriteByte('"')
 
88
                        argBuf.WriteTo(&buf)
 
89
                        buf.WriteByte('"')
 
90
                } else {
 
91
                        argBuf.WriteTo(&buf)
 
92
                }
 
93
        }
 
94
        return buf.String()
 
95
}
 
96
 
64
97
// Gzip compresses the given data.
65
98
func Gzip(data []byte) []byte {
66
99
        var buf bytes.Buffer