-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
126 lines (109 loc) · 3.63 KB
/
install.sh
File metadata and controls
126 lines (109 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env bash
set -euo pipefail
REPO="${STAC_RELEASES_REPO:-StacDev/cli-installer}" # fallback org/repo if not set
VERSION="${STAC_VERSION:-latest}"
BIN_NAME="stac"
_err() { echo "[stac_cli] $*" >&2; }
_log() { echo "[stac_cli] $*"; }
_detect_os_arch() {
local os arch
os=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m)
case "$arch" in
x86_64|amd64) arch=x64 ;;
arm64|aarch64) arch=arm64 ;;
*) _err "Unsupported architecture: $arch"; exit 1 ;;
esac
echo "$os" "$arch"
}
_download() {
local url=$1 dest=$2
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$url" -o "$dest"
elif command -v wget >/dev/null 2>&1; then
wget -qO "$dest" "$url"
else
_err "curl or wget required"; exit 1
fi
}
_latest_tag() {
# Requires public repo
curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" | sed -n 's/.*"tag_name":[[:space:]]*"\([^"]*\)".*/\1/p'
}
main() {
local os arch tag version asset url tmpdir bin_dir
read -r os arch < <(_detect_os_arch)
if [[ "$VERSION" == "latest" ]]; then
tag=$(_latest_tag)
if [[ -z "$tag" ]]; then _err "Failed to resolve latest release"; exit 1; fi
else
tag="stac-cli-v${VERSION}"
fi
version=${tag#stac-cli-v}
case "$os" in
darwin|linux) asset="stac_cli_${version}_${os}_${arch}.tar.gz" ;;
*) _err "Unsupported OS: $os"; exit 1 ;;
esac
url="https://github.com/${REPO}/releases/download/${tag}/${asset}"
tmpdir=$(mktemp -d)
trap 'rm -rf "${tmpdir:-}"' EXIT
_log "Downloading $asset from $REPO ($tag)"
_download "$url" "$tmpdir/$asset"
tar -C "$tmpdir" -xzf "$tmpdir/$asset"
if [[ -n "${STAC_INSTALL_DIR:-}" ]]; then
bin_dir="$STAC_INSTALL_DIR"
mkdir -p "$bin_dir"
else
# Install to ~/.stac/bin by default
bin_dir="$HOME/.stac/bin"
mkdir -p "$bin_dir"
fi
install -m 0755 "$tmpdir/stac" "$bin_dir/$BIN_NAME"
_log "Installed to $bin_dir/$BIN_NAME"
case :$PATH: in
*:$bin_dir:*)
_log "✓ $bin_dir is already in PATH"
;;
*)
_log "Adding $bin_dir to PATH..."
# Auto-update shell profiles for ~/.stac/bin
if [[ "$bin_dir" == "$HOME/.stac/bin" ]] && [[ -z "${STAC_NO_PATH_UPDATE:-}" ]]; then
updated_profile=""
for profile in ~/.zshrc ~/.bashrc ~/.bash_profile ~/.profile; do
# Create profile if it doesn't exist and matches current shell
if [[ ! -f "$profile" ]]; then
if [[ "$profile" == ~/.zshrc && "$SHELL" == *zsh* ]]; then
touch "$profile"
elif [[ "$profile" == ~/.bashrc && "$SHELL" == *bash* ]]; then
touch "$profile"
fi
fi
# Update existing profiles
if [[ -f "$profile" ]]; then
if ! grep -q "\.stac/bin\|\.stac\/bin" "$profile" 2>/dev/null; then
echo 'export PATH="$HOME/.stac/bin:$PATH"' >> "$profile"
updated_profile="$profile"
_log "✓ Added to PATH in $profile"
break
else
_log "✓ PATH already configured in $profile"
updated_profile="found"
break
fi
fi
done
if [[ -n "$updated_profile" && "$updated_profile" != "found" ]]; then
_log "Run: source $updated_profile # or restart your terminal"
elif [[ "$updated_profile" == "found" ]]; then
_log "PATH is already configured. You may need to restart your terminal."
else
_log "Add manually: export PATH=\"$bin_dir:\$PATH\""
fi
else
_log "Add to PATH: export PATH=\"$bin_dir:\$PATH\""
fi
;;
esac
_log "Run: stac --help"
}
main "$@"