align repl with cli
This commit is contained in:
@@ -21,14 +21,70 @@ func execute(input string) (string, error) {
|
||||
return ReplTitle, nil
|
||||
}
|
||||
|
||||
if input == "status" {
|
||||
return executePMCommand("survey status")
|
||||
}
|
||||
|
||||
if after, ok := strings.CutPrefix(input, "pm"); ok {
|
||||
return executePMCommand(after)
|
||||
}
|
||||
return executeShellCommand(input)
|
||||
}
|
||||
|
||||
// shellSplit splits input respecting quoted strings and escapes
|
||||
// similar to how a shell would parse arguments
|
||||
func shellSplit(input string) []string {
|
||||
var args []string
|
||||
var current strings.Builder
|
||||
var inQuote rune
|
||||
var escaped bool
|
||||
|
||||
for _, ch := range input {
|
||||
if escaped {
|
||||
current.WriteRune(ch)
|
||||
escaped = false
|
||||
continue
|
||||
}
|
||||
|
||||
if ch == '\\' {
|
||||
escaped = true
|
||||
continue
|
||||
}
|
||||
|
||||
if inQuote != 0 {
|
||||
if ch == inQuote {
|
||||
inQuote = 0
|
||||
continue
|
||||
}
|
||||
current.WriteRune(ch)
|
||||
continue
|
||||
}
|
||||
|
||||
if ch == '"' || ch == '\'' {
|
||||
inQuote = ch
|
||||
continue
|
||||
}
|
||||
|
||||
if ch == ' ' || ch == '\t' {
|
||||
if current.Len() > 0 {
|
||||
args = append(args, current.String())
|
||||
current.Reset()
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
current.WriteRune(ch)
|
||||
}
|
||||
|
||||
if current.Len() > 0 {
|
||||
args = append(args, current.String())
|
||||
}
|
||||
|
||||
return args
|
||||
}
|
||||
|
||||
func executeShellCommand(input string) (string, error) {
|
||||
parts := strings.Fields(input)
|
||||
parts := shellSplit(input)
|
||||
if len(parts) == 0 {
|
||||
return "", nil
|
||||
}
|
||||
@@ -39,7 +95,7 @@ func executeShellCommand(input string) (string, error) {
|
||||
}
|
||||
|
||||
func executePMCommand(input string) (string, error) {
|
||||
parts := strings.Fields(input)
|
||||
parts := shellSplit(input)
|
||||
if len(parts) == 0 {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user