Diff
1diff --git a/plugins/jsontools/jsontools.plugin.zsh b/plugins/jsontools/jsontools.plugin.zsh
2index 912c835090b4155207c0cecc4c3d129bf7b2cf97..f92d336daecdd6ad3a0bf0f56dc6335943537f73 100644
3--- a/plugins/jsontools/jsontools.plugin.zsh
4+++ b/plugins/jsontools/jsontools.plugin.zsh
5@@ -1,42 +1,61 @@
6 # JSON Tools
7 # Adds command line aliases useful for dealing with JSON
8
9-if [[ $(whence $JSONTOOLS_METHOD) = "" ]]; then
10- JSONTOOLS_METHOD=""
11+# Check that user-defined method is installed
12+if [[ -n "$JSONTOOLS_METHOD" ]]; then
13+ (( $+commands[$JSONTOOLS_METHOD] )) || unset JSONTOOLS_METHOD
14 fi
15
16-if [[ $(whence node) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xnode" ) ]]; then
17- alias pp_json='xargs -0 node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 4));"'
18- 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); }"'
19- alias urlencode_json='xargs -0 node -e "console.log(encodeURIComponent(process.argv[1]))"'
20- alias urldecode_json='xargs -0 node -e "console.log(decodeURIComponent(process.argv[1]))"'
21-elif [[ $(whence python) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xpython" ) ]]; then
22- alias pp_json='python -c "import sys; del sys.path[0]; import runpy; runpy._run_module_as_main(\"json.tool\")"'
23- alias is_json='python -c "
24+# If method undefined, find the first one that is installed
25+if [[ ! -v JSONTOOLS_METHOD ]]; then
26+ for JSONTOOLS_METHOD in node python ruby; do
27+ # If method found, break out of loop
28+ (( $+commands[$JSONTOOLS_METHOD] )) && break
29+ # Otherwise unset the variable
30+ unset JSONTOOLS_METHOD
31+ done
32+
33+ # If no methods were found, exit the plugin
34+ [[ -v JSONTOOLS_METHOD ]] || return 1
35+fi
36+
37+# Define json tools for each method
38+case "$JSONTOOLS_METHOD" in
39+ node)
40+ alias pp_json='xargs -0 node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 4));"'
41+ 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); }"'
42+ alias urlencode_json='xargs -0 node -e "console.log(encodeURIComponent(process.argv[1]))"'
43+ alias urldecode_json='xargs -0 node -e "console.log(decodeURIComponent(process.argv[1]))"'
44+ ;;
45+ python)
46+ alias pp_json='python -c "import sys; del sys.path[0]; import runpy; runpy._run_module_as_main(\"json.tool\")"'
47+ alias is_json='python -c "
48 import sys; del sys.path[0];
49 import json;
50 try:
51- json.loads(sys.stdin.read())
52+ json.loads(sys.stdin.read())
53 except ValueError, e:
54- print False
55+ print False
56 else:
57- print True
58+ print True
59 sys.exit(0)"'
60- alias urlencode_json='python -c "
61+ alias urlencode_json='python -c "
62 import sys; del sys.path[0];
63 import urllib, json;
64 print urllib.quote_plus(sys.stdin.read())
65 sys.exit(0)"'
66- alias urldecode_json='python -c "
67+ alias urldecode_json='python -c "
68 import sys; del sys.path[0];
69 import urllib, json;
70 print urllib.unquote_plus(sys.stdin.read())
71 sys.exit(0)"'
72-elif [[ $(whence ruby) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xruby" ) ]]; then
73- alias pp_json='ruby -e "require \"json\"; require \"yaml\"; puts JSON.parse(STDIN.read).to_yaml"'
74- alias is_json='ruby -e "require \"json\"; begin; JSON.parse(STDIN.read); puts true; rescue Exception => e; puts false; end"'
75- alias urlencode_json='ruby -e "require \"uri\"; puts URI.escape(STDIN.read)"'
76- alias urldecode_json='ruby -e "require \"uri\"; puts URI.unescape(STDIN.read)"'
77-fi
78+ ;;
79+ ruby)
80+ alias pp_json='ruby -e "require \"json\"; require \"yaml\"; puts JSON.parse(STDIN.read).to_yaml"'
81+ alias is_json='ruby -e "require \"json\"; begin; JSON.parse(STDIN.read); puts true; rescue Exception => e; puts false; end"'
82+ alias urlencode_json='ruby -e "require \"uri\"; puts URI.escape(STDIN.read)"'
83+ alias urldecode_json='ruby -e "require \"uri\"; puts URI.unescape(STDIN.read)"'
84+ ;;
85+esac
86
87 unset JSONTOOLS_METHOD