From c51050b4e3a115d0aa2bb070ab8537990f4b522f Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Thu, 28 Oct 2021 10:40:21 +0200 Subject: [PATCH] kivi.js: Helfsfunktion zum Formatieren eines JS-Calls als CURL-Aufruf MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Nützlich, wenn man mehrfach von der Kommandozeile aus eine Controller-Funktion aufrufen will, ohne jedes Mal zur Maus greifen zu müssen. Aufruf grob so: var data = $('#form').serializeArray(); data.push({ name: 'action', value: 'Order/frobnicate' }); console.log(kivi.call_as_curl({ data: data })); --- js/kivi.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/js/kivi.js b/js/kivi.js index c6245c855..7fb250149 100644 --- a/js/kivi.js +++ b/js/kivi.js @@ -676,6 +676,29 @@ namespace("kivi", function(ns) { $input.prop('selectionStart', position); $input.prop('selectionEnd', position); }; + + ns._shell_escape = function(str) { + if (str.match(/^[a-zA-Z0-9.,_=+/-]+$/)) + return str; + + return "'" + str.replace(/'/, "'\\''") + "'"; + }; + + ns.call_as_curl = function(params) { + params = params || {}; + var uri = document.documentURI.replace(/\?.*/, ''); + var command = ['curl', '--user', kivi.myconfig.login + ':SECRET', '--request', params.method || 'POST'] + + $(params.data || []).each(function(idx, elt) { + command = command.concat([ '--form-string', elt.name + '=' + (elt.value || '') ]); + }); + + command.push(uri); + + return $.map(command, function(elt, idx) { + return kivi._shell_escape(elt); + }).join(' '); + }; }); kivi = namespace('kivi'); -- 2.20.1