1/*
2Package shellescape provides the shellescape.Quote to escape arbitrary
3strings for a safe use as command line arguments in the most common
4POSIX shells.
5
6The original Python package which this work was inspired by can be found
7at https://pypi.python.org/pypi/shellescape.
8*/
9package shellescape
10
11/*
12The functionality provided by shellescape.Quote could be helpful
13in those cases where it is known that the output of a Go program will
14be appended to/used in the context of shell programs' command line arguments.
15*/
16
17import (
18	"regexp"
19	"strings"
20)
21
22var pattern *regexp.Regexp
23
24func init() {
25	pattern = regexp.MustCompile(`[^\w@%+=:,./-]`)
26}
27
28// Quote returns a shell-escaped version of the string s. The returned value
29// is a string that can safely be used as one token in a shell command line.
30func Quote(s string) string {
31	if len(s) == 0 {
32		return "''"
33	}
34	if pattern.MatchString(s) {
35		return "'" + strings.Replace(s, "'", "'\"'\"'", -1) + "'"
36	}
37
38	return s
39}
40