Added the config feature

Added the ability for the script to checkout the config and parsing it
Added an example of a config file
This commit is contained in:
Karma Riuk 2021-03-04 22:31:45 +01:00
parent 9dbca4a5e9
commit 6edd9bd290
2 changed files with 112 additions and 42 deletions

10
config.ini Normal file
View File

@ -0,0 +1,10 @@
[Controller]
normal_delta=5
big_delta=10
volume_min=5
volume_max=150
[Notification]
enabled=yes
timeout=3000

144
volumectl
View File

@ -1,33 +1,49 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# TODO: in the readme, remind the user that they don't have to write a fully
# fledged config if they only want to modify a single element (like "normal
# delta"), if the other values are not specified, the default values will be
# used
## Default controller values ## Default controller values
VOLUME_CONTROLLER="pamixer" DEFAULT_VOLUME_CONTROLLER="pamixer"
INCREASE_FLAG="--increase" DEFAULT_INCREASE_FLAG="--increase"
DECREASE_FLAG="--decrease" DEFAULT_DECREASE_FLAG="--decrease"
SET_VOLUME_FLAG="--set-volume" DEFAULT_SET_VOLUME_FLAG="--set-volume"
SET_MUTE_FLAG="--mute" DEFAULT_SET_MUTE_FLAG="--mute"
SET_UNMUTE_FLAG="--unmute" DEFAULT_SET_UNMUTE_FLAG="--unmute"
TOGGLE_MUTE_FLAG="--toggle-mute" DEFAULT_TOGGLE_MUTE_FLAG="--toggle-mute"
GET_VOLUME_FLAG="--get-volume" DEFAULT_GET_VOLUME_FLAG="--get-volume"
GET_MUTE_FLAG="--get-mute" DEFAULT_GET_MUTE_FLAG="--get-mute"
ADDITIONAL_FLAGS=("--allow-boost") DEFAULT_ADDITIONAL_FLAGS=("--allow-boost")
## Default values ## Default values
NOTIFICATION_ENABLED=1 DEFAULT_NOTIFICATION_ENABLED=1
NOTIFICATION_TIMEOUT=3000 DEFAULT_NOTIFICATION_TIMEOUT=3000
DEFAULT_NORMAL_DELTA=5
DEFAULT_BIG_DELTA=10
DEFAULT_VOLUME_MAX=100
DEFAULT_VOLUME_MIN=5
CONFIG_PATH="$XDG_CONFIG_HOME/volumectl/config.ini"
VERBOSE=0 VERBOSE=0
NORMAL_DELTA=5 CONFIG_PATH="${XDG_CONFIG_HOME:-$HOME/.config}/volumectl/config.ini"
BIG_DELTA=10 READ_INI_POSSIBLE_PATHS=(\
VOLUME_MAX=100 "${XDG_LIB_HOME:-$HOME/.local/lib}/read_ini.sh" "${XDG_LIB_HOME:-$HOME/.local/lib}/bash_ini_parser/read_ini.sh" \
VOLUME_MIN=5 "./read_ini.sh" "./bash_ini_parser/read_ini.sh" \
"./lib/read_ini.sh" "./lib/bash_ini_parser/read_ini.sh" \
)
## Args ## Args
OPTIONAL_FLAGS=("--notification-timeout" "-v" "--verbose" "-c" "--config" "-n" "--notifcation" "-N" "--no-notification" "-h" "--help") OPTIONAL_FLAGS=( \
"-v" "--verbose" \
"-c" "--config" \
"-h" "-?" "--help" \
)
verbose () { verbose () {
[[ $VERBOSE -eq 1 ]] && return 0 || return 1 [[ $VERBOSE -eq 1 ]] && return 0 || return 1
@ -52,6 +68,67 @@ notify () {
verbose && ( [[ $NOTIFICATION_ENABLED -eq 1 ]] && echo "Notified with \`$cmd\`" || echo "Didn't notify" ) verbose && ( [[ $NOTIFICATION_ENABLED -eq 1 ]] && echo "Notified with \`$cmd\`" || echo "Didn't notify" )
} }
get_parser_location () {
local parser_location=""
for location in "${READ_INI_POSSIBLE_PATHS[@]}"; do
[[ -f "$location" ]] && parser_location="$location" && break
done
echo "$parser_location"
}
checkout_config () {
local parser_location="$(get_parser_location)"
[[ -z "$parser_location" ]] && verbose && echo "Unable to find the 'bash_ini_parser' file, which is used to read the config file. Will be using the default values of the script." && return 0
verbose && echo "Sourcing the ini parser"
source "$parser_location"
verbose && echo "Reading the config file"
read_ini "$CONFIG_PATH"
VOLUME_CONTROLLER=${INI__Controller__volume_controller:-$DEFAULT_VOLUME_CONTROLLER}
INCREASE_FLAG=${INI__Controller__increase_flag:-$DEFAULT_INCREASE_FLAG}
DECREASE_FLAG=${INI__Controller__decrease_flag:-$DEFAULT_DECREASE_FLAG}
SET_VOLUME_FLAG=${INI__Controller__set_volume_flag:-$DEFAULT_SET_VOLUME_FLAG}
SET_MUTE_FLAG=${INI__Controller__set_mute_flag:-$DEFAULT_SET_MUTE_FLAG}
SET_UNMUTE_FLAG=${INI__Controller__set_unmute_flag:-$DEFAULT_SET_UNMUTE_FLAG}
TOGGLE_MUTE_FLAG=${INI__Controller__toggle_mute_flag:-$DEFAULT_TOGGLE_MUTE_FLAG}
GET_VOLUME_FLAG=${INI__Controller__get_volume_flag:-$DEFAULT_GET_VOLUME_FLAG}
GET_MUTE_FLAG=${INI__Controller__get_mute_flag:-$DEFAULT_GET_MUTE_FLAG}
ADDITIONAL_FLAGS=${INI__Controller__additional_flags:-$DEFAULT_ADDITIONAL_FLAGS}
NORMAL_DELTA=${INI__Controller__normal_delta:-$DEFAULT_NORMAL_DELTA}
BIG_DELTA=${INI__Controller__big_delta:-$DEFAULT_BIG_DELTA}
VOLUME_MAX=${INI__Controller__volume_max:-$DEFAULT_VOLUME_MAX}
VOLUME_MIN=${INI__Controller__volume_min:-$DEFAULT_VOLUME_MIN}
NOTIFICATION_ENABLED=${INI__Notification__enabled:-$DEFAULT_NOTIFICATION_ENABLED}
NOTIFICATION_TIMEOUT=${INI__Notification__timeout:-$DEFAULT_NOTIFICATION_TIMEOUT}
verbose && echo "" && echo "After parsing config file, variables are:" && \
echo "VOLUME_CONTROLLER: '$VOLUME_CONTROLLER'" && \
echo "INCREASE_FLAG: '$INCREASE_FLAG'" && \
echo "DECREASE_FLAG: '$DECREASE_FLAG'" && \
echo "SET_VOLUME_FLAG: '$SET_VOLUME_FLAG'" && \
echo "GET_VOLUME_FLAG: '$GET_VOLUME_FLAG'" && \
echo "SET_MUTE_FLAG: '$SET_MUTE_FLAG'" && \
echo "GET_MUTE_FLAG: '$GET_MUTE_FLAG'" && \
echo "ADDITIONAL_FLAGS: '${ADDITIONAL_FLAGS[@]}'" && \
echo "NORMAL_DELTA: '$NORMAL_DELTA'" && \
echo "BIG_DELTA: '$BIG_DELTA'" && \
echo "VOLUME_MIN: '$VOLUME_MIN'" && \
echo "VOLUME_MAX: '$VOLUME_MAX'" && \
echo "NOTIFICATION_ENABLED: '$NOTIFICATION_ENABLED'" && \
echo "NOTIFICATION_TIMEOUT: '$NOTIFICATION_TIMEOUT'" && \
echo ""
}
volume_up_by () { volume_up_by () {
local cmd="$VOLUME_CONTROLLER ${ADDITIONAL_FLAGS[@]} $INCREASE_FLAG $1" local cmd="$VOLUME_CONTROLLER ${ADDITIONAL_FLAGS[@]} $INCREASE_FLAG $1"
verbose && echo "Increasing volume by $1, with \`$cmd\`" verbose && echo "Increasing volume by $1, with \`$cmd\`"
@ -167,47 +244,28 @@ Options:
-G, --get-mute get mute -G, --get-mute get mute
Optional arguments: Optional arguments:
--notification-timeout arg forces the notification timeout, in milliseconds (overrides the configuation setting, -c path, --config path instead of reading from the default location ($CONFIG_PATH),
the default value is $NOTIFICATION_TIMEOUT) read from the path given
-n, --notification forces the notification to appear (overrides the configuration setting)
-N, --no-notification forces the notification NOT to appear (overrides the configuration setting)
-v, --verbose print each step the script passes through -v, --verbose print each step the script passes through
-h, -?, --help show this message -h, -?, --help show this message
EOF EOF
} }
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then #if '__name__' == '__main__' if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then #if '__name__' == '__main__'
# check if the notification is being forced or not
while [[ " ${OPTIONAL_FLAGS[@]} " =~ " $1 " ]]; do while [[ " ${OPTIONAL_FLAGS[@]} " =~ " $1 " ]]; do
verbose && echo "Checking for optional arguments" verbose && echo "Checking for optional arguments"
case $1 in case $1 in
-v|--verbose) -v|--verbose)
VERBOSE=1 VERBOSE=1
verbose && echo "Setting \$VERBOSE to true (\$VERBOSE=$VERBOSE)" echo "Setting \$VERBOSE to 1 (\$VERBOSE=$VERBOSE)"
shift shift
;; ;;
--notification-timeout)
NOTIFICATION_TIMEOUT="$2";
verbose && echo "Setting \$NOTIFICATION_TIMEOUT to $2 (\$NOTIFICATION_TIMEOUT=$NOTIFICATION_TIMEOUT)"
shift 2
;;
-c|--config) -c|--config)
CONFIG_PATH="$2"; CONFIG_PATH="$2";
verbose && echo "Setting \$CONFIG_PATH to $2 (\$CONFIG_PATH=$CONFIG_PATH)" verbose && echo "Setting \$CONFIG_PATH to $2 (\$CONFIG_PATH=$CONFIG_PATH)"
shift 2 shift 2
;; ;;
-n|--notification)
NOTIFICATION_ENABLED=1
verbose && echo "Setting \$NOTIFICATION_ENABLED to true (\$NOTIFICATION_ENABLED=$NOTIFICATION_ENABLED)"
shift
;;
-N|--no-notification)
NOTIFICATION_ENABLED=0;
verbose && echo "Setting \$NOTIFICATION_ENABLED to false (\$NOTIFICATION_ENABLED=$NOTIFICATION_ENABLED)"
shift
;;
-h|-\?|--help) -h|-\?|--help)
verbose && echo "Detected -h, -? or --help" verbose && echo "Detected -h, -? or --help"
usage usage
@ -216,6 +274,8 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then #if '__name__' == '__main__'
esac esac
done done
checkout_config
verbose && echo "Looking at the argument '$@'" verbose && echo "Looking at the argument '$@'"
case $1 in case $1 in
-i|--increase) -i|--increase)