Skip to content

Commit 1b554af

Browse files
mcnaveennicktrn
andauthored
fix sed handling in macOS (#7)
* fix(lib): 🐛 fix sed handling in macOS Resolves #6 * fix(lib): ♻️ refactor function for better way of working * remove dangerous generate_secrets call * lint * only make a single sed call and simplify error handling * put backup back in --------- Co-authored-by: nicktrn <[email protected]>
1 parent e367f1c commit 1b554af

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

lib.sh

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,38 @@ docker_compose() {
1414
set +x
1515
}
1616

17+
sed_cmd() {
18+
# Determine the sed in-place flag based on the operating system
19+
if [ "$(uname)" = "Darwin" ]; then
20+
sed -i '' "$@"
21+
else
22+
sed -i "$@"
23+
fi
24+
}
25+
26+
write_secrets() {
27+
env_file="$1"
28+
29+
sed_cmd \
30+
-e "s|^MAGIC_LINK_SECRET=.*|MAGIC_LINK_SECRET=$MAGIC_LINK_SECRET|" \
31+
-e "s|^SESSION_SECRET=.*|SESSION_SECRET=$SESSION_SECRET|" \
32+
-e "s|^ENCRYPTION_KEY=.*|ENCRYPTION_KEY=$ENCRYPTION_KEY|" \
33+
-e "s|^PROVIDER_SECRET=.*|PROVIDER_SECRET=$PROVIDER_SECRET|" \
34+
-e "s|^COORDINATOR_SECRET=.*|COORDINATOR_SECRET=$COORDINATOR_SECRET|" \
35+
"$env_file"
36+
37+
if [ $? -ne 0 ]; then
38+
return 1
39+
fi
40+
41+
echo "Wrote secrets to $(basename "$env_file")"
42+
}
43+
1744
generate_secrets() {
1845
env_file=$1
1946

2047
echo "Generated secrets:"
21-
48+
2249
MAGIC_LINK_SECRET=$(openssl rand -hex 16)
2350
echo MAGIC_LINK_SECRET="$MAGIC_LINK_SECRET"
2451

@@ -34,14 +61,6 @@ generate_secrets() {
3461
COORDINATOR_SECRET=$(openssl rand -hex 32)
3562
echo COORDINATOR_SECRET="$COORDINATOR_SECRET"
3663

37-
write_secrets() {
38-
sed -i "s/MAGIC_LINK_SECRET=.*/MAGIC_LINK_SECRET=$MAGIC_LINK_SECRET/" "$env_file"
39-
sed -i "s/SESSION_SECRET=.*/SESSION_SECRET=$SESSION_SECRET/" "$env_file"
40-
sed -i "s/ENCRYPTION_KEY=.*/ENCRYPTION_KEY=$ENCRYPTION_KEY/" "$env_file"
41-
sed -i "s/PROVIDER_SECRET=.*/PROVIDER_SECRET=$PROVIDER_SECRET/" "$env_file"
42-
sed -i "s/COORDINATOR_SECRET=.*/COORDINATOR_SECRET=$COORDINATOR_SECRET/" "$env_file"
43-
}
44-
4564
if [ -z "$env_file" ]; then
4665
return
4766
fi
@@ -56,9 +75,11 @@ generate_secrets() {
5675
* )
5776
env_example_file=$(dirname "$env_file")/.env.example
5877
cp -v "$env_example_file" "$env_file"
59-
78+
6079
echo "Writing secrets to $(basename "$env_file")"
61-
write_secrets
80+
if ! write_secrets "$env_file"; then
81+
return 1
82+
fi
6283
;;
6384
esac
6485
fi
@@ -70,12 +91,16 @@ generate_secrets() {
7091
echo "Skipped writing secrets. You may want to add them manually to $(basename "$env_file")"
7192
;;
7293
* )
94+
if ! cp "$env_file" "$env_file.backup"; then
95+
echo "Failed to backup $(basename "$env_file")"
96+
return 1
97+
fi
98+
echo "Wrote backup to $(basename "$env_file").backup"
99+
73100
echo "Overwriting secrets in $(basename "$env_file")"
74-
cp -v "$env_file" "$env_file.backup"
75-
write_secrets
76-
echo "Done. Backup written to: $(basename "$env_file").backup"
101+
if ! write_secrets "$env_file"; then
102+
return 1
103+
fi
77104
;;
78105
esac
79-
80-
81-
}
106+
}

start.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ if [ ! -f "$env_file" ]; then
3636
echo "Skipping secret generation. You should really not skip this step."
3737
;;
3838
* )
39-
generate_secrets "$env_file"
39+
if ! generate_secrets "$env_file"; then
40+
echo "Failed to generate secrets. Exiting."
41+
exit 1
42+
fi
4043
sleep 2
4144
;;
4245
esac
@@ -52,4 +55,4 @@ else
5255
extra_args="-p=trigger-$kind"
5356
fi
5457

55-
docker_compose -f "$compose_file" "$extra_args" up "$@"
58+
docker_compose -f "$compose_file" "$extra_args" up "$@"

0 commit comments

Comments
 (0)