-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshellquote.go
44 lines (38 loc) · 1.24 KB
/
shellquote.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Benjamin Zeller <[email protected]>
*
* Ported from shellescape Python library: https://github.com/chrissimpkins/shellescape
* Author: Chris Simpkins
*/
package ubuntu_sdk_tools
import (
"strings"
"regexp"
)
var _find_unsafe = regexp.MustCompile("[^\\w@%+=:,./-]")
func QuoteString (s string) string{
//Return a shell-escaped version of the string *s*.
if len(s) == 0 {
return "''"
}
if !_find_unsafe.MatchString(s) {
return s
}
// use single quotes, and put single quotes into double quotes
// the string $'b is then quoted as '$'"'"'b'
return "'" + strings.Replace(s, "'", "'\"'\"'", -1) + "'"
}