diff --git a/backend/api/commands_controller.go b/backend/api/commands_controller.go index 10a61dc..3ca210a 100644 --- a/backend/api/commands_controller.go +++ b/backend/api/commands_controller.go @@ -4,7 +4,7 @@ import ( "context" "fmt" "net/http" - "strings" + "net/url" "github.com/localhots/cmdui/backend/api/auth" "github.com/localhots/cmdui/backend/commands" @@ -61,18 +61,13 @@ func commandFromRequest(r *http.Request) (commands.Command, error) { return cmd, fmt.Errorf("Unknown command: %q", r.PostForm.Get("command")) } - for key, val := range r.PostForm { - if key == "args" { - cmd.Args = val[0] - } else if strings.HasPrefix(key, "flags[") { - name := key[6 : len(key)-1] - for i, f := range cmd.Flags { - if f.Name == name { - cmd.Flags[i].Value = val[0] - } - } - } - } - - return cmd, nil + return buildCommand(cmd, r.PostForm), nil +} + +func buildCommand(cmd commands.Command, p url.Values) commands.Command { + cmd.Args = p.Get("args") + for i, f := range cmd.Flags { + cmd.Flags[i].Value = p.Get(fmt.Sprintf("flags[%s]", f.Name)) + } + return cmd }