b5909396370db43ba1c6c2513b1745991e5f60d0

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

refactor(jsontools): move to functions, align `is_json` tool to return exit code

Also:

- Fix Python code in accordance with Python 3
- Use CGI module in Ruby to encode/decode strings

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/jsontools/jsontools.plugin.zsh b/plugins/jsontools/jsontools.plugin.zsh
  2index f92d336daecdd6ad3a0bf0f56dc6335943537f73..b518288ad65ac1ef168ba8f744d9b58809decf4e 100644
  3--- a/plugins/jsontools/jsontools.plugin.zsh
  4+++ b/plugins/jsontools/jsontools.plugin.zsh
  5@@ -22,39 +22,83 @@ fi
  6 # Define json tools for each method
  7 case "$JSONTOOLS_METHOD" in
  8   node)
  9-    alias pp_json='xargs -0 node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 4));"'
 10-    alias is_json='xargs -0 node -e "try {json = JSON.parse(process.argv[1]);} catch (e) { console.log(false); json = null; } if(json) { console.log(true); }"'
 11-    alias urlencode_json='xargs -0 node -e "console.log(encodeURIComponent(process.argv[1]))"'
 12-    alias urldecode_json='xargs -0 node -e "console.log(decodeURIComponent(process.argv[1]))"'
 13+    # node doesn't make it easy to deal with stdin, so we pass it as an argument with xargs -0
 14+    function pp_json() {
 15+      xargs -0 node -e 'console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 4));'
 16+    }
 17+    function is_json() {
 18+      xargs -0 node -e '
 19+        try {
 20+          json = JSON.parse(process.argv[1]);
 21+          console.log("true");
 22+          process.exit(0);
 23+        } catch (e) {
 24+          console.log("false");
 25+          process.exit(1);
 26+        }
 27+      '
 28+    }
 29+    function urlencode_json() {
 30+      xargs -0 node -e "console.log(encodeURIComponent(process.argv[1]))"
 31+    }
 32+    function urldecode_json() {
 33+      xargs -0 node -e "console.log(decodeURIComponent(process.argv[1]))"
 34+    }
 35   ;;
 36   python)
 37-    alias pp_json='python -c "import sys; del sys.path[0]; import runpy; runpy._run_module_as_main(\"json.tool\")"'
 38-    alias is_json='python -c "
 39+    function pp_json() {
 40+      python -c 'import sys; del sys.path[0]; import runpy; runpy._run_module_as_main("json.tool")'
 41+    }
 42+    function is_json() {
 43+      python -c '
 44 import sys; del sys.path[0];
 45-import json;
 46-try: 
 47-    json.loads(sys.stdin.read())
 48-except ValueError, e: 
 49-    print False
 50-else:
 51-    print True
 52-sys.exit(0)"'
 53-    alias urlencode_json='python -c "
 54+import json
 55+try:
 56+  json.loads(sys.stdin.read())
 57+  print("true"); sys.exit(0)
 58+except ValueError:
 59+  print("false"); sys.exit(1)
 60+      '
 61+    }
 62+    function urlencode_json() {
 63+      python -c '
 64 import sys; del sys.path[0];
 65-import urllib, json;
 66-print urllib.quote_plus(sys.stdin.read())
 67-sys.exit(0)"'
 68-    alias urldecode_json='python -c "
 69+from urllib.parse import quote_plus
 70+print(quote_plus(sys.stdin.read()))
 71+      '
 72+    }
 73+    function urldecode_json() {
 74+      python -c '
 75 import sys; del sys.path[0];
 76-import urllib, json;
 77-print urllib.unquote_plus(sys.stdin.read())
 78-sys.exit(0)"'
 79+from urllib.parse import unquote_plus
 80+print(unquote_plus(sys.stdin.read()))
 81+      '
 82+    }
 83   ;;
 84   ruby)
 85-    alias pp_json='ruby -e "require \"json\"; require \"yaml\"; puts JSON.parse(STDIN.read).to_yaml"'
 86-    alias is_json='ruby -e "require \"json\"; begin; JSON.parse(STDIN.read); puts true; rescue Exception => e; puts false; end"'
 87-    alias urlencode_json='ruby -e "require \"uri\"; puts URI.escape(STDIN.read)"'
 88-    alias urldecode_json='ruby -e "require \"uri\"; puts URI.unescape(STDIN.read)"'
 89+    function pp_json() {
 90+      ruby -e '
 91+        require "json"
 92+        require "yaml"
 93+        puts JSON.parse(STDIN.read).to_yaml
 94+      '
 95+    }
 96+    function is_json() {
 97+      ruby -e '
 98+        require "json"
 99+        begin
100+          puts !!JSON.parse(STDIN.read); exit(0)
101+        rescue JSON::ParserError
102+          puts false; exit(1)
103+        end
104+      '