summaryrefslogtreecommitdiff
path: root/git-config-profile
blob: a2f4a93b6bae63bb9e66451ddfbd4369a61c8d4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash

## GIT CONFIG PROFILE
#
# Configure git profiles
#
#  echo <<_EOF >/etc/default/git-config-profiles
#  #!/bin/bash
#  config_profile "work"     "John Doe" "jdoe@company.com" "ssh -i ~/.ssh/jdoe_work"
#  config_profile "personal" "John Doe" "jdoe@gmail.com"   "ssh -i ~/.ssh/jdoe_personal"
#  _EOF

set -eE -o pipefail

CMD_PROMPT="[#] "
PROGRAM="${0##*/}"

die () {
  if [[ $# -eq 0 ]]; then
    echo -n "$PROGRAM: " 1>&2
    cat <&0 1>&2
  elif [[ $# -eq 1 ]]; then
    echo "$PROGRAM: $1" 1>&2
  fi

  exit 1
}

cmd() {
  echo "$CMD_PROMPT $*"
  "$@"
}

PROFILES=()
REQ_PROFILE=""
INIT_REPO=""
PROFILES_FILE="/etc/default/git-config-profiles"

config_profile() {
	local profile
	local name
	local email
	local ssh_key

	profile="$1"
	name="$2"
	email="$3"
	ssh_key="$4"

	if [[ -z "${REQ_PROFILE}" ]]; then
		PROFILES+=( "${profile}" );
	elif [[ "${REQ_PROFILE}" = "${profile}" ]]; then
	
		if [[ -n "${INIT_REPO}" ]]; then
			cmd git init
		fi

		cmd git config --local user.name  "$name";
		cmd git config --local user.email "$email";

		if [[ -n "$ssh_key" ]]; then
			cmd git config --local core.sshCommand "ssh -i $ssh_key";
		fi
	fi

}

get_profile() {
	cmd git config --get user.name
	cmd git config --get user.email
	cmd git config --get core.sshCommand
	exit 0
}

die_usage() {
	die "Usage: $PROGRAM [get|list|config|init] (profile)";
}

if [[ "$#" -eq 1 && "$1" = "list" ]]; then
	REQ_PROFILE="";
elif [[ "$#" -eq 1 && "$1" = "get" ]]; then
	get_profile;
elif [[ "$#" -eq 2 ]]; then

	if [[ "$1" = "config" ]]; then
		REQ_PROFILE="$2";
	elif [[ "$1" = "init" ]]; then
		INIT_REPO=1;
		REQ_PROFILE="$2";
	else
		die_usage;
	fi

else
	die_usage;
fi

. /etc/default/git-config-profiles

for profile in ${PROFILES[@]}; do
	echo "${profile}";
done