-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_workspaces.zsh
executable file
·102 lines (86 loc) · 3.42 KB
/
setup_workspaces.zsh
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
#!/usr/bin/env zsh
#
# Generate (git) workspaces for personal and work related coding projects.
#
# This script is driven by a config file that is stored outside of this git project
# (since it contains confidential data, like company names and email addresses).
# An example config file can be found in .dotfiles/resources/workspaces.conf.yaml.
#
# You can create additional workspaces afterwards with the 'mkws' alias.
#
# This script uses 'yq' (https://mikefarah.gitbook.io/yq/) to parse the config yaml file.
echo "\n<<< Setup workspaces >>>\n"
WORKSPACES_PREFIX="$HOME/workspaces"
WORKSPACES_CONFIG="$HOME/Dropbox/Apps/dotfiles/workspaces.conf.yaml"
GIT_CONFIG_GLOBAL="$HOME/.gitconfig"
# Each workspace gets its own git config file (with client specific git credentials)
# https://dzone.com/articles/how-to-use-gitconfigs-includeif
# https://stackoverflow.com/questions/4220416/can-i-specify-multiple-users-for-myself-in-gitconfig/43654115#43654115
# https://stackoverflow.com/a/43654115
update_global_gitconfig() {
WORKSPACE_NAME="$1"
WORKSPACE="$2"
if [ ! -f "$GIT_CONFIG_GLOBAL" ]; then
echo "Creating file: $GIT_CONFIG_GLOBAL"
touch "$GIT_CONFIG_GLOBAL"
fi
SEARCH_STRING="/$WORKSPACE_NAME/"
if grep -Fq "$SEARCH_STRING" "$GIT_CONFIG_GLOBAL"; then
echo "Not updating global ~/.gitconfig. It already contains a reference to workspace '$WORKSPACE_NAME'."
else
echo "Updating file: ~/.gitconfig"
# https://git-scm.com/docs/git-config#Documentation/git-config.txt-codegitdircode
cat <<EOF >> "$GIT_CONFIG_GLOBAL"
[includeIf "gitdir:$WORKSPACE/"]
path = $WORKSPACE/.gitconfig
EOF
fi
}
create_workspaces_root() {
if [ ! -d "$WORKSPACES_PREFIX" ]; then
echo "Creating folder: $WORKSPACES_PREFIX"
mkdir "$WORKSPACES_PREFIX"
else
echo "Folder '$WORKSPACES_PREFIX' already exists."
fi
}
create_workspace() {
WORKSPACE_NAME="$1"
GIT_USER="$2"
GIT_EMAIL="$3"
GIT_ORG="$4"
WORKSPACE="$WORKSPACES_PREFIX/$WORKSPACE_NAME"
GIT_CONFIG="$WORKSPACE/.gitconfig"
echo "\n\nCreating workspace $WORKSPACE_NAME with Git user $GIT_USER, email $GIT_EMAIL and org $GIT_ORG.\n\n"
if [ ! -d "$WORKSPACE" ]; then
echo "Creating folder: $WORKSPACE"
mkdir "$WORKSPACE"
echo "Cloning repositories ..."
cd "$WORKSPACE"
gh repo list "$GIT_ORG" | while read -r repo _; do
printf "\n\n\n\n"
git clone git@github.com:$repo.git
done
cd ..
echo "\nAll repositories have been cloned into $WORKSPACE."
echo "Remove any repositories that you don't need now.\n\n"
vared -p "Press [Enter] when done. " -c REPLY
echo "Creating gitconfig: $GIT_CONFIG"
cat <<EOF > "$GIT_CONFIG"
[user]
name = $GIT_USER
email = $GIT_EMAIL
EOF
update_global_gitconfig $WORKSPACE_NAME $WORKSPACE
else
echo "Workspace '$WORKSPACE_NAME' already exists."
fi
}
create_workspaces_root
# Read items from a list with yq; no idea how this works but it works...
# https://stackoverflow.com/questions/62898925/using-yq-in-for-loop-bash
# https://stackoverflow.com/a/70841936
while IFS=$'\t' read -r name git_username git_email git_org _; do
echo "\n[$name]"
create_workspace $name $git_username $git_email $git_org
done < <(yq e '.workspaces[] | [.name, .git.username, .git.email, .git.org] | @tsv' $WORKSPACES_CONFIG)