70 lines
1.5 KiB
Bash
Executable File
70 lines
1.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
usage() {
|
|
cat <<'EOF'
|
|
Usage:
|
|
./scripts/init.sh <new-module-path>
|
|
|
|
Example:
|
|
./scripts/init.sh github.com/yourname/yourrepo
|
|
|
|
What it does:
|
|
- updates go.mod module path
|
|
- replaces old module imports across text files
|
|
- runs go mod tidy
|
|
EOF
|
|
}
|
|
|
|
if [[ $# -ne 1 ]]; then
|
|
usage
|
|
exit 1
|
|
fi
|
|
|
|
new_module="$1"
|
|
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
repo_root="$(cd "$script_dir/.." && pwd)"
|
|
cd "$repo_root"
|
|
|
|
if [[ ! -f go.mod ]]; then
|
|
echo "go.mod not found in repository root"
|
|
exit 1
|
|
fi
|
|
|
|
old_module="$(awk 'NR==1 && $1 == "module" { print $2 }' go.mod)"
|
|
if [[ -z "$old_module" ]]; then
|
|
echo "failed to detect current module path from go.mod"
|
|
exit 1
|
|
fi
|
|
|
|
if [[ "$new_module" == "$old_module" ]]; then
|
|
echo "module path is already set to: $new_module"
|
|
exit 0
|
|
fi
|
|
|
|
echo "Updating module path:"
|
|
echo " old: $old_module"
|
|
echo " new: $new_module"
|
|
|
|
while IFS= read -r file; do
|
|
[[ -n "$file" ]] || continue
|
|
perl -0pi -e "s@\Q${old_module}/\E@${new_module}/@g" "$file"
|
|
done < <(rg -l --hidden --glob '!**/.git/**' --glob '!**/.idea/**' "${old_module}/" . || true)
|
|
|
|
go mod edit -module "$new_module"
|
|
go mod tidy
|
|
|
|
atlas_tool_dir="$repo_root/tools/atlas-loader"
|
|
if [[ -f "$atlas_tool_dir/go.mod" ]]; then
|
|
(
|
|
cd "$atlas_tool_dir"
|
|
go mod edit -droprequire "$old_module"
|
|
go mod edit -require "$new_module@v0.0.0"
|
|
go mod edit -dropreplace "$old_module"
|
|
go mod edit -replace "$new_module=../.."
|
|
go mod tidy
|
|
)
|
|
fi
|
|
|
|
echo "Initialization complete."
|