From 3bd6a0ff4e2c4be925a61c83e84f917f4e1e60f0 Mon Sep 17 00:00:00 2001 From: "Colin T.A. Gray" Date: Thu, 20 Jul 2017 15:20:05 -0600 Subject: [PATCH] bind 'ctrl+f' to 'loadRequest' command, document it and -f/--file option --- README.md | 2 ++ commands.go | 22 +++++++++++++++ config/config.go | 1 + sample-config.toml | 1 + wuzz.go | 70 ++++++++++++++++++++++++++-------------------- 5 files changed, 66 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 2a84e76..2c65d1c 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,8 @@ Keybinding | Description Ctrl+R | Send request Ret | Send request (only from URL view) Ctrl+S | Save response +Ctrl+E | Save request +Ctrl+F | Load request Ctrl+C | Quit Ctrl+K, Shift+Tab | Previous view Ctlr+J, Tab | Next view diff --git a/commands.go b/commands.go index 2317ec4..1f53840 100644 --- a/commands.go +++ b/commands.go @@ -46,6 +46,28 @@ var COMMANDS map[string]func(string, *App) CommandFunc = map[string]func(string, }) } }, + "loadRequest": func(_ string, a *App) CommandFunc { + return func(g *gocui.Gui, _ *gocui.View) error { + return a.OpenSaveDialog(VIEW_TITLES[LOAD_REQUEST_DIALOG_VIEW], g, + func(g *gocui.Gui, _ *gocui.View) error { + defer a.closePopup(g, SAVE_DIALOG_VIEW) + loadLocation := getViewValue(g, SAVE_DIALOG_VIEW) + + requestJson, ioErr := ioutil.ReadFile(loadLocation) + if ioErr != nil { + return ioErr + } + + var requestMap map[string]string + jsonErr := json.Unmarshal(requestJson, &requestMap) + if jsonErr != nil { + return jsonErr + } + + return a.LoadRequest(g, requestMap) + }) + } + }, "saveRequest": func(_ string, a *App) CommandFunc { return func(g *gocui.Gui, _ *gocui.View) error { return a.OpenSaveDialog(VIEW_TITLES[SAVE_REQUEST_DIALOG_VIEW], g, diff --git a/config/config.go b/config/config.go index 2088bcb..902f294 100644 --- a/config/config.go +++ b/config/config.go @@ -55,6 +55,7 @@ var DefaultKeys = map[string]map[string]string{ "CtrlR": "submit", "CtrlC": "quit", "CtrlS": "saveResponse", + "CtrlF": "loadRequest", "CtrlE": "saveRequest", "CtrlD": "deleteLine", "CtrlW": "deleteWord", diff --git a/sample-config.toml b/sample-config.toml index 55d4303..9c173d2 100644 --- a/sample-config.toml +++ b/sample-config.toml @@ -15,6 +15,7 @@ CtrlC = "quit" CtrlS = "saveResponse" CtrlD = "deleteLine" CtrlW = "deleteWord" +CtrlF = "loadRequest" CtrlE = "saveRequest" CtrlT = "toggleContextSpecificSearch" CtrlX = "clearHistory" diff --git a/wuzz.go b/wuzz.go index 7f0241d..b57c94d 100644 --- a/wuzz.go +++ b/wuzz.go @@ -58,6 +58,7 @@ const ( HISTORY_VIEW = "history" SAVE_DIALOG_VIEW = "save-dialog" SAVE_RESPONSE_DIALOG_VIEW = "save-response-dialog" + LOAD_REQUEST_DIALOG_VIEW = "load-request-dialog" SAVE_REQUEST_DIALOG_VIEW = "save-request-dialog" SAVE_RESULT_VIEW = "save-result" METHOD_LIST_VIEW = "method-list" @@ -69,6 +70,7 @@ var VIEW_TITLES = map[string]string{ ERROR_VIEW: "Error", HISTORY_VIEW: "History", SAVE_RESPONSE_DIALOG_VIEW: "Save Response (enter to submit, ctrl+q to cancel)", + LOAD_REQUEST_DIALOG_VIEW: "Load Request (enter to submit, ctrl+q to cancel)", SAVE_REQUEST_DIALOG_VIEW: "Save Request (enter to submit, ctrl+q to cancel)", SAVE_RESULT_VIEW: "Save Result (press enter to close)", METHOD_LIST_VIEW: "Methods", @@ -1169,6 +1171,40 @@ func (a *App) CreatePopupView(name string, width, height int, g *gocui.Gui) (v * return } +func (a *App) LoadRequest(g *gocui.Gui, requestMap map[string]string) (err error) { + var v *gocui.View + url, exists := requestMap[URL_VIEW] + if exists { + v, _ = g.View(URL_VIEW) + setViewTextAndCursor(v, url) + } + + method, exists := requestMap[REQUEST_METHOD_VIEW] + if exists { + v, _ = g.View(REQUEST_METHOD_VIEW) + setViewTextAndCursor(v, method) + } + + params, exists := requestMap[URL_PARAMS_VIEW] + if exists { + v, _ = g.View(URL_PARAMS_VIEW) + setViewTextAndCursor(v, params) + } + + data, exists := requestMap[REQUEST_DATA_VIEW] + if exists { + v, _ = g.View(REQUEST_DATA_VIEW) + setViewTextAndCursor(v, data) + } + + headers, exists := requestMap[REQUEST_HEADERS_VIEW] + if exists { + v, _ = g.View(REQUEST_HEADERS_VIEW) + setViewTextAndCursor(v, headers) + } + return nil +} + func (a *App) ToggleHistory(g *gocui.Gui, _ *gocui.View) (err error) { // Destroy if present if a.currentPopup == HISTORY_VIEW { @@ -1521,36 +1557,7 @@ func (a *App) ParseArgs(g *gocui.Gui, args []string) error { return jsonErr } - var v *gocui.View - url, exists := requestMap[URL_VIEW] - if exists { - v, _ = g.View(URL_VIEW) - setViewTextAndCursor(v, url) - } - - method, exists := requestMap[REQUEST_METHOD_VIEW] - if exists { - v, _ = g.View(REQUEST_METHOD_VIEW) - setViewTextAndCursor(v, method) - } - - params, exists := requestMap[URL_PARAMS_VIEW] - if exists { - v, _ = g.View(URL_PARAMS_VIEW) - setViewTextAndCursor(v, params) - } - - data, exists := requestMap[REQUEST_DATA_VIEW] - if exists { - v, _ = g.View(REQUEST_DATA_VIEW) - setViewTextAndCursor(v, data) - } - - headers, exists := requestMap[REQUEST_HEADERS_VIEW] - if exists { - v, _ = g.View(REQUEST_HEADERS_VIEW) - setViewTextAndCursor(v, headers) - } + a.LoadRequest(g, requestMap) default: u := args[arg_index] if strings.Index(u, "http://") != 0 && strings.Index(u, "https://") != 0 { @@ -1665,6 +1672,7 @@ Usage: wuzz [-H|--header HEADER]... [-d|--data|--data-binary DATA] [-X|--request Other command line options: -c, --config PATH Specify custom configuration file -e, --editor EDITOR Specify external editor command + -f, --file REQUEST Load a previous request -F, --form DATA Add multipart form request data and set related request headers If the value starts with @ it will be handled as a file path for upload -h, --help Show this @@ -1684,6 +1692,8 @@ Other command line options: Key bindings: ctrl+r Send request ctrl+s Save response + ctrl+e Save request + ctrl+f Load request tab, ctrl+j Next window shift+tab, ctrl+k Previous window alt+h Show history