dotfiles/.local/bin/scripts/edit-config

39 lines
1.8 KiB
Plaintext
Raw Normal View History

2022-07-06 07:10:13 +00:00
#!/bin/sh
2022-03-15 15:56:22 +00:00
# Find config files with fzf, preview and edit them
# Common config files are automatically checked/reloaded
2021-09-18 21:09:40 +00:00
alias dedup='awk '"'"'!a[$0]++'"'"
2021-09-20 06:56:42 +00:00
listconf() {
2022-07-05 09:40:11 +00:00
{ cat "$conf_cache"
fd --hidden --type file --size -1m --max-depth 1 . "$HOME"
find /boot -name "*.c*f*" 2>/dev/null
2021-09-21 21:10:39 +00:00
find "${XDG_CONFIG_HOME:-$HOME/.config}" /etc /var/lib/postgres /var/lib/pleroma/static/instance \
2022-03-15 15:56:22 +00:00
-maxdepth 4 -follow \
\( -name Partitions -o -name mdn -o -name .git -o -name .local -o -name plugged \) -prune -o \
! \( -iname "*.markdown" -o -iname "*.md" -o -name "Network Persistent State" -o -iname "*.pem" \) -a \
2022-07-05 09:40:11 +00:00
-type f -readable -exec grep -lI '' {} + 2>/dev/null
2021-12-23 20:39:55 +00:00
} | dedup
}
2021-09-20 06:56:42 +00:00
conf_cache_dir="${XDG_CACHE_HOME:-$HOME/.cache}/$(basename "$0")"
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;;
2022-08-04 21:55:07 +00:00
#systemctl daemon-reload && mount -af -o remount;;
(/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" && sudo nginx -t && sudo 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;;
2022-03-15 15:56:22 +00:00
(/etc/network/interfaces*) sudoedit "$sel" && sudo service networking reload;;
2022-08-04 21:55:07 +00:00
(*) edit "$sel";;
2021-09-18 21:23:11 +00:00
esac
echo "$sel" | cat - "$conf_cache" | head -20 >"$conf_tmp" && mv "$conf_tmp" "$conf_cache"