Skip to content

Commit 244c522

Browse files
committed
feat: allow multipole enables in a single command
1 parent 81bac97 commit 244c522

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

bash.d.sh

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#!/bin/bash
22

3-
BASHDDIR=$(dirname $0)
3+
BASHDDIR=$(dirname "$0")
44

5-
USAGE="`basename $0` [-e <config>|-d config|-l]"
5+
# shellcheck disable=SC2086
6+
USAGE="$(basename $0) [-e <config>|-d config|-l]"
67

78
function usage {
8-
echo $USAGE
9+
echo "$USAGE"
910
cat <<EOF
1011
Enable, disable or list available bash config. Run without any parameters it
1112
will list currently enabled configs.
@@ -19,39 +20,48 @@ EOF
1920
}
2021

2122
function enable_config {
22-
SOURCE=$BASHDDIR/avaiable/$1
23-
TARGET=$BASHDDIR/enabled/$1
23+
# first arg is the -e so renove it
24+
shift
25+
exit_state=0
26+
for src in "$@"; do
27+
SOURCE=$BASHDDIR/available/$src
28+
TARGET=$BASHDDIR/enabled/$src
2429

25-
if [ ! -e "$SOURCE" ]; then
26-
echo "No such config ($1)"
27-
exit 1
28-
fi
30+
if [ ! -e "$SOURCE" ]; then
31+
echo "No such config ($1)"
32+
exit_state=1
33+
fi
2934

30-
if [ -L "$TARGET" ]; then
31-
echo "Config already enabled ($1)"
32-
exit 1
33-
fi
35+
if [ -L "$TARGET" ]; then
36+
echo "Config already enabled ($1)"
37+
exit_state=1
38+
fi
3439

35-
ln -s $(realpath $SOURCE) $TARGET
40+
# shellcheck disable=SC2046
41+
ln -s $(realpath "$SOURCE") "$TARGET"
42+
echo "Enabled $src"
43+
done
44+
exit "$exit_state"
3645
}
3746

3847
function disable_config {
3948
TARGET=$BASHDDIR/enabled/$1
40-
rm -f $TARGET
49+
rm -f "$TARGET"
4150
}
4251

4352
function list_configs {
44-
ls -1 $BASHDDIR/avaiable
53+
ls -1 "$BASHDDIR/available"
4554
}
4655

4756
while getopts "e:d:lh" options; do
4857
case $options in
49-
e) enable_config $OPTARG; exit;;
50-
d) disable_config $OPTARG; exit;;
58+
e) enable_config "$@"; exit;;
59+
d) disable_config "$@"; exit;;
5160
l) list_configs; exit;;
52-
h) usage; exit;;
61+
h) ;;
62+
*) usage; exit;;
5363
esac
5464
done
5565

5666
# If we got this far without exiting it's a list currently enabled
57-
ls -1 $BASHDDIR/enabled
67+
ls -1 "$BASHDDIR/enabled"

0 commit comments

Comments
 (0)