1 // Copyright (c) 2015-present Jeevanandam M (jeeva@myjeeva.com)
2 // 2024 Ahuigo (https://github.com/ahuigo)
3 // All rights reserved.
4 // resty source code and usage is governed by a MIT style
5 // license that can be found in the LICENSE file.
6 7 /*
8 Package shellescape provides the methods to escape arbitrary
9 strings for a safe use as command line arguments in the most common
10 POSIX shells.
11 12 The original Python package which this work was inspired by can be found
13 at https://pypi.python.org/pypi/shellescape.
14 */
15 package shellescape
16 17 import (
18 "regexp"
19 "strings"
20 )
21 22 var pattern *regexp.Regexp
23 24 func init() {
25 pattern = regexp.MustCompile(`[^\w@%+=:,./-]`)
26 }
27 28 // Quote method returns a shell-escaped version of the string. The returned value
29 // can safely be used as one token in a shell command line.
30 func Quote(s string) string {
31 if len(s) == 0 {
32 return "''"
33 }
34 35 if pattern.MatchString(s) {
36 return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'"
37 }
38 39 return s
40 }
41