Files
QSfera/vendor/github.com/gookit/goutil/internal/comfunc/cli_func.go
T
dependabot[bot]andGitHub 89a7d171ee build(deps): bump github.com/gookit/config/v2 from 2.2.6 to 2.2.7
Bumps [github.com/gookit/config/v2](https://github.com/gookit/config) from 2.2.6 to 2.2.7.
- [Release notes](https://github.com/gookit/config/releases)
- [Commits](https://github.com/gookit/config/compare/v2.2.6...v2.2.7)

---
updated-dependencies:
- dependency-name: github.com/gookit/config/v2
  dependency-version: 2.2.7
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-08-15 14:15:33 +00:00

94 lines
1.7 KiB
Go

package comfunc
import (
"fmt"
"strings"
)
// Cmdline build
func Cmdline(args []string, binName ...string) string {
b := new(strings.Builder)
if len(binName) > 0 {
b.WriteString(binName[0])
b.WriteByte(' ')
}
for i, a := range args {
if i > 0 {
b.WriteByte(' ')
}
if strings.ContainsRune(a, '"') {
b.WriteString(fmt.Sprintf(`'%s'`, a))
} else if a == "" || strings.ContainsRune(a, '\'') || strings.ContainsRune(a, ' ') {
b.WriteString(fmt.Sprintf(`"%s"`, a))
} else {
b.WriteString(a)
}
}
return b.String()
}
// ShellQuote quote a string on contains ', ", SPACE. refer strconv.Quote()
func ShellQuote(a string) string {
if a == "" {
return `""`
}
// use quote char
var quote byte
// has double quote
if pos := strings.IndexByte(a, '"'); pos > -1 {
if !checkNeedQuote(a, pos, '"') {
return a
}
quote = '\''
} else if pos := strings.IndexByte(a, '\''); pos > -1 {
// single quote
if !checkNeedQuote(a, pos, '\'') {
return a
}
quote = '"'
} else if strings.IndexByte(a, ' ') > -1 {
quote = '"'
}
// no quote char OR not need quote
if quote == 0 {
return a
}
return fmt.Sprintf("%c%s%c", quote, a, quote)
}
func checkNeedQuote(a string, pos int, char byte) bool {
// end with char. eg: "
lastIsQ := a[len(a)-1] == char
// start with char. eg: "
if pos == 0 {
if lastIsQ {
return false
}
if pos1 := strings.IndexByte(a[pos+1:], char); pos1 > -1 {
// eg: `"one two" three four`
lastS := a[pos1+pos+1:]
if !strings.ContainsRune(lastS, ' ') {
return false
}
}
} else {
startS := a[:pos]
// eg: `--one="two three"`
if lastIsQ && strings.IndexByte(startS, ' ') == -1 {
return false
}
}
return true
}