From 81d574b2ea7c88812e23661dd5f53c42e763f438 Mon Sep 17 00:00:00 2001 From: flu0r1ne Date: Mon, 20 Jun 2022 13:23:40 -0500 Subject: Add git-config-profile --- Makefile | 20 ++++++++++++ git-config-profile | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 Makefile create mode 100755 git-config-profile 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 -- cgit v1.2.3