mirror of
https://gitee.com/goploy/goploy.git
synced 2024-12-04 21:21:40 +08:00
93 lines
1.5 KiB
Go
93 lines
1.5 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// GetScriptExt return script extension default bash
|
|
func GetScriptExt(scriptMode string) string {
|
|
switch scriptMode {
|
|
case "sh", "zsh", "bash":
|
|
return "sh"
|
|
case "php":
|
|
return "php"
|
|
case "python":
|
|
return "py"
|
|
case "cmd":
|
|
return "bat"
|
|
default:
|
|
return "sh"
|
|
}
|
|
}
|
|
|
|
// ParseCommandLine parse cmd arg
|
|
func ParseCommandLine(command string) ([]string, error) {
|
|
var args []string
|
|
state := "start"
|
|
current := ""
|
|
quote := "\""
|
|
escapeNext := true
|
|
for i := 0; i < len(command); i++ {
|
|
c := command[i]
|
|
|
|
if state == "quotes" {
|
|
if string(c) != quote {
|
|
current += string(c)
|
|
} else {
|
|
args = append(args, current)
|
|
current = ""
|
|
state = "start"
|
|
}
|
|
continue
|
|
}
|
|
|
|
if escapeNext {
|
|
current += string(c)
|
|
escapeNext = false
|
|
continue
|
|
}
|
|
|
|
if c == '\\' {
|
|
escapeNext = true
|
|
continue
|
|
}
|
|
|
|
if c == '"' {
|
|
state = "quotes"
|
|
quote = string(c)
|
|
continue
|
|
}
|
|
|
|
if state == "arg" {
|
|
if c == ' ' || c == '=' || c == '\t' {
|
|
args = append(args, current)
|
|
current = ""
|
|
state = "start"
|
|
} else {
|
|
current += string(c)
|
|
}
|
|
continue
|
|
}
|
|
|
|
if c != ' ' && c != '=' && c != '\t' {
|
|
state = "arg"
|
|
current += string(c)
|
|
}
|
|
}
|
|
|
|
if state == "quotes" {
|
|
return []string{}, fmt.Errorf("unclosed quote in command line: %s", command)
|
|
}
|
|
|
|
if current != "" {
|
|
args = append(args, current)
|
|
}
|
|
|
|
return args, nil
|
|
}
|
|
|
|
func ClearNewline(str string) string {
|
|
return strings.TrimRight(strings.Replace(str, "\r\n", "\n", -1), "\n")
|
|
}
|