1
// Copyright 2012, 2013 Canonical Ltd.
2
// Licensed under the LGPLv3, see LICENCE file for details.
18
// TODO(ericsnow) Move the quoting helpers into the shell package?
20
// ShQuote quotes s so that when read by bash, no metacharacters
21
// within s will be interpreted as such.
22
func ShQuote(s string) string {
23
// single-quote becomes single-quote, double-quote, single-quote, double-quote, single-quote
24
return `'` + strings.Replace(s, `'`, `'"'"'`, -1) + `'`
27
// WinPSQuote quotes s so that when read by powershell, no metacharacters
28
// within s will be interpreted as such.
29
func WinPSQuote(s string) string {
30
// See http://ss64.com/ps/syntax-esc.html#quotes.
31
// Double quotes inside single quotes are fine, double single quotes inside
32
// single quotes, not so much so. Having double quoted strings inside single
33
// quoted strings, ensure no expansion happens.
34
return `'` + strings.Replace(s, `'`, `"`, -1) + `'`
37
// WinCmdQuote quotes s so that when read by cmd.exe, no metacharacters
38
// within s will be interpreted as such.
39
func WinCmdQuote(s string) string {
40
// See http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/everyone-quotes-arguments-the-wrong-way.aspx.
41
quoted := winCmdQuote(s)
42
return winCmdEscapeMeta(quoted)
45
func winCmdQuote(s string) string {
54
return `"` + escaped + `"`
57
func winCmdEscapeMeta(str string) string {
58
const meta = `()%!^"<>&|`
60
for _, c := range str {
61
if strings.Contains(meta, string(c)) {
69
// CommandString flattens a sequence of command arguments into a
70
// string suitable for executing in a shell, escaping slashes,
71
// variables and quotes as necessary; each argument is double-quoted
72
// if and only if necessary.
73
func CommandString(args ...string) string {
75
for i, arg := range args {
77
var argBuf bytes.Buffer
78
for _, r := range arg {
79
if unicode.IsSpace(r) {
81
} else if r == '"' || r == '$' || r == '\\' {
83
argBuf.WriteByte('\\')
101
// Gzip compresses the given data.
102
func Gzip(data []byte) []byte {
104
w := gzip.NewWriter(&buf)
105
if _, err := w.Write(data); err != nil {
106
// Compression should never fail unless it fails
107
// to write to the underlying writer, which is a bytes.Buffer
111
if err := w.Close(); err != nil {
117
// Gunzip uncompresses the given data.
118
func Gunzip(data []byte) ([]byte, error) {
119
r, err := gzip.NewReader(bytes.NewReader(data))
123
return ioutil.ReadAll(r)
126
// ReadSHA256 returns the SHA256 hash of the contents read from source
127
// (hex encoded) and the size of the source in bytes.
128
func ReadSHA256(source io.Reader) (string, int64, error) {
130
size, err := io.Copy(hash, source)
134
digest := hex.EncodeToString(hash.Sum(nil))
135
return digest, size, nil
138
// ReadFileSHA256 is like ReadSHA256 but reads the contents of the
140
func ReadFileSHA256(filename string) (string, int64, error) {
141
f, err := os.Open(filename)