2023-06-18 11:50:45 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
project_dir="$(git rev-parse --show-toplevel)"
|
2024-10-27 17:07:54 +00:00
|
|
|
action="${1:-show}"
|
|
|
|
target="${2:-}"
|
|
|
|
|
|
|
|
if [ "$action" = "save" ]; then
|
|
|
|
target="" # Save all files
|
|
|
|
fi
|
2023-06-18 11:50:45 +00:00
|
|
|
|
|
|
|
if [ -z "$project_dir" ]; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2024-10-27 17:07:54 +00:00
|
|
|
cd "$project_dir" || exit 1
|
|
|
|
|
2023-06-18 11:50:45 +00:00
|
|
|
url="$(git remote get-url origin)"
|
|
|
|
|
|
|
|
re="^(https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+)(.git)*$"
|
|
|
|
|
|
|
|
if [[ $url =~ $re ]]; then
|
2024-05-12 16:38:33 +00:00
|
|
|
#protocol=${BASH_REMATCH[1]}
|
|
|
|
#separator=${BASH_REMATCH[2]}
|
|
|
|
#hostname=${BASH_REMATCH[3]}
|
|
|
|
user=${BASH_REMATCH[4]}
|
2023-06-18 11:50:45 +00:00
|
|
|
repo=$(basename -s .git "${BASH_REMATCH[5]}")
|
|
|
|
fi
|
|
|
|
|
|
|
|
project="$user/$repo"
|
|
|
|
|
|
|
|
if ! pass "ansible/$project" >/dev/null 2>&1; then
|
|
|
|
echo "Error: ansible/$project not found in pass" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-12-10 23:36:03 +00:00
|
|
|
pass_content=$(pass "ansible/$project")
|
2024-10-27 17:07:54 +00:00
|
|
|
pass_paths=()
|
|
|
|
|
|
|
|
grep_filter="^path:"
|
|
|
|
if [ -n "$target" ]; then
|
|
|
|
grep_filter="^path: $target$"
|
|
|
|
fi
|
|
|
|
while read -r pass_path; do
|
|
|
|
pass_paths+=("$pass_path")
|
|
|
|
done <<< "$(echo "$pass_content" | grep "$grep_filter" | cut -d' ' -f2-)"
|
|
|
|
|
2023-12-10 23:36:03 +00:00
|
|
|
|
2024-04-30 16:24:24 +00:00
|
|
|
case "$action" in
|
|
|
|
show)
|
|
|
|
echo "$pass_content" | head -n 1
|
|
|
|
exit 0 ;;
|
2024-10-29 21:45:13 +00:00
|
|
|
list)
|
|
|
|
echo "$pass_content" | grep "^path:" | cut -d' ' -f2-
|
|
|
|
exit 0 ;;
|
2024-04-30 16:24:24 +00:00
|
|
|
view)
|
2024-10-27 17:07:54 +00:00
|
|
|
ansible-vault view "${pass_paths[@]}"
|
2024-04-30 16:24:24 +00:00
|
|
|
exit 0 ;;
|
2023-12-10 23:36:03 +00:00
|
|
|
edit)
|
2024-10-27 17:07:54 +00:00
|
|
|
ansible-vault edit "${pass_paths[@]}"
|
2024-04-30 16:24:24 +00:00
|
|
|
exit 0 ;;
|
2023-12-10 23:36:03 +00:00
|
|
|
deploy)
|
2024-10-27 17:07:54 +00:00
|
|
|
pass show "ansible/$project.tar" | tar x
|
2024-04-30 16:24:24 +00:00
|
|
|
exit 0 ;;
|
2023-12-10 23:36:03 +00:00
|
|
|
save)
|
2024-10-27 17:07:54 +00:00
|
|
|
tar c "${pass_paths[@]}" | pass insert -m "ansible/$project.tar"
|
2024-04-30 16:24:24 +00:00
|
|
|
exit 0 ;;
|
2023-12-12 17:55:32 +00:00
|
|
|
pass-edit)
|
|
|
|
pass edit "ansible/$project"
|
2024-04-30 16:24:24 +00:00
|
|
|
exit 0 ;;
|
|
|
|
*)
|
2024-10-29 21:45:13 +00:00
|
|
|
echo "Usage: ansible-vault-manager [show|list|view|edit|deploy|save|pass-edit]"
|
2024-04-30 16:24:24 +00:00
|
|
|
exit 0 ;;
|
2023-12-10 23:36:03 +00:00
|
|
|
esac
|