Skip to content

Commit 3ab1fff

Browse files
cburgmermclarkson
authored andcommitted
Make it work on macOS (bashtools#7)
* Document how to run on macOS, which is a BSD system * Fix padding to work on BSD Generated via `sed -i'' -e 's/%0${indent}s/%${indent}s/' JSONPath.sh`. Implementations seem to deviate here. For % with the 0 flag character, Linux states > The value should be zero padded. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversions, the converted value is padded on the left with zeros rather than blanks. while BSD states > A zero `0' character indicating that zero-padding should be used rather than blank-padding.
1 parent 086eb5d commit 3ab1fff

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

JSONPath.sh

+12-12
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ json() {
659659
arrays[j]=
660660
[[ -n ${closers[j]} ]] && {
661661
let indent=j*4
662-
printf "\n%0${indent}s${closers[j]}" ""
662+
printf "\n%${indent}s${closers[j]}" ""
663663
unset closers[j]
664664
comma[j]=
665665
}
@@ -673,7 +673,7 @@ json() {
673673
arrays[j]=
674674
[[ -n ${closers[j]} ]] && {
675675
let indent=j*4
676-
printf "\n%0${indent}s${closers[j]}" ""
676+
printf "\n%${indent}s${closers[j]}" ""
677677
unset closers[j]
678678
comma[j]=
679679
}
@@ -695,7 +695,7 @@ json() {
695695
arrays[i]=
696696
[[ -n ${closers[i]} ]] && {
697697
let indent=i*4
698-
printf "\n%0${indent}s${closers[i]}" ""
698+
printf "\n%${indent}s${closers[i]}" ""
699699
unset closers[i]
700700
comma[i]=
701701
}
@@ -721,25 +721,25 @@ json() {
721721
# Object
722722
[[ $i -ge $broken ]] && {
723723
let indent=i*4
724-
printf "${comma[i]}%0${indent}s{\n" ""
724+
printf "${comma[i]}%${indent}s{\n" ""
725725
closers[i]='}'
726726
comma[i]=
727727
}
728728
let indent=(i+1)*4
729-
printf "${comma[i]}%0${indent}s${path[i]}:\n" ""
729+
printf "${comma[i]}%${indent}s${path[i]}:\n" ""
730730
comma[i]=",\n"
731731
else
732732
# Array
733733
if [[ ${arrays[i]} != 1 ]]; then
734734
let indent=i*4
735-
printf "%0${indent}s" ""
735+
printf "%${indent}s" ""
736736
echo "["
737737
closers[i]=']'
738738
arrays[i]=1
739739
comma[i]=
740740
else
741741
let indent=(i+1)*4
742-
printf "\n%0${indent}s${closers[i-1]}" ""
742+
printf "\n%${indent}s${closers[i-1]}" ""
743743
direction=$DOWN
744744
comma[i+1]=",\n"
745745
fi
@@ -752,25 +752,25 @@ json() {
752752
# Object
753753
[[ $direction -eq $DOWN ]] && {
754754
let indent=pathlen*4
755-
printf "${comma[pathlen]}%0${indent}s{\n" ""
755+
printf "${comma[pathlen]}%${indent}s{\n" ""
756756
closers[pathlen]='}'
757757
comma[pathlen]=
758758
}
759759
let indent=(pathlen+1)*4
760-
printf "${comma[pathlen]}%0${indent}s" ""
760+
printf "${comma[pathlen]}%${indent}s" ""
761761
echo -n "${path[-1]}:$value"
762762
comma[pathlen]=",\n"
763763
else
764764
# Array
765765
[[ ${arrays[i]} != 1 ]] && {
766766
let indent=(pathlen-0)*4
767-
printf "%0${indent}s[\n" ""
767+
printf "%${indent}s[\n" ""
768768
closers[pathlen]=']'
769769
comma[pathlen]=
770770
arrays[i]=1
771771
}
772772
let indent=(pathlen+1)*4
773-
printf "${comma[pathlen]}%0${indent}s" ""
773+
printf "${comma[pathlen]}%${indent}s" ""
774774
echo -n "$value"
775775
comma[pathlen]=",\n"
776776
fi
@@ -784,7 +784,7 @@ json() {
784784
for i in `seq $((pathlen)) -1 0`
785785
do
786786
let indent=i*4
787-
printf "\n%0${indent}s${closers[i]}" ""
787+
printf "\n%${indent}s${closers[i]}" ""
788788
done
789789
echo
790790
fi

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ yo, so it's a JSONPath implementation written in Bash - and it probably only wor
44

55
[![travis](https://secure.travis-ci.org/mclarkson/JSONPath.sh.png?branch=master)](https://travis-ci.org/mclarkson/JSONPath.sh)
66

7+
Currently we rely on GNU implementations of the underlying tools. On OSX you can
8+
try wrapping the command like `./ensure_deps.sh ./JSONPath.sh`.
9+
710
## Invocation
811

912
JSONPath.sh [-b] [-i] [-j] [-h] [-p] [-u] [-f FILE] [pattern]
@@ -67,7 +70,7 @@ $ ./JSONPath.sh < package.json
6770
["repository","url"] "https://github.com/mclarkson/JSONPath.sh.git"
6871
["bin","JSONPath.sh"] "./JSONPath.sh"
6972
["author"] "Mark Clarkson <[email protected]>"
70-
["scripts","test"] "./all-tests.sh"
73+
["scripts","test"] "./ensure_deps.sh ./all-tests.sh"
7174
```
7275

7376
more complex examples:

ensure_deps.sh

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
is_osx() {
5+
test "$(uname -s)" == "Darwin"
6+
}
7+
8+
test_gnu_compat() {
9+
local error=0
10+
if ! seq 0 -1 1 &> /dev/null; then
11+
error=1
12+
>&2 echo "Make sure you have GNU coreutils installed"
13+
fi
14+
15+
if grep --version | grep 'BSD' &> /dev/null; then
16+
error=1
17+
>&2 echo "Make sure you have GNU grep or compatible installed"
18+
fi
19+
20+
if ! sed --version &> /dev/null; then
21+
error=1
22+
>&2 echo "Make sure you have GNU sed or compatible installed"
23+
fi
24+
25+
if [[ $error -gt 0 && is_osx ]]; then
26+
>&2 echo "With homebrew you may install necessary deps via \`brew install grep gnu-sed coreutils\`"
27+
fi
28+
29+
return $error
30+
}
31+
32+
main() {
33+
if is_osx; then
34+
export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
35+
export PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"
36+
export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"
37+
fi
38+
39+
test_gnu_compat
40+
41+
"$@"
42+
}
43+
44+
main "$@"

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
"devDependencies": {},
1515
"author": "Mark Clarkson <[email protected]>",
1616
"scripts": {
17-
"test": "./all-tests.sh"
17+
"test": "./ensure_deps.sh ./all-tests.sh"
1818
}
1919
}

0 commit comments

Comments
 (0)