add addpaths script

This commit is contained in:
2026-02-03 08:44:37 +02:00
parent 49300923f7
commit 540b18e6e8
4 changed files with 94 additions and 2 deletions

85
config/shared/addpaths Executable file
View File

@@ -0,0 +1,85 @@
#!/usr/bin/env bash
set -euo pipefail
usage() {
echo "Usage: $0 -c COMMENT_SYMBOL [-e EXCLUDE_PATTERN]... TARGET"
echo " -c Comment symbol (e.g., '#' or '//')"
echo " -e Exclude pattern (can be specified multiple times)"
echo " TARGET File or directory to process"
exit 1
}
comment_sym=""
excludes=()
while getopts "c:e:h" opt; do
case $opt in
c) comment_sym="$OPTARG" ;;
e) excludes+=("$OPTARG") ;;
h) usage ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
[[ $# -ne 1 ]] && usage
[[ -z "$comment_sym" ]] && usage
target="$(realpath "$1")"
base_dir="$(pwd)"
should_exclude() {
local path="$1"
for pattern in "${excludes[@]}"; do
if [[ "$path" == *"$pattern"* ]]; then
return 0
fi
done
return 1
}
process_file() {
local file="$1"
local rel_path="${file#"$base_dir"/}"
should_exclude "$rel_path" && return
local path_comment="$comment_sym path: $rel_path"
# Read first two lines
local line1 line2
IFS= read -r line1 <"$file" 2>/dev/null || line1=""
IFS= read -r line2 < <(tail -n +2 "$file") 2>/dev/null || line2=""
# Check if path comment exists in first or second line
[[ "$line1" == *"path: "* ]] && return
[[ "$line2" == *"path: "* ]] && return
# Insert after shebang if present, otherwise at top
if [[ "$line1" =~ ^#! ]]; then
{
echo "$line1"
echo "$path_comment"
tail -n +2 "$file"
} >"$file.tmp"
else
{
echo "$path_comment"
cat "$file"
} >"$file.tmp"
fi
mv "$file.tmp" "$file"
}
if [[ -f "$target" ]]; then
process_file "$target"
elif [[ -d "$target" ]]; then
while IFS= read -r -d '' file; do
process_file "$file"
done < <(find "$target" -type f -print0)
else
echo "Error: $target is not a file or directory" >&2
exit 1
fi

View File

@@ -54,4 +54,4 @@ alias gcm='git commit -m'
alias gp='git push' alias gp='git push'
alias gst='git status' alias gst='git status'
alias gd='git diff --patience --color-moved=dimmed-zebra --word-diff=plain --function-context --ignore-space-change -U3' alias gd='git diff --patience --color-moved=dimmed-zebra --word-diff=plain --function-context --ignore-space-change -U3'
alias gl='git log --oneline --graph --decorate --all' alias glg='git log --oneline --graph --decorate --all'

View File

@@ -12,6 +12,12 @@
"to": "~/.local/bin/barg" "to": "~/.local/bin/barg"
} }
}, },
"addpaths": {
"link": {
"from": "shared/addpaths",
"to": "~/.local/bin/addpaths"
}
},
"htop": { "htop": {
"link": { "link": {
"from": "shared/htop", "from": "shared/htop",
@@ -131,6 +137,7 @@
"linux-vm": [ "linux-vm": [
"bin", "bin",
"barg", "barg",
"addpaths",
"gitignore", "gitignore",
{ {
"package": "htop", "package": "htop",