Skip to content

Commit

Permalink
feat: devcontainer part 4 (#3339)
Browse files Browse the repository at this point in the history
add utils.sh, prelim docs

Signed-off-by: Dave Lee <[email protected]>
  • Loading branch information
dave-gray101 authored Aug 20, 2024
1 parent 6aba622 commit 9cfd890
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
49 changes: 49 additions & 0 deletions .devcontainer-scripts/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

# This file contains some really simple functions that are useful when building up customization scripts.


# Checks if the git config has a user registered - and sets it up if not.
#
# Param 1: name
# Param 2: email
#
config_user() {
local gcn=$(git config --global user.name)
if [ -z "${gcn}" ]; then
echo "Setting up git user / remote"
git config --global user.name "$1"
git config --global user.email "$2"

fi
}

# Checks if the git remote is configured - and sets it up if not. Fetches either way.
#
# Param 1: remote name
# Param 2: remote url
#
config_remote() {
local gr=$(git remote -v | grep $1)
if [ -z "${gr}" ]; then
git remote add $1 $2
fi
git fetch $1
}

# Setup special .ssh files
#
# Param 1: bash array, filenames relative to the customization directory that should be copied to ~/.ssh
setup_ssh() {
local files=("$@")
for file in "${files[@]}"; then
local cfile="/devcontainer-customization/${file}"
local hfile="~/.ssh/${file}"
if [ ! -f "${hfile}" ]; then
echo "copying ${file}"
cp "${cfile}" "${hfile}"
chmod 600 "${hfile}"
fi
done
ls ~/.ssh
}
19 changes: 12 additions & 7 deletions .devcontainer/customization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ If files with those names exist here, they will be called at the end of the norm

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.

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

```
#!/bin/bash
gcn=$(git config --global user.name)
if [ -z "$gcn" ]; then
git config --global user.name YOUR.NAME
git config --global user.email YOUR.EMAIL
git remote add PREFIX FORK_URL
fi
source "/.devcontainer-scripts/utils.sh"
sshfiles=("config", "key.pub")
setup_ssh "${sshfiles[@]}"
config_user "YOUR NAME" "YOUR EMAIL"
config_remote "REMOTE NAME" "REMOTE URL"
```

0 comments on commit 9cfd890

Please sign in to comment.