summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorflu0r1ne <flu0r1ne@flu0r1ne.net>2022-06-20 13:23:40 -0500
committerflu0r1ne <flu0r1ne@flu0r1ne.net>2022-06-20 13:23:40 -0500
commit81d574b2ea7c88812e23661dd5f53c42e763f438 (patch)
treee70dfaf2b29d00d51ae4628b6366dbc03b1d357e
downloadgit-config-profile-81d574b2ea7c88812e23661dd5f53c42e763f438.tar.xz
git-config-profile-81d574b2ea7c88812e23661dd5f53c42e763f438.zip
Add git-config-profile
-rw-r--r--Makefile20
-rwxr-xr-xgit-config-profile93
2 files changed, 113 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..5885135
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,20 @@
+ifeq ($(PREFIX),)
+ PREFIX := /usr/local
+endif
+
+ifeq ($(BINDIR),)
+ BINDIR := /bin
+endif
+
+CMD=git-config-profile
+
+make:
+
+install:
+ mkdir -p $(DESTDIR)$(PREFIX)$(BINDIR)/
+ install -m 755 $(CMD) $(DESTDIR)$(PREFIX)$(BINDIR)/
+
+uninstall:
+ rm -rf $(DESTDIR)$(PREFIX)$(BINDIR)/$(CMD)
+
+.PHONY: install uninstall make
diff --git a/git-config-profile b/git-config-profile
new file mode 100755
index 0000000..d4be154
--- /dev/null
+++ b/git-config-profile
@@ -0,0 +1,93 @@
+#!/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=""
+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
+ 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] (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";
+ else
+ die_usage;
+ fi
+
+else
+ die_usage;
+fi
+
+. /etc/default/git-config-profiles
+
+for profile in ${PROFILES[@]}; do
+ echo "${profile}";
+done