Skip to content

Commit

Permalink
fixes related to config loading, close #222
Browse files Browse the repository at this point in the history
- fix interactive configs loading all, instead of the first - `load_dorothy_config` now accepts `--first`
- fix interactive configs failing if don't exist - they are optional, don't fail - `load_dorothy_config` now accepts `--optional`
- fix a secondary user config not loading if a primary default config existed - default config now only loads if none of the user config filenames exist
  • Loading branch information
balupton committed Apr 25, 2024
1 parent 29c6302 commit 2da8d15
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 74 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ For each shell that you configured during the Dorothy installation (can be recon

1. If a login and interactive shell, it loads our interactive script `sources/interactive.(bash|dash|elv|fish|ksh|nu|xsh|zsh)`, which will:

1. Load your own `user/config(.local)/interactive.(bash|dash|elv|fish|ksh|nu|xsh|zsh)` configuration script for your own interactive login shell configuration.
1. Load your own `user/config(.local)/interactive.(sh|bash|dash|elv|fish|ksh|nu|xsh|zsh)` configuration script for your own interactive login shell configuration.
- [Elvish](https://elv.sh) will only load `interactive.elv` if it exists.
- [Fish](<https://en.wikipedia.org/wiki/Fish_(Unix_shell)>) will load `interactive.fish` if it exists, otherwise it will load `interactive.sh`.
- [Nu](https://www.nushell.sh) will only load `interactive.nu` and it must exist.
Expand Down Expand Up @@ -350,6 +350,13 @@ Stable commands:
- [`gocryptfs-helper`](https://github.com/bevry/dorothy/tree/master/commands/gocryptfs-helper) helpers for [GoCryptFS](https://github.com/rfjakob/gocryptfs)
- [`what-is-using`](https://github.com/bevry/dorothy/tree/master/commands/gocryptfs-helper) find out what is using a path so that you can unmount it safely

- Dorothy also provides commands for writing commands, such as:

- [`bash.bash`](https://github.com/bevry/dorothy/tree/master/sourcces/bash.bash) for a Bash strict mode that actually works, and various shims/polyfills
- [`ask`](https://github.com/bevry/dorothy/tree/master/commands/ask), [`confirm`](https://github.com/bevry/dorothy/tree/master/commands/confirm), and [`choose`](https://github.com/bevry/dorothy/tree/master/commands/choose) for prompting the user for input
- [`echo-style`](https://github.com/bevry/dorothy/tree/master/commands/echo-style), [`echo-segment`](https://github.com/bevry/dorothy/tree/master/commands/echo-segment), [`echo-element`](https://github.com/bevry/dorothy/tree/master/commands/echo-element), [`echo-error`](https://github.com/bevry/dorothy/tree/master/commands/echo-error), [`echo-verbose`](https://github.com/bevry/dorothy/tree/master/commands/echo-verbose), and [`eval-helper`](https://github.com/bevry/dorothy/tree/master/commands/eval-helper) for output styling
- Dozens of `echo-*`, `fs-*`, `get-*`, and `is-*` helpers

Beta commands:

- [`mail-sync`](https://github.com/bevry/dorothy/tree/master/commands.beta/mail-sync) helps you migrate all your emails from one cloud provider to another.
Expand Down
61 changes: 46 additions & 15 deletions sources/config.elv
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,62 @@
# @todo: couldn't get this going

# for scripts and sources to load a configuration file
# load_dorothy_config ...<filename>
fn load_dorothy_config {|@filenames|
var dorothy_config_loaded = $false
# load_dorothy_config [--first] [--silent] [--] ...<filename>
fn load_dorothy_config {
# process arguments
var only_first = $false
var optional = $false
while (count $args > 0) {
if (eq $args[0] '--first') {
set args = (drop $args 1)
set only_first $true
} elif (eq $args[0] '--optional') {
set args = (drop $args 1)
set optional $true
} elif (eq $args[0] '--') {
set args = (drop $args 1)
break
} else {
break
}
}

# for each filename, load a single config file
for filename filenames {
var filename
var loaded = $false
for filename $args {
if ?(test -f $E:DOROTHY'/user/config.local/'$filename) {
# load user/config.local/*
eval (cat $E:DOROTHY'/user/config.local/'$filename | slurp)
set dorothy_config_loaded = $true
set loaded = $true
} elif ?(test -f $E:DOROTHY'/user/config/'$filename) {
# otherwise load user/config/*
# load user/config/*
eval (cat $E:DOROTHY'/user/config/'$filename | slurp)
set dorothy_config_loaded = $true
} elif ?(test -f $E:DOROTHY'/config/'$filename) {
# otherwise load default configuration
eval (cat $E:DOROTHY'/config/'$filename | slurp)
set dorothy_config_loaded = $true
set loaded = $true
}
if (and (eq $only_first $true) (eq $loaded $true)) {
break
}
}
if (eq $loaded $false) {
for filename $filenames {
if ?(test -f $E:DOROTHY'/config/'$filename) {
# load default configuration
eval (cat $E:DOROTHY'/config/'$filename | slurp)
set loaded = $true
}
if (and (eq $only_first $true) (eq $loaded $true)) {
break
}
}
# otherwise try next filename
}

# if nothing was loaded, then fail
if (eq dorothy_config_loaded $false) {
echo-style --error="Missing the configuration file: $argv" >/dev/stderr
return 2 # No such file or directory
if (eq loaded $false) {
if (eq $optional $false) {
echo-style --error="Missing the configuration file: $argv" >/dev/stderr
return 2 # ENOENT 2 No such file or directory
}
}
return 0
}
62 changes: 48 additions & 14 deletions sources/config.fish
Original file line number Diff line number Diff line change
@@ -1,31 +1,65 @@
#!/usr/bin/env fish

# for scripts and sources to load a configuration file
# load_dorothy_config ...<filename>
# load_dorothy_config [--first] [--silent] [--] ...<filename>
function load_dorothy_config
set --local dorothy_config_loaded 'no'
# process arguments
set --local only_first 'no'
set --local optional 'no'
while test (count $argv) -ne 0
switch $argv[1]
case '--first'
set -e argv[1]
set only_first 'yes'
case '--optional'
set -e argv[1]
set optional 'yes'
case '--'
set -e argv[1]
break
case '*'
break
end
end

# for each filename, load a single config file
# load the configuration
set --local filename
set --local loaded 'no'
# for each filename, try user/config.local otherwise user/config
for filename in $argv
if test -f "$DOROTHY/user/config.local/$filename"
# load user/config.local/*
source "$DOROTHY/user/config.local/$filename"
set dorothy_config_loaded 'yes'
set loaded 'yes'
else if test -f "$DOROTHY/user/config/$filename"
# otherwise load user/config/*
# load user/config/*
source "$DOROTHY/user/config/$filename"
set dorothy_config_loaded 'yes'
else if test -f "$DOROTHY/config/$filename"
# otherwise load default configuration
source "$DOROTHY/config/$filename"
set dorothy_config_loaded 'yes'
set loaded 'yes'
end
if test "$only_first" = 'yes' -a "$loaded" = 'yes'
break
end
end
# if no user-defined configuration was provided, try the same filenames, but in the default configuration
if test "$loaded" = 'no'
for filename in $argv
if test -f "$DOROTHY/config/$filename"
# load default configuration
source "$DOROTHY/config/$filename"
set loaded 'yes'
end
if test "$only_first" = 'yes' -a "$loaded" = 'yes'
break
end
end
# otherwise try next filename
end

# if nothing was loaded, then fail
if test "$dorothy_config_loaded" = 'no'
echo-style --error="Missing the configuration file: $argv" >/dev/stderr
return 2 # No such file or directory
if test "$loaded" = 'no'
if test "$optional" = 'no'
echo-style --error="Missing the configuration file: $argv" >/dev/stderr
return 2 # ENOENT 2 No such file or directory
end
end
return 0
end
2 changes: 1 addition & 1 deletion sources/config.nu
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@

def load_dorothy_config [...filenames: string] {
echo-style --error='Nu does not support dynamic loading of configuration files.' >/dev/stderr
return 2 # No such file or directory
return 1 # EPERM 1 Operation not permitted
}
71 changes: 53 additions & 18 deletions sources/config.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,65 @@
#!/usr/bin/env sh

# for scripts and sources to load a configuration file
# load_dorothy_config ...<filename>
# load_dorothy_config [--first] [--silent] [--] ...<filename>
load_dorothy_config() {
dorothy_config_loaded='no'
# process arguments
load_dorothy_config__only_first='no' load_dorothy_config__optional='no'
while test "$#" -ne 0; do
case "$1" in
'--first')
shift
load_dorothy_config__only_first='yes'
;;
'--optional')
shift
load_dorothy_config__optional='yes'
;;
'--')
shift
break
;;
*) break ;;
esac
done

# for each filename, load a single config file
for dorothy_config_filename in "$@"; do
if test -f "$DOROTHY/user/config.local/$dorothy_config_filename"; then
# load the configuration
load_dorothy_config__loaded='no'
# for each filename, try user/config.local otherwise user/config
for load_dorothy_config__filename in "$@"; do
if test -f "$DOROTHY/user/config.local/$load_dorothy_config__filename"; then
# load user/config.local/*
. "$DOROTHY/user/config.local/$dorothy_config_filename"
dorothy_config_loaded='yes'
elif test -f "$DOROTHY/user/config/$dorothy_config_filename"; then
# otherwise load user/config/*
. "$DOROTHY/user/config/$dorothy_config_filename"
dorothy_config_loaded='yes'
elif test -f "$DOROTHY/config/$dorothy_config_filename"; then
# otherwise load default configuration
. "$DOROTHY/config/$dorothy_config_filename"
dorothy_config_loaded='yes'
. "$DOROTHY/user/config.local/$load_dorothy_config__filename"
load_dorothy_config__loaded='yes'
elif test -f "$DOROTHY/user/config/$load_dorothy_config__filename"; then
# load user/config/*
. "$DOROTHY/user/config/$load_dorothy_config__filename"
load_dorothy_config__loaded='yes'
fi
if test "$load_dorothy_config__only_first" = 'yes' -a "$load_dorothy_config__loaded" = 'yes'; then
break
fi
done
# if no user-defined configuration was provided, try the same filenames, but in the default configuration
if test "$load_dorothy_config__loaded" = 'no'; then
for load_dorothy_config__filename in "$@"; do
if test -f "$DOROTHY/config/$load_dorothy_config__filename"; then
# load default configuration
. "$DOROTHY/config/$load_dorothy_config__filename"
load_dorothy_config__loaded='yes'
fi
if test "$load_dorothy_config__only_first" = 'yes' -a "$load_dorothy_config__loaded" = 'yes'; then
break
fi
done
fi

# if nothing was loaded, then fail
if test "$dorothy_config_loaded" = 'no'; then
echo-style --error="Missing the configuration file: $*" >/dev/stderr
return 2 # ENOENT 2 No such file or directory
if test "$load_dorothy_config__loaded" = 'no'; then
if test "$load_dorothy_config__optional" = 'no'; then
echo-style --error="Missing the configuration file: $*" >/dev/stderr
return 2 # ENOENT 2 No such file or directory
fi
fi
return 0
}
55 changes: 39 additions & 16 deletions sources/config.xsh
Original file line number Diff line number Diff line change
@@ -1,31 +1,54 @@
#!/usr/bin/env xonsh

# @todo: couldn't get this going
# @todo THIS IS UNUSED, AS COULD NOT GET IT WORKING AS INTENDED

from os import path

# for scripts and sources to load a configuration file
# load_dorothy_config ...<filename>
# load_dorothy_config [--first] [--silent] [--] ...<filename>
def load_dorothy_config(*args):
dorothy_config_loaded = False
# process arguments
only_first = False
optional = False
while len(args) > 0:
if args[0] == '--first':
args = args[1:]
only_first = True
elif args[0] == '--optional':
args = args[1:]
optional = True
elif args[0] == '--':
args = args[1:]
break
else:
break

# for each filename, load a single config file
# load the configuration
loaded = False
# for each filename, try user/config.local otherwise user/config
for filename in args:
if path.exists($DOROTHY + '/user/config.local/' + filename):
# load user/config.local/*
execx(compilex(open($DOROTHY + '/user/config.local/' + filename).read()))
dorothy_config_loaded = True
loaded = True
elif path.exists($DOROTHY + '/user/config/' + filename):
# otherwise load user/config/*
# load user/config/*
execx(compilex(open($DOROTHY + '/user/config/' + filename).read()))
dorothy_config_loaded = True
elif path.exists($DOROTHY + '/config/' + filename):
# otherwise load default configuration
execx(compilex(open($DOROTHY + '/config/' + filename).read()))
dorothy_config_loaded = True
# otherwise try next filename
loaded = True
if only_first == 'yes' and loaded == 'yes':
break
# if no user-defined configuration was provided, try the same filenames, but in the default configuration
if loaded == False:
for filename in args:
if path.exists($DOROTHY + '/config/' + filename):
# load default configuration
execx(compilex(open($DOROTHY + '/config/' + filename).read()))
loaded = True
if only_first == 'yes' and loaded == 'yes':
break

# if nothing was loaded, then fail
if dorothy_config_loaded == False:
echo-style --error=@('Missing the configuration file: ' + args.join(' ')) >/dev/stderr
return 2 # No such file or directory
if loaded == False:
if optional == False:
echo-style --error=@('Missing the configuration file: ' + args.join(' ')) >/dev/stderr
return 2 # ENOENT 2 No such file or directory
return 0
5 changes: 1 addition & 4 deletions sources/interactive.fish
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
source "$DOROTHY/sources/config.fish"

# Load the configuration for interactive shells
# load each filename
# passes if one or more were loaded
# fails if none were loaded (all were missing)
load_dorothy_config 'interactive.fish' 'interactive.sh'
load_dorothy_config --first --optional -- 'interactive.fish' 'interactive.sh'

# Continue with the shell extras
source "$DOROTHY/sources/history.fish"
Expand Down
7 changes: 2 additions & 5 deletions sources/interactive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@

# Load the configuration for interactive shells
if test "$ACTIVE_POSIX_SHELL" = 'sh'; then
load_dorothy_config 'interactive.sh'
load_dorothy_config --first --optional -- 'interactive.sh'
else
# load each filename
# passes if one or more were loaded
# fails if none were loaded (all were missing)
load_dorothy_config "interactive.$ACTIVE_POSIX_SHELL" 'interactive.sh'
load_dorothy_config --first --optional -- "interactive.$ACTIVE_POSIX_SHELL" 'interactive.sh'
fi

# Continue with the shell extras
Expand Down

0 comments on commit 2da8d15

Please sign in to comment.