dotfiles/.local/bin/scripts/edconf

35 lines
1.6 KiB
Plaintext
Raw Normal View History

#!/bin/sh
# Find and edit config files utilizing fzf
2021-09-18 21:09:40 +00:00
alias dedup='awk '"'"'!a[$0]++'"'"
2021-09-20 06:56:42 +00:00
listconf() {
2021-09-21 20:51:19 +00:00
{ cat "$conf_cache";
fd --hidden --type file --size -1m --max-depth 1 . ~;
2021-09-21 21:10:39 +00:00
find "${XDG_CONFIG_HOME:-$HOME/.config}" /etc /var/lib/postgres /var/lib/pleroma/static/instance \
2021-10-31 19:27:18 +00:00
-maxdepth 4 -follow \( -name Partitions -o -name mdn -o -name .git -o -name .local -o -name plugged \) -prune -o \
2021-09-27 09:18:37 +00:00
! -iname "*.markdown" -a ! -iname "*.md" -a ! -iname "*.org" \
2021-12-23 20:39:55 +00:00
-type f -readable -exec grep -lI '' {} + 2>/dev/null;
2022-01-05 16:53:06 +00:00
find /boot -name "*.c*" 2>/dev/null;
2021-12-23 20:39:55 +00:00
} | dedup
}
2021-09-20 06:56:42 +00:00
2021-09-18 21:23:11 +00:00
conf_cache_dir="${XDG_CACHE_HOME:-$HOME/.cache}/edconf"
conf_cache="$conf_cache_dir/files"
conf_tmp="${conf_cache}.tmp"
mkdir -p "$conf_cache_dir"
touch "$conf_cache"
2021-09-20 06:56:42 +00:00
2021-10-04 08:46:40 +00:00
sel=$(listconf | fzf -1 -0 --tiebreak=end,length --preview '$(test -r {} || echo "sudo") bat --color=always --style=numbers --line-range :200 {}' --query="$1" --history "$conf_cache_dir/searches")
2021-09-18 21:23:11 +00:00
case "$sel" in
("") exit 1;;
(/etc/sudoers) sudo visudo;;
(/etc/fstab) sudoedit "$sel" && sudo findmnt --verify;;
(/etc/default/grub) sudoedit "$sel" && sudo grub-mkconfig -o /boot/grub/grub.cfg;;
(/etc/locale.gen) sudoedit "$sel" && sudo locale-gen;;
(/etc/nginx/*) sudoedit "$sel" && nginx -t && nginx -s reload;;
2022-01-25 13:02:13 +00:00
(/etc/caddy/*) sudoedit "$sel" && caddy validate --config /etc/caddy/Caddyfile && sudo systemctl reload-or-restart caddy;;
2022-01-21 18:57:10 +00:00
(/etc/ssh/*) sudoedit "$sel" && sudo systemctl reload-or-restart sshd;;
(*) $(test -w "$sel" || echo sudo) "$EDITOR" "$sel";;
2021-09-18 21:23:11 +00:00
esac
echo "$sel" | cat - "$conf_cache" | head -20 >"$conf_tmp" && mv "$conf_tmp" "$conf_cache"