/
wax_boy
/
semaphore_custom
Обзор
Документация
Войти
/
wax_boy
/
semaphore_custom
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
util/shell.go
43 строки
972 B
Rémi Ferrand
feat: Quote and escape shell vars
16 фев 2026, 13:20
Не верифицирован
16 фев 2026, 13:20
de0773f
Код
Авторство
О чём код?
package util import ( "regexp" "strings" "unicode" ) // Imported from https://github.com/alessio/shellescape/blob/master/shellescape.go // Credits goes to https://github.com/alessio/shellescape maintainers var shellQuotePattern *regexp.Regexp func init() { shellQuotePattern = regexp.MustCompile(`[^\w@%+=:,./-]`) } // Quote returns a shell-escaped version of the string s. The returned value // is a string that can safely be used as one token in a shell command line. func ShellQuote(s string) string { if len(s) == 0 { return "''" } if shellQuotePattern.MatchString(s) { return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'" } return s } // StripUnsafe remove non-printable runes, e.g. control characters in // a string that is meant for consumption by terminals that support // control characters. func ShellStripUnsafe(s string) string { return strings.Map(func(r rune) rune { if unicode.IsPrint(r) { return r } return -1 }, s) }