Skip to content

Commit 9cfd890

Browse files
authored
feat: devcontainer part 4 (#3339)
add utils.sh, prelim docs Signed-off-by: Dave Lee <[email protected]>
1 parent 6aba622 commit 9cfd890

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

.devcontainer-scripts/utils.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
# This file contains some really simple functions that are useful when building up customization scripts.
4+
5+
6+
# Checks if the git config has a user registered - and sets it up if not.
7+
#
8+
# Param 1: name
9+
# Param 2: email
10+
#
11+
config_user() {
12+
local gcn=$(git config --global user.name)
13+
if [ -z "${gcn}" ]; then
14+
echo "Setting up git user / remote"
15+
git config --global user.name "$1"
16+
git config --global user.email "$2"
17+
18+
fi
19+
}
20+
21+
# Checks if the git remote is configured - and sets it up if not. Fetches either way.
22+
#
23+
# Param 1: remote name
24+
# Param 2: remote url
25+
#
26+
config_remote() {
27+
local gr=$(git remote -v | grep $1)
28+
if [ -z "${gr}" ]; then
29+
git remote add $1 $2
30+
fi
31+
git fetch $1
32+
}
33+
34+
# Setup special .ssh files
35+
#
36+
# Param 1: bash array, filenames relative to the customization directory that should be copied to ~/.ssh
37+
setup_ssh() {
38+
local files=("$@")
39+
for file in "${files[@]}"; then
40+
local cfile="/devcontainer-customization/${file}"
41+
local hfile="~/.ssh/${file}"
42+
if [ ! -f "${hfile}" ]; then
43+
echo "copying ${file}"
44+
cp "${cfile}" "${hfile}"
45+
chmod 600 "${hfile}"
46+
fi
47+
done
48+
ls ~/.ssh
49+
}

.devcontainer/customization/README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ If files with those names exist here, they will be called at the end of the norm
77

88
This is a good place to set things like `git config --global user.name` are set - and to handle any other files that are mounted via this directory.
99

10-
An example of a useful script might be:
10+
To assist in doing so, `source /.devcontainer-scripts/utils.sh` will provide utility functions that may be useful - for example:
1111

1212
```
1313
#!/bin/bash
14-
gcn=$(git config --global user.name)
15-
if [ -z "$gcn" ]; then
16-
git config --global user.name YOUR.NAME
17-
git config --global user.email YOUR.EMAIL
18-
git remote add PREFIX FORK_URL
19-
fi
14+
15+
source "/.devcontainer-scripts/utils.sh"
16+
17+
sshfiles=("config", "key.pub")
18+
19+
setup_ssh "${sshfiles[@]}"
20+
21+
config_user "YOUR NAME" "YOUR EMAIL"
22+
23+
config_remote "REMOTE NAME" "REMOTE URL"
24+
2025
```

0 commit comments

Comments
 (0)