Skip to content

Commit

Permalink
add --file=<file> support to config-edit in prep for updating nushe…
Browse files Browse the repository at this point in the history
…ll config files

also rename `echo-escape-backslashes` to `echo-escape-regex-replacement` to be more accurate to its intent
  • Loading branch information
balupton committed Aug 18, 2023
1 parent cfadc05 commit c6c4767
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 20 deletions.
39 changes: 29 additions & 10 deletions commands/config-edit
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,37 @@ function config_edit() (
config-edit --sudoers --line=<line>
config-edit --cron-system --line=<line>
config-edit --cron-user --line=<line>
config-edit --file --line=<line>
OPTIONS:
--name=<name>
The name of the configuration to edit, e.g. 'sudoers'
[--action=<add|remove|edit|has>]
[--add]
[--remove]
[--has]
--action=<add|remove|edit|has>
--add
--remove
--has
Search for the <line> and return success if found, or failure if missing.
[--edit]
--edit
Whether to add or remove the line, or edit the configuration.
Defaults to <add> if <line> is provided, otherwise <remove> if <needle> is provided, otherwise <edit> which will just trigger the <edit-command> command.
[--line=<line>]
--line=<line>
The line that should be inserted or removed.
--searcher=<search-command>
A command that is called that will fetch relevant lines from the configuration file. Receives the line as the first argument.
[--comparer=<compare-command>]
--comparer=<compare-command>
A command that is called that will compare the found lines (first argument) with the desired <line> (second argument).
--editer=<edit-command>
A command that is called that will edit the configuration file.
[--applier=<apply-command>]
--applier=<apply-command>
A command that is called that will automatically replace the found lines (first argument) with the desired <line> (second argument) and save changes, inserting the desired <line> if no lines were found.
--fuse | --fstab | --sudoers | --cron-system || --cron-user
--fuse | --fstab | --sudoers | --cron-system | --cron-user | --file=<file>
Each of these set appropriate defaults for those configurations.
EOF
if test "$#" -ne 0; then
Expand All @@ -65,7 +66,7 @@ function config_edit() (
}

# process
local item option_name='' option_action='' option_line='' option_needle='' option_searcher='' option_comparer='default_comparer' option_editer='' option_applier=''
local item option_name='' option_file='' option_action='' option_line='' option_needle='' option_searcher='' option_comparer='default_comparer' option_editer='' option_applier=''
while test "$#" -ne 0; do
item="$1"
shift
Expand All @@ -89,6 +90,24 @@ function config_edit() (
'--has') option_action='has' ;;
'--edit') option_action='edit' ;;
'--name='*) option_name="${item#*--name=}" ;;
'--file='*)
option_file="${item#*--file=}"
option_name="$option_file"
option_searcher='default_file_searcher'
option_editer='default_file_editer'
option_applier='default_file_applier'
function default_file_searcher {
rg --fixed-strings --regexp="$1" "$option_file"
}
function default_file_editer {
edit -- "$option_file"
}
function default_file_applier {
local needle="${1:-"$2"}" replace="$2"
config-helper --file="$option_file" -- \
--string-find="$needle" --string-replace="$replace"
}
;;
'--line='*) option_line="${item#*--line=}" ;;
'--needle='*) option_needle="${item#*--needle=}" ;;
'--searcher='*) option_searcher="${item#*--searcher=}" ;;
Expand Down
2 changes: 1 addition & 1 deletion commands/config-helper
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function config_helper() (
# raw replace value
value="${option_replace#*--string-replace=}"
addition="$value"
replace_pattern="$(echo-escape-backslash "$value")"
replace_pattern="$(echo-escape-regex-replacement "$value")"
elif [[ $option_replace == '--replace='* ]]; then
# replace value
value="${option_replace#*--replace=}"
Expand Down
1 change: 1 addition & 0 deletions commands/dorothy
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,7 @@ function dorothy() (
source "$DOROTHY/sources/config.bash"
update_dorothy_user_config 'interactive.sh' -- \
--find='export DOROTHY_THEME=(.*)' --replace="export DOROTHY_THEME=$(echo-quote "$theme")"
# config-edit --file="$DOROTHY/user/config/interactive.nu" --needle="\$env.DOROTHY_THEME" --line="\$env.DOROTHY_THEME = $(echo-quote "$theme")" --add

# log
echo-style --success='Configuration change applied, ' --notice='restart your terminal for the change to take effect.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,32 @@ function echo_escape_backslash() (
function help {
cat <<-EOF >/dev/stderr
ABOUT:
For each input, escape backslash characters by prepending backslashes.
For each input, escape any characters that would interfere with a regex replacement.
USAGE:
echo-escape-backslash <...input>
echo-lines <...input> | echo-escape-backslash
echo-escape-regex-replacement <...input>
echo-lines <...input> | echo-escape-regex-replacement
EXAMPLE:
echo-escape-backslash 'a\040b'
echo-escape-regex-replacement 'Special: \040b'
a\\\\040b
Special: \\\\040b
# exit status: 0
echo-lines 'a\040b' | echo-escape-backslash
echo-escape-regex-replacement 'Special: \040b' | echo-escape-regex-replacement
a\\\\040b
Special: \\\\040b
# exit status: 0
EOF
return 22 # EINVAL 22 Invalid argument
}

function on_input {
echo "${1//\\/\\\\}"
local value="$1"
value="${value//\\/\\\\}"
printf '%s\n' "$value"
}

source "$DOROTHY/sources/stdinargs.bash"
Expand Down
4 changes: 3 additions & 1 deletion commands/echo-escape-spaces
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ function echo_escape_spaces() (
}

function on_input {
echo "${1// /\\ }"
local value="$1"
value="${value// /\\ }"
printf '%s\n' "$value"
}

source "$DOROTHY/sources/stdinargs.bash"
Expand Down

0 comments on commit c6c4767

Please sign in to comment.