From 5546950fd55fbfb7ba89c7475d416eead7aea565 Mon Sep 17 00:00:00 2001 From: Salt Date: Thu, 26 Sep 2019 14:40:37 -0500 Subject: [PATCH] Add support for a config file --- README.md | 10 +++++++++- ptgdp | 32 ++++++++++++++++++++++++++++++-- ptgdp.conf | 9 +++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 ptgdp.conf diff --git a/README.md b/README.md index 8ae4b16..7379d90 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/ptgdp b/ptgdp index 5f06550..d85499f 100755 --- a/ptgdp +++ b/ptgdp @@ -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 diff --git a/ptgdp.conf b/ptgdp.conf new file mode 100644 index 0000000..ffb9e6e --- /dev/null +++ b/ptgdp.conf @@ -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