-
Notifications
You must be signed in to change notification settings - Fork 0
/
structurer
executable file
·114 lines (99 loc) · 2.81 KB
/
structurer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bash
set -e
set -u
file=${1:--}
dest=${2?"Missing destination directory"}
configpath=${STRUCTURER_PATH:-~/.structurer}
configfile=$(cat $configpath/structurer.json || echo '{ "aliases":[] }')
function wrap() {
DIR=$(mktemp -d)
$1 $DIR
N=$(ls $DIR | wc -w | tr -d ' ')
if [[ $N = 1 ]]; then
mv $DIR/* "$2"
else
mv $DIR "$2"
fi
rm -rf $DIR
}
function unarchive() {
archive=$1
case "$archive" in
*.tar.gz)
function unarchive_tar() { tar -xzf "$archive" -C $1 ; }
wrap unarchive_tar $2
;;
*.zip)
function unarchive_zip() { unzip "$archive" -d $1 > /dev/null ; }
wrap unarchive_zip $2
;;
*)
(>&2 echo "Cannot link '$archive'; this is not a directory")
return -1
;;
esac
return 0
}
mkdir -p $dest
lastpath=
while IFS=":" read line alias; do
path=
file=
atype=
avalue=
if [[ -n $alias ]]; then
atype="$(jq -r --arg alias "$alias" '.aliases[] | select(.name == $alias) | .type' <(echo "$configfile"))"
avalue="$(jq -r --arg alias "$alias" '.aliases[] | select(.name == $alias) | .value' <(echo "$configfile"))"
fi
if [[ ${line:0:1} != "/" ]]; then
lastpath=
fi
if [[ ${line:((${#line}-1))} = "/" ]]; then
path="$lastpath/$line"
else
path="$lastpath/$(dirname $line)"
file="$(basename $line)"
fi
if [[ ! -n $file ]] && [[ -n $alias ]]; then
case "$atype" in
file)
if [[ -d "$configpath/$avalue" ]]; then
cp -r "$configpath/$avalue" "$dest/$path"
else
unarchive "$configpath/$avalue" "$dest/$path"
fi
;;
url)
tmp=$(mktemp -d)
(cd $tmp; curl -Ls -O -J "$avalue")
unarchive $tmp/* "$dest/$path"
rm -rf $tmp
;;
*)
(>&2 echo "Invalid alias '${alias}' for file '${file}'")
;;
esac
else
mkdir -p "$dest/$path"
fi
if [[ -n $file ]] && [[ -n $alias ]]; then
case "$atype" in
file)
if [[ -f "$configpath/$avalue" ]]; then
cat "$configpath/$avalue" > "$dest/$path/$file"
else
(>&2 echo "Cannot get the content of '$configpath/$avalue'; this is not a file")
fi
;;
url)
curl -s "$avalue" -o "$dest/$path/$file"
;;
*)
(>&2 echo "Invalid alias '${alias}' for file '${file}'")
;;
esac
else
touch "$dest/$path/$file"
fi
lastpath="$path"
done < <(cat -- "$file")