Skip to content

Commit

Permalink
fix: check if init for pass, passage commands
Browse files Browse the repository at this point in the history
  • Loading branch information
entelecheia committed Apr 12, 2023
1 parent c05815e commit 9f50bae
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 46 deletions.
72 changes: 43 additions & 29 deletions chezmoi/dot_local/bin/executable_pass-insert.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,72 @@
if [[ $# -eq 0 ]]; then
log_info "Usage: $0 pass-name [password]"
exit 0
fi
fi

PASS_NAME=${1:-""}
PASSWORD=${2:-""}
AGE_KEY_FILE=${AGE_KEY_FILE:-"{{ .age.identity_file }}"}

if [[ -z "${PASS_NAME}" ]]; then
log_info "Usage: $0 pass-name [password]"
exit 0
fi

if pass --version &>/dev/null; then
# check if pass-name is already in the password-store
if pass "${PASS_NAME}" &>/dev/null; then
EXISTING_PASSWORD="$(pass "${PASS_NAME}")"
if [[ -n "${PASSWORD}" ]]; then
if [[ "${EXISTING_PASSWORD}" != "${PASSWORD}" ]]; then
log_task "Updating password for [${PASS_NAME}] in password-store"
if [ -d "${PASSWORD_STORE_DIR}" ] && [ -e "${PASSWORD_STORE_DIR}/.gpg-id" ]; then

# check if pass-name is already in the password-store
if pass "${PASS_NAME}" &>/dev/null; then
EXISTING_PASSWORD="$(pass "${PASS_NAME}")"
if [[ -n "${PASSWORD}" ]]; then
if [[ "${EXISTING_PASSWORD}" != "${PASSWORD}" ]]; then
log_task "Updating password for [${PASS_NAME}] in password-store"
echo "${PASSWORD}" | pass insert -m "${PASS_NAME}"
else
log_info "[${PASS_NAME}] already exists in password-store"
fi
fi
else
log_task "Inserting password of [${PASS_NAME}] into password-store"
if [[ -n "${PASSWORD}" ]]; then
echo "${PASSWORD}" | pass insert -m "${PASS_NAME}"
else
log_info "[${PASS_NAME}] already exists in password-store"
pass insert -m "${PASS_NAME}"
fi
fi
PASSWORD="$(pass "${PASS_NAME}")"
else
log_task "Inserting password of [${PASS_NAME}] into password-store"
if [[ -n "${PASSWORD}" ]]; then
echo "${PASSWORD}" | pass insert -m "${PASS_NAME}"
else
pass insert -m "${PASS_NAME}"
fi
log_info "Password-store is not initialized"
fi
PASSWORD="$(pass "${PASS_NAME}")"
else
log_info "pass is not installed"
fi

if passage --version &>/dev/null; then
# check if pass-name is already in the passage store
if passage "${PASS_NAME}" &>/dev/null; then
EXISTING_PASSWORD="$(passage "${PASS_NAME}")"
if [[ -n "${PASSWORD}" ]]; then
if [[ "${EXISTING_PASSWORD}" != "${PASSWORD}" ]]; then
log_task "Updating password for [${PASS_NAME}] in passage store"
if [ -d "${PASSAGE_DIR}" ] && [ -e "${AGE_KEY_FILE}" ]; then
# check if pass-name is already in the passage store
if passage "${PASS_NAME}" &>/dev/null; then
EXISTING_PASSWORD="$(passage "${PASS_NAME}")"
if [[ -n "${PASSWORD}" ]]; then
if [[ "${EXISTING_PASSWORD}" != "${PASSWORD}" ]]; then
log_task "Updating password for [${PASS_NAME}] in passage store"
echo "${PASSWORD}" | passage insert -m "${PASS_NAME}"
else
log_info "[${PASS_NAME}] already exists in passage store"
fi
fi
else
log_task "Inserting password of [${PASS_NAME}] into passage store"
if [[ -n "${PASSWORD}" ]]; then
echo "${PASSWORD}" | passage insert -m "${PASS_NAME}"
else
log_info "[${PASS_NAME}] already exists in passage store"
passage insert -m "${PASS_NAME}"
fi
fi
PASSWORD="$(passage "${PASS_NAME}")"
else
log_task "Inserting password of [${PASS_NAME}] into passage store"
if [[ -n "${PASSWORD}" ]]; then
echo "${PASSWORD}" | passage insert -m "${PASS_NAME}"
else
passage insert -m "${PASS_NAME}"
fi
log_info "Passage is not initialized"
fi
PASSWORD="$(passage "${PASS_NAME}")"
else
log_info "passage is not installed"
fi
21 changes: 15 additions & 6 deletions chezmoi/dot_local/bin/executable_pass-pull.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ if ! gh auth status &>/dev/null; then
fi

# pull pass changes from remote
if git -C "${PASSWORD_STORE_DIR}" remote get-url origin >/dev/null 2>&1; then
log_task "Pulling pass changes from remote"
git -C "${PASSWORD_STORE_DIR}" pull
if [ -d "${PASSWORD_STORE_DIR}" ] && [ -e "${PASSWORD_STORE_DIR}/.gpg-id" ]; then
if git -C "${PASSWORD_STORE_DIR}" remote get-url origin >/dev/null 2>&1; then
log_task "Pulling pass changes from remote"
git -C "${PASSWORD_STORE_DIR}" pull
fi
else
log_info "Password-store is not initialized"
fi

# pull passage changes from remote
if git -C "${PASSAGE_DIR}" remote get-url origin >/dev/null 2>&1; then
log_task "Pulling passage changes from remote"
git -C "${PASSAGE_DIR}" pull
AGE_KEY_FILE=${AGE_KEY_FILE:-"{{ .age.identity_file }}"}
if [ -d "${PASSAGE_DIR}" ] && [ -e "${AGE_KEY_FILE}" ]; then
if git -C "${PASSAGE_DIR}" remote get-url origin >/dev/null 2>&1; then
log_task "Pulling passage changes from remote"
git -C "${PASSAGE_DIR}" pull
fi
else
log_info "Passage is not initialized"
fi
23 changes: 16 additions & 7 deletions chezmoi/dot_local/bin/executable_pass-push.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@
# {{ template "scripts-library" }}

# push pass changes to remote
if git -C "${PASSWORD_STORE_DIR}" remote get-url origin >/dev/null 2>&1; then
log_task "Committing pass changes and pushing to remote"
c pass git push
if [ -d "${PASSWORD_STORE_DIR}" ] && [ -e "${PASSWORD_STORE_DIR}/.gpg-id" ]; then
if git -C "${PASSWORD_STORE_DIR}" remote get-url origin >/dev/null 2>&1; then
log_task "Committing pass changes and pushing to remote"
c pass git push
fi
else
log_info "Password-store is not initialized"
fi

# push passage changes to remote
if git -C "${PASSAGE_DIR}" remote get-url origin >/dev/null 2>&1; then
log_task "Committing passage changes and pushing to remote"
c passage git push
# pull passage changes from remote
AGE_KEY_FILE=${AGE_KEY_FILE:-"{{ .age.identity_file }}"}
if [ -d "${PASSAGE_DIR}" ] && [ -e "${AGE_KEY_FILE}" ]; then
if git -C "${PASSAGE_DIR}" remote get-url origin >/dev/null 2>&1; then
log_task "Committing passage changes and pushing to remote"
c passage git push
fi
else
log_info "Passage is not initialized"
fi
23 changes: 19 additions & 4 deletions chezmoi/dot_local/bin/executable_pass-sync.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@
set -eou pipefail
# {{ template "scripts-library" }}

AGE_KEY_FILE=${AGE_KEY_FILE:-"{{ .age.identity_file }}"}

if [ ! -d "${PASSWORD_STORE_DIR}" ] || [ ! -e "${PASSWORD_STORE_DIR}/.gpg-id" ] || [ ! -d "${PASSAGE_DIR}" ] || [ ! -e "${AGE_KEY_FILE}" ]; then
log_info "Password-store or Passage is not initialized"
exit 0
fi

# sync from pass to passage
log_task "Syncing from pass to passage"
cd "${PASSWORD_STORE_DIR:-$HOME/.password-store}"
while read -r -d "" passfile; do
name="${passfile#./}"; name="${name%.gpg}"
name="${passfile#./}"
name="${name%.gpg}"
[[ -f "${PASSAGE_DIR:-$HOME/.passage/store}/$name.age" ]] && continue
pass "$name" | passage insert -m "$name" || { echo "Exisintg... Skip to insert $name"; continue; }
pass "$name" | passage insert -m "$name" || {
echo "Exisintg... Skip to insert $name"
continue
}
done < <(find . -path '*/.git' -prune -o -iname '*.gpg' -print0)

# sync from passage to pass
log_task "Syncing from passage to pass"
cd "${PASSAGE_DIR:-$HOME/.passage/store}"
while read -r -d "" passagefile; do
name="${passagefile#./}"; name="${name%.age}"
name="${passagefile#./}"
name="${name%.age}"
[[ -f "${PASSWORD_STORE_DIR:-$HOME/.password-store}/$name.gpg" ]] && continue
passage "$name" | pass insert -m "$name" || { echo "Exisintg... Skip to insert $name"; continue; }
passage "$name" | pass insert -m "$name" || {
echo "Exisintg... Skip to insert $name"
continue
}
done < <(find . -path '*/.git' -prune -o -iname '*.age' -print0)

0 comments on commit 9f50bae

Please sign in to comment.