Last active
May 4, 2025 17:09
-
-
Save divyam234/f55351b6ecdbfc543dc12d6b90683c48 to your computer and use it in GitHub Desktop.
fix docker paths android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
target_dir="$1" | |
raw_path_prefix="$2" | |
target_paths=( | |
"/var/run/" | |
"/var/lib/" | |
"/run/" | |
"/usr/lib/" | |
"/opt/" | |
) | |
if [[ -z "$target_dir" || ! -d "$target_dir" ]]; then | |
echo "Error: Target directory '$target_dir' not provided or not found." | |
echo "Usage: $0 <directory> <path_prefix>" | |
exit 1 | |
fi | |
if [[ -z "$raw_path_prefix" ]]; then | |
echo "Error: Path prefix not provided." | |
echo "Usage: $0 <directory> <path_prefix>" | |
exit 1 | |
fi | |
path_prefix="${raw_path_prefix%/}" | |
if [[ -z "$path_prefix" ]]; then | |
path_prefix="/" | |
fi | |
replace_paths() { | |
local file="$1" | |
for target_path in "${target_paths[@]}"; do | |
if grep -q -F "\"${target_path}" "$file"; then | |
replacement_suffix="" | |
needs_trimming=false | |
if [[ "$target_path" == "/usr/"* ]]; then | |
replacement_suffix="${target_path#/usr/}" | |
needs_trimming=true | |
elif [[ "$target_path" == "/var/"* ]]; then | |
replacement_suffix="${target_path#/var/}" | |
needs_trimming=true | |
else | |
replacement_suffix="${target_path}" | |
needs_trimming=false | |
fi | |
final_replacement_path="" | |
if [[ "$needs_trimming" = true ]]; then | |
if [[ "$path_prefix" == "/" ]]; then | |
final_replacement_path="/${replacement_suffix}" | |
else | |
final_replacement_path="${path_prefix}/${replacement_suffix}" | |
fi | |
else | |
if [[ "$path_prefix" == "/" ]]; then | |
final_replacement_path="${replacement_suffix}" | |
else | |
final_replacement_path="${path_prefix}${replacement_suffix}" | |
fi | |
fi | |
sed_command="s#\"${target_path}#\"${final_replacement_path}#g" | |
sed -i "$sed_command" "$file" | |
fi | |
done | |
} | |
replace_sockets(){ | |
local file="$1" | |
if grep -q "unix:///var/run" "$file"; then | |
if [[ "$path_prefix" == "/" ]]; then | |
sed -i 's#unix:///var/run#unix:///run#g' "$file" | |
else | |
sed -i -E 's#unix:///var/(.*)#unix://'"${path_prefix}"'/\1#g' "$file" | |
fi | |
fi | |
} | |
replace_goos() { | |
local file="$1" | |
goos_usage_count=0 | |
remaining_runtime_usage=0 | |
should_remove_import=false | |
goos_usage_count=$(grep -v '^\s*//' "$file" | grep -oP '\bruntime\.GOOS' | wc -l) | |
if [[ "$goos_usage_count" -eq "0" ]]; then | |
return | |
fi | |
sed -i 's/runtime\.GOOS/"linux"/g' "$file" | |
remaining_runtime_usage=$(grep -v '[\w.]runtime' $file | grep -v '^\s*//' | grep -oP '\bruntime\.\w+' | wc -l) | |
if [[ "$remaining_runtime_usage" -eq "0" ]]; then | |
sed -i 's/"runtime"/_"runtime"/1' "$file" | |
fi | |
} | |
find "$target_dir" -type f -name '*.go' -not -name '*_test.go' -print0 | while IFS= read -r -d $'\0' file; do | |
# if [[ "$file" != *"/vendor/"* ]]; then | |
# replace_paths "$file" | |
# fi | |
replace_goos "$file" | |
#replace_sockets "$file" | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment