feat: validate input
Gracefully handle invalid setting and environment.
This commit is contained in:
parent
2d6f745bf9
commit
7324eeb65f
|
@ -0,0 +1,53 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
pictureuri="picture-uri"
|
||||||
|
|
||||||
|
validate_config() {
|
||||||
|
if [ -z "$WALLPAPER_DIR" ]; then
|
||||||
|
quit "Error: The wallpaper directory environment variable (WALLPAPER_DIR) has not been set."
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -e "$WALLPAPER_DIR" ]; then
|
||||||
|
quit "Error: The wallpaper directory \"$WALLPAPER_DIR\" does not exist."
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -d "$WALLPAPER_DIR" ]; then
|
||||||
|
quit "Error: The wallpaper directory \"$WALLPAPER_DIR\" is not a directory."
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -r "$WALLPAPER_DIR" ]; then
|
||||||
|
quit "Error: The wallpaper directory \"$WALLPAPER_DIR\" is not accessible. "
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
prepare() {
|
||||||
|
get_wallpaper
|
||||||
|
validate_wallpaper
|
||||||
|
}
|
||||||
|
get_wallpaper() {
|
||||||
|
selection="$(find "$WALLPAPER_DIR" -type f -name "*.jpg" -o -name "*.png" | shuf -n1)"
|
||||||
|
}
|
||||||
|
validate_wallpaper() {
|
||||||
|
if [ ! -f "$selection" ]; then
|
||||||
|
quit "Error: The wallpaper \"$WALLPAPER_DIR\" is not a file."
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -r "$selection" ]; then
|
||||||
|
quit "Error: The wallpaper \"$WALLPAPER_DIR\" is not accessible."
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
set_wallpaper() {
|
||||||
|
gsettings set org.gnome.desktop.background "$pictureuri" "file://$selection"
|
||||||
|
}
|
||||||
|
quit() {
|
||||||
|
if [[ "$1" = 0 ]]
|
||||||
|
then
|
||||||
|
exit 0
|
||||||
|
else
|
||||||
|
printf "%b\n" "$1"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
validate_config
|
||||||
|
prepare
|
||||||
|
set_wallpaper
|
||||||
|
quit 0
|
Loading…
Reference in New Issue