Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions src/commands/config.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
###
# Output usage information
###
function _zulu_config_usage() {
echo $(_zulu_color yellow "Usage:")
echo " zulu config <args...>"
echo
echo $(_zulu_color yellow "Contexts:")
echo " list List all config values"
echo " get <key> Get a config value"
echo " set <key> <value> Set a config value"
}

###
# Set a config value
###
function _zulu_config_set() {
local key="$1" value="${(@)@:2}"

_zulu_config_load

# Rewrite the config file, omitting the key
# we're setting if it exists
echo $(cat $configfile | grep -v -E "^$key:") >! $configfile

# Write the new value to the config file
echo "$key: $value" >> $configfile

# Write the config file again, stripping out any blank lines
echo $(cat $configfile | grep -v -E '^\s*$') >! $configfile

zulu config $key
}

###
# Get a config value
###
function _zulu_config_get() {
local key="$1"

_zulu_config_load

if (( ! $+zulu_config[$key] )); then
return 1
fi

echo "$zulu_config[$key]"
}

###
# Load the current config
###
function _zulu_config_load {
[[ ! -f $configfile ]] && touch $configfile
eval $(_zulu_config_parse_yaml)
}

###
# Parse the YAML config file
# Based on https://gist.github.com/pkuczynski/8665367
###
function _zulu_config_parse_yaml() {
local s w fs prefix='zulu_config'
s='[[:space:]]*'
w='[a-zA-Z0-9_]*'
fs="$(echo @|tr @ '\034')"
sed -ne "s|^\(${s}\)\(${w}\)${s}:${s}\"\(.*\)\"${s}\$|\1${fs}\2${fs}\3|p" \
-e "s|^\(${s}\)\(${w}\)${s}[:-]${s}\(.*\)${s}\$|\1${fs}\2${fs}\3|p" "$configfile" |
awk -F"${fs}" '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s[%s]=\"%s\"\n", "'"$prefix"'",vn, $2, $3);
}
}' | sed 's/_=/+=/g'
}

###
# Zulu command to handle configuration
###
function _zulu_config() {
local ctx
local configfile="${ZULU_CONFIG_DIR:-"${ZDOTDIR:-$HOME}/.config/zulu"}/config.yml"

zparseopts -D h=help -help=help

# Output help and return if requested
if [[ -n $help ]]; then
_zulu_var_usage
return
fi

typeset -A zulu_config

ctx="$1"
case $ctx in
list )
cat $configfile
;;
set )
_zulu_config_set "${(@)@:2}"
;;
* )
_zulu_config_get "$@"
;;
esac
}
67 changes: 67 additions & 0 deletions tests/commands/config.zunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env zunit

@teardown {
cat /dev/null > tests/_support/.config/zulu/config.yml
}

@test 'Test "zulu config list" returns contents of config.yml' {
echo "testing: true" > tests/_support/.config/zulu/config.yml

run zulu config list

assert $state equals 0
assert "$output" same_as "testing: true"
}

@test 'Test "zulu config" returns correct value' {
echo "testing: true" > tests/_support/.config/zulu/config.yml

run zulu config testing

assert $state equals 0
assert "$output" same_as "true"
}

@test 'Test "zulu config set" sets value' {
run zulu config set rainbows unicorns

assert $state equals 0
assert "$output" same_as "unicorns"

run zulu config list

assert $state equals 0
assert "$output" same_as "rainbows: unicorns"

run zulu config rainbows

assert $state equals 0
assert "$output" same_as "unicorns"
}

@test 'Test "zulu config set" overwrites existing value' {
run zulu config set rainbows unicorns

assert $state equals 0
assert "$output" same_as "unicorns"

run zulu config list

assert $state equals 0
assert "$output" same_as "rainbows: unicorns"

run zulu config set rainbows pixies

assert $state equals 0
assert "$output" same_as "pixies"

run zulu config list

assert $state equals 0
assert "$output" same_as "rainbows: pixies"

run zulu config rainbows

assert $state equals 0
assert "$output" same_as "pixies"
}
18 changes: 18 additions & 0 deletions zulu.zsh-completion
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ _zulu_commands=(
'manpath:Functions for adding/removing directories from \$manpath'
'cdpath:Functions for adding/removing directories from \$cdpath'
'fpath:Functions for adding/removing directories from \$fpath'
'config:Functions for getting/setting configuration values'
)

###
Expand Down Expand Up @@ -90,6 +91,15 @@ _zulu_var_commands=(
'load:Reload all environment variables'
)

###
# Commands in the config context
###
_zulu_config_commands=(
'list:List all config values'
'get:Get a config value'
'set:Set a config value'
)

###
# Locating the package file
###
Expand Down Expand Up @@ -236,6 +246,10 @@ _zulu() {
_arguments \
'1: :_zulu_var_cmds'
;;
(config)
_arguments \
'1: :_zulu_config_cmds'
;;
(fpath)
_arguments \
'1: :_zulu_fpath_cmds'
Expand Down Expand Up @@ -385,6 +399,10 @@ _zulu() {
_describe -t commands 'commands' _zulu_var_commands "$@"
}

(( $+functions[_zulu_config_cmds] )) || _zulu_config_cmds() {
_describe -t commands 'commands' _zulu_config_commands "$@"
}

(( $+functions[_zulu_cmds] )) || _zulu_cmds() {
_describe -t commands 'commands' _zulu_commands "$@"
}
Expand Down