Skip to content

Commit 10540e6

Browse files
authored
Merge pull request #88 from ethpandaops/fix/home-dir-expansion
fix(install): ensure home dir expansion works as expected
2 parents 0e18c1f + a8ef53e commit 10540e6

File tree

3 files changed

+144
-18
lines changed

3 files changed

+144
-18
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ contributoor update # Update to latest version
9191
contributoor logs # Show logs
9292
```
9393

94+
If you chose to install contributoor under a custom directory, you will need to specify the directory when running the commands, for example:
95+
96+
```bash
97+
contributoor --config-path /path/to/contributoor start
98+
```
99+
94100
## 🔨 Development
95101

96102
<details>

install.bats

+91-9
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,11 @@ EOF
10261026
*) return 0 ;;
10271027
esac
10281028
}
1029-
export -f detect_platform sudo docker
1029+
function contributoor() {
1030+
echo "Config Path : $TEST_DIR/.contributoor/config.yaml"
1031+
return 0
1032+
}
1033+
export -f detect_platform sudo docker contributoor
10301034
export HOME="$TEST_DIR"
10311035

10321036
# Test 'no' response
@@ -1064,9 +1068,14 @@ EOF
10641068
case "$2" in
10651069
"systemctl") return 0 ;;
10661070
"docker") return 0 ;;
1071+
"contributoor") return 0 ;;
10671072
*) command "$@" ;;
10681073
esac
10691074
}
1075+
contributoor() {
1076+
echo "Config Path : $TEST_DIR/.contributoor/config.yaml"
1077+
return 0
1078+
}
10701079
sudo() {
10711080
case "$1" in
10721081
"systemctl")
@@ -1098,7 +1107,7 @@ EOF
10981107
*) return 0 ;;
10991108
esac
11001109
}
1101-
export -f detect_platform command sudo docker
1110+
export -f detect_platform command sudo docker contributoor
11021111
11031112
printf "y\n" | uninstall
11041113
'
@@ -1134,6 +1143,16 @@ EOF
11341143
11351144
# Setup mock functions
11361145
detect_platform() { echo "darwin"; }
1146+
command() {
1147+
case "$2" in
1148+
"contributoor") return 0 ;;
1149+
*) return 1 ;;
1150+
esac
1151+
}
1152+
contributoor() {
1153+
echo "Config Path : $TEST_DIR/.contributoor/config.yaml"
1154+
return 0
1155+
}
11371156
sudo() {
11381157
case "$1" in
11391158
"launchctl")
@@ -1153,7 +1172,7 @@ EOF
11531172
;;
11541173
esac
11551174
}
1156-
export -f detect_platform sudo
1175+
export -f detect_platform command sudo contributoor
11571176
11581177
printf "y\n" | uninstall
11591178
'
@@ -1165,32 +1184,95 @@ EOF
11651184
}
11661185

11671186
@test "uninstall handles missing components gracefully" {
1168-
# Create minimal test environment.
1169-
mkdir -p "$TEST_DIR"
1187+
# Create minimal test environment with config path
1188+
mkdir -p "$TEST_DIR/.contributoor"
1189+
touch "$TEST_DIR/.contributoor/config.yaml"
11701190

1171-
# Setup mock functions.
1191+
# Setup mock functions
11721192
function detect_platform() { echo "linux"; }
11731193
function command() {
11741194
case "$2" in
11751195
"systemctl") return 1 ;; # systemd not available
11761196
"docker") return 1 ;; # docker not available
1197+
"contributoor") return 0 ;;
11771198
*) command "$@" ;;
11781199
esac
11791200
}
1201+
function contributoor() {
1202+
echo "Config Path : $TEST_DIR/.contributoor/config.yaml"
1203+
return 0
1204+
}
11801205
function sudo() {
11811206
"${@:2}"
11821207
return 0
11831208
}
1184-
export -f detect_platform command sudo
1209+
export -f detect_platform command sudo contributoor
11851210
export HOME="$TEST_DIR"
11861211

1187-
# Run uninstall with 'yes' response.
1212+
# Run uninstall with 'yes' response
11881213
run bash -c '
11891214
source ./install.sh
11901215
printf "y\n" | uninstall
11911216
'
11921217

1193-
# Should complete successfully even with nothing to clean.
1218+
# Should complete successfully even with nothing to clean
11941219
[ "$status" -eq 0 ]
11951220
echo "$output" | grep -q "Contributoor has been uninstalled successfully"
1221+
}
1222+
1223+
@test "installation path expands tilde correctly" {
1224+
# Setup test environment
1225+
export HOME="$TEST_DIR"
1226+
CUSTOM_PATH="~/custom/contributoor"
1227+
EXPECTED_PATH="$TEST_DIR/custom/contributoor"
1228+
1229+
run bash -c "
1230+
source ./install.sh
1231+
CUSTOM_PATH='$CUSTOM_PATH'
1232+
# Expand ~ to \$HOME in a portable way
1233+
CUSTOM_PATH=\$(echo \"\$CUSTOM_PATH\" | sed \"s|^~|\$HOME|\")
1234+
echo \$CUSTOM_PATH
1235+
"
1236+
1237+
[ "$status" -eq 0 ]
1238+
[ "$output" = "$EXPECTED_PATH" ]
1239+
}
1240+
1241+
@test "uninstall handles tilde expansion in config path" {
1242+
# Create test environment with config in custom path
1243+
CUSTOM_DIR="$TEST_DIR/custom/contributoor"
1244+
mkdir -p "$CUSTOM_DIR"
1245+
touch "$CUSTOM_DIR/config.yaml"
1246+
export HOME="$TEST_DIR"
1247+
1248+
# Run uninstall with config path using tilde
1249+
run bash -c '
1250+
source ./install.sh
1251+
CONFIG_PATH="~/custom/contributoor/config.yaml"
1252+
detect_platform() { echo "linux"; }
1253+
command() { return 1; }
1254+
export -f detect_platform command
1255+
printf "y\n" | CONFIG_PATH="$CONFIG_PATH" uninstall
1256+
'
1257+
1258+
# Should expand ~ and find the config
1259+
[ "$status" -eq 0 ]
1260+
echo "$output" | grep -q "custom/contributoor"
1261+
[ ! -d "$CUSTOM_DIR" ]
1262+
}
1263+
1264+
@test "uninstall fails with non-existent config path after tilde expansion" {
1265+
export HOME="$TEST_DIR"
1266+
1267+
run bash -c '
1268+
source ./install.sh
1269+
CONFIG_PATH="~/nonexistent/config.yaml"
1270+
detect_platform() { echo "linux"; }
1271+
command() { return 1; }
1272+
export -f detect_platform command
1273+
printf "y\n" | CONFIG_PATH="$CONFIG_PATH" uninstall
1274+
'
1275+
1276+
[ "$status" -eq 1 ]
1277+
echo "$output" | grep -q "Config file not found at: $TEST_DIR/nonexistent/config.yaml"
11961278
}

install.sh

+47-9
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ warn() {
8989
}
9090

9191
usage() {
92-
echo "Usage: $0 [-p path] [-v version] [-u]"
92+
echo "Usage: $0 [-p path] [-v version] [-u] [-c config_path]"
9393
echo " -p: Path to install contributoor (default: $HOME/.contributoor)"
9494
echo " -v: Version of contributoor to install without 'v' prefix (default: latest, example: 0.0.6)"
9595
echo " -u: Uninstall Contributoor"
96+
echo " -c: Path to config.yaml (only used with -u for uninstall)"
9697
exit 1
9798
}
9899

@@ -599,11 +600,37 @@ EOF
599600
}
600601

601602
uninstall() {
603+
# Try to determine the installation directory
604+
local install_dir=""
605+
606+
# If config path was provided via -c, use it
607+
if [ -n "${CONFIG_PATH:-}" ]; then
608+
# Expand ~ to $HOME in a portable way
609+
CONFIG_PATH=$(echo "$CONFIG_PATH" | sed "s|^~|$HOME|")
610+
if [ ! -f "$CONFIG_PATH" ]; then
611+
fail "Config file not found at: $CONFIG_PATH"
612+
fi
613+
install_dir=$(dirname "$CONFIG_PATH")
614+
else
615+
# Try using contributoor status
616+
if command -v contributoor >/dev/null 2>&1; then
617+
local config_path=$(contributoor status 2>/dev/null | grep "Config Path" | cut -d':' -f2 | tr -d ' ')
618+
if [ -n "$config_path" ]; then
619+
install_dir=$(dirname "$config_path")
620+
fi
621+
fi
622+
623+
# If we couldn't determine the install directory, ask user to specify.
624+
if [ -z "$install_dir" ]; then
625+
fail "Could not determine installation directory.\nIt's likely you installed contributoor under a custom directory.\n\nPlease specify the directory when running the uninstaller:\n./install.sh -u -c /path/to/custom/config.yaml"
626+
fi
627+
fi
628+
602629
printf "\n${COLOR_RED}Warning, this will:${COLOR_RESET}\n"
603630
printf " • Stop and remove any contributoor services (systemd/launchd)\n"
604631
printf " • Stop and remove any contributoor Docker containers and images\n"
605632
printf " • Remove contributoor from your PATH\n"
606-
printf " • Delete all contributoor data from ${HOME}/.contributoor\n\n"
633+
printf " • Delete all contributoor data from ${install_dir}\n\n"
607634
printf "Are you sure you want to uninstall? [y/N]: "
608635
read -r confirm
609636
case "$(echo "$confirm" | tr '[:upper:]' '[:lower:]')" in
@@ -663,19 +690,20 @@ uninstall() {
663690
fi
664691

665692
# Remove PATH entry from shell config
693+
local bin_path="$install_dir/bin"
666694
for rc in "$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.bash_profile" "$HOME/.profile"; do
667695
if [ -f "$rc" ]; then
668696
temp_file=$(mktemp)
669-
grep -v "export PATH=.*contributoor.*bin" "$rc" > "$temp_file"
697+
grep -v "export PATH=.*$bin_path" "$rc" > "$temp_file"
670698
mv "$temp_file" "$rc"
671699
success "Cleaned PATH from $rc"
672700
fi
673701
done
674702

675-
# Remove contributoor directory
676-
if [ -d "$HOME/.contributoor" ]; then
677-
rm -rf "$HOME/.contributoor"
678-
success "Removed contributoor directory"
703+
# Remove contributoor directory using detected path
704+
if [ -d "$install_dir" ]; then
705+
rm -rf "$install_dir"
706+
success "Removed contributoor directory: $install_dir"
679707
fi
680708

681709
printf "\n\n${COLOR_GREEN}Contributoor has been uninstalled successfully${COLOR_RESET}\n\n"
@@ -684,16 +712,24 @@ uninstall() {
684712

685713
main() {
686714
# Parse arguments
687-
while getopts "p:v:hu" FLAG; do
715+
SHOULD_UNINSTALL=false
716+
while getopts "p:v:c:hu" FLAG; do
688717
case "$FLAG" in
689718
p) CONTRIBUTOOR_PATH="$OPTARG" ;;
690719
v) CONTRIBUTOOR_VERSION="$OPTARG" ;;
691-
u) uninstall ;;
720+
c) CONFIG_PATH="$OPTARG" ;;
721+
u) SHOULD_UNINSTALL=true ;;
692722
h) usage ;;
693723
*) usage ;;
694724
esac
695725
done
696726

727+
# Handle uninstall if requested
728+
if [ "$SHOULD_UNINSTALL" = true ]; then
729+
uninstall
730+
exit 0
731+
fi
732+
697733
# Setup environment
698734
CONTRIBUTOOR_BIN="$CONTRIBUTOOR_PATH/bin"
699735
ARCH=$(detect_architecture)
@@ -726,6 +762,8 @@ main() {
726762
read -r CUSTOM_PATH
727763
fi
728764
if [ -n "$CUSTOM_PATH" ]; then
765+
# Expand ~ to $HOME in a portable way
766+
CUSTOM_PATH=$(echo "$CUSTOM_PATH" | sed "s|^~|$HOME|")
729767
CONTRIBUTOOR_PATH="$CUSTOM_PATH"
730768
CONTRIBUTOOR_BIN="$CONTRIBUTOOR_PATH/bin"
731769
fi

0 commit comments

Comments
 (0)