]> git.sven.stormbind.net Git - sven/scripts.git/blob - home/mpass.sh
8d88b6d7735cc35989d351e726d83b6647aabbc7
[sven/scripts.git] / home / mpass.sh
1 #!/bin/bash
2
3 set -eu
4
5 # since we're running with nounset we've to ensure all variables exist
6 repo=""
7
8 function help {
9     cat <<EOF
10     ${0} [ -r repository ] [ pull | push | pass-command ]
11
12     mpass is a thin wrapper around pass to ease working with
13     multiple repository.
14
15     Before you get started create a configuration file in
16     ${HOME}/.mpass. This fill must contain an associative bash array
17     named "REPO_STORE_DIR[reponame]" to define your repository location.
18     The default repoistory should be named "default" is used when no
19     other repository name is defined with "-r" on the mpass invocation.
20
21     Example:
22       REPO_STORE_DIR[default]="/home/sven/pass"
23       REPO_STORE_DIR[foo]="/home/sven/pass-foo"
24
25     
26     Commands:
27
28     pull: executes a "git pull" in the configured REPO_STORE_DIR[reponame]
29     push: executes a "git push" in the configured REPO_STORE_DIR[reponame]
30
31 EOF
32     exit 42
33 }
34
35 function initialize {
36     # try to source our configuration
37     if [ -f "${HOME}/.mpass" ]; then
38         # declare all known configuration options as an associative array first
39         declare -A REPO_STORE_DIR
40         source ${HOME}/.mpass
41     else
42         echo "ERROR: Could not find mpass configuration in ${HOME}/.mpass" >&2
43         help
44     fi
45
46     # in case no repository is defined, assume it's "default"
47     if [ -z "${repo}" ]; then
48         repo="default"
49     fi
50
51     # define pass repo location
52     # this variable is also used by pass to decide where to operate
53     export PASSWORD_STORE_DIR="${REPO_STORE_DIR[${repo}]}"
54
55     # check if we actually have something that could be a repository configuraton
56     if [ -z "${PASSWORD_STORE_DIR}" ]; then
57         echo "ERROR: No valid repository configuration found for repo ${repo}" >&2
58         help
59     fi
60
61     # try to find out if we've a repository at hand, otherwise issue a warning
62     # this could still result in an error, but is perfectly fine on an "init" command
63     if ! [ -d "${PASSWORD_STORE_DIR}" ]; then
64         echo "WARNING: Given pass repo ${PASSWORD_STORE_DIR} does not exist" >&2
65     fi
66 }
67
68 while getopts ":r:" option; do
69   case $option in
70     r)
71         repo="${OPTARG}"
72         shift $((OPTIND-1))
73         ;;
74     \?)
75         echo "ERROR: Invalid option: -$OPTARG" >&2
76         help
77         ;;
78     :)
79         echo "ERROR: Option -$OPTARG requires an argument" >&2
80         help
81         ;;
82   esac
83 done
84
85 if [[ -z "${@}" ]]; then
86     echo "ERROR: no commands given" >&2
87     help
88 fi
89
90 # read our configuration
91 initialize
92
93 case $1 in
94     pull)
95         echo "Trying to update pass repo ${PASSWORD_STORE_DIR}"
96         cd ${PASSWORD_STORE_DIR}
97         set +e
98         git pull
99         set -e
100         cd -
101         ;;
102     push)
103         echo "Trying to push pass repo ${PASSWORD_STORE_DIR}"
104         cd ${PASSWORD_STORE_DIR}
105         set +e
106         git push
107         set -e
108         cd -
109         ;;
110     *)
111         echo "Remaining pass commands: $@"
112         pass ${@}
113         ;;
114 esac