[#1332] cli/playground: Move command handler selection to separate function
All checks were successful
Pre-commit hooks / Pre-commit (push) Successful in 1m33s
Build / Build Components (push) Successful in 1m56s
Vulncheck / Vulncheck (push) Successful in 1m55s
Tests and linters / Run gofumpt (push) Successful in 3m19s
Tests and linters / Tests (push) Successful in 3m33s
Tests and linters / Staticcheck (push) Successful in 3m34s
OCI image / Build container images (push) Successful in 4m24s
Tests and linters / gopls check (push) Successful in 4m20s
Tests and linters / Lint (push) Successful in 4m32s
Tests and linters / Tests with -race (push) Successful in 4m34s

Change-Id: I2dcbd85e61960c3cf141b815edab174e308ef858
Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
This commit is contained in:
Alexander Chuprov 2025-04-09 16:27:03 +03:00 committed by Aleksandr Chuprov
parent 8e87cbee17
commit 0d36e93169

View file

@ -260,6 +260,28 @@ Example of usage:
},
}
func (repl *policyPlaygroundREPL) handleCommand(args []string) error {
if len(args) == 0 {
return nil
}
switch args[0] {
case "list", "ls":
return repl.handleLs(args[1:])
case "add":
return repl.handleAdd(args[1:])
case "load":
return repl.handleLoad(args[1:])
case "remove", "rm":
return repl.handleRemove(args[1:])
case "eval":
return repl.handleEval(args[1:])
case "help":
return repl.handleHelp(args[1:])
}
return fmt.Errorf("unknown command %q", args[0])
}
func (repl *policyPlaygroundREPL) run() error {
if len(viper.GetString(commonflags.RPC)) > 0 {
key := key.GetOrGenerate(repl.cmd)
@ -277,17 +299,6 @@ func (repl *policyPlaygroundREPL) run() error {
}
}
cmdHandlers := map[string]func([]string) error{
"list": repl.handleLs,
"ls": repl.handleLs,
"add": repl.handleAdd,
"load": repl.handleLoad,
"remove": repl.handleRemove,
"rm": repl.handleRemove,
"eval": repl.handleEval,
"help": repl.handleHelp,
}
var cfgCompleter []readline.PrefixCompleterInterface
var helpSubItems []readline.PrefixCompleterInterface
@ -326,17 +337,8 @@ func (repl *policyPlaygroundREPL) run() error {
}
exit = false
parts := strings.Fields(line)
if len(parts) == 0 {
continue
}
cmd := parts[0]
if handler, exists := cmdHandlers[cmd]; exists {
if err := handler(parts[1:]); err != nil {
fmt.Fprintf(repl.console, "error: %v\n", err)
}
} else {
fmt.Fprintf(repl.console, "error: unknown command %q\n", cmd)
if err := repl.handleCommand(strings.Fields(line)); err != nil {
fmt.Fprintf(repl.console, "error: %v\n", err)
}
}
}