Add support for a config file

This commit is contained in:
Salt 2019-09-26 14:40:37 -05:00
parent ec56861b40
commit 5546950fd5
3 changed files with 48 additions and 3 deletions

View File

@ -2,7 +2,11 @@
A tool to play a plaintext playlist composed entirely of youtube-dl search queries
## Setup
## Installation
Put `ptgdp` somewhere in `$PATH` and install `youtube-dl`.
## Quickstart
Make a file containing a few download queries. For example:
@ -20,6 +24,10 @@ Then invoke `ptgdp`:
See below for more intricate usage.
## Configuration
Move `ptgdp.conf` from this repo to `~/.config/ptgdp.conf` and change the values as you see fit.
## License
See `LICENSE` (hint: it's MIT, just like the header says).

32
ptgdp
View File

@ -9,7 +9,12 @@ set -e
# Read-only set-once variables
declare -r _name="$(basename -- "$0")"
declare -ra _supportedbackends=("mpd")
# Options
declare -A _config=(
[backend]="mpd"
)
declare _optconfigfile="${XDG_CONFIG_HOME:-$HOME/.config}/${_name}.conf"
declare -i _opthelp
declare -i _optverbose=0
# Working variables
@ -54,6 +59,7 @@ Usage: $_name [OPTION]... [FILE]...
Use youtube-dl and a music player to queue up or download a number of songs
given plaintext files with only search queries
-c [FILE] Load the given file in place of the usual config file
-h Print this help text
-v Print more status messages. Stacks
@ -65,8 +71,11 @@ EOF
# Main
main() {
# Getopts before anything else
while getopts ":hv" opt; do
while getopts ":c:hv" opt; do
case $opt in
c)
_optconfigfile="$OPTARG"
;;
h)
_opthelp=1
;;
@ -81,7 +90,26 @@ main() {
;;
esac
done
# Parse out a config file if it exists
if [ -f "$_optconfigfile" ]; then
log "Loading config file" 1
while read line; do
# If the line has an equals sign and isn't a comment
if [ "$line" != "${line#*=}" ] && [ "$line" = "${line#\#}" ]; then
local varname="${line%=*}"
local value="${line#*=}"
_config[$varname]="$value"
log "Setting $varname to $value" 1
fi
done < "$_optconfigfile"
fi
# Validate critical options
{
for backend in ${_supportedbackends[@]}; do
[ "$backend" = "${_config[backend]}" ] && return 0
done
error "Unsupported backend: ${_config[backend]}" 50
}
# Pre-really-do-stuff hooks like help text
[ -n "$_opthelp" ] && printhelp && exit 0

9
ptgdp.conf Normal file
View File

@ -0,0 +1,9 @@
#
# ptgdp.conf
# Config file for Play the Goddamned Playlist
#
# The media player backend to use
# Supported: mpd
# Default: mpd
backend=mpd