Skip to content

Commit b19b7b4

Browse files
authored
Merge pull request #103 from gekkowrld/install
Add install script
2 parents 0805bb2 + f07d4d5 commit b19b7b4

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed

sh/install.sh

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#!/usr/bin/env sh
2+
3+
# Hii ni skripti ya shell ili kusakinisha programu ya Nuru.
4+
# Programu zinazohitajika:
5+
# - curl/wget: Kupakua faili za 'tar' kutoka 'Github'
6+
# - cp: Nakili faili kuenda mahali sahihi
7+
# - jq: Kupata uhusiano kwenye fomati ya 'JSON'
8+
# - tar: Kufungua faili za tar.gz
9+
10+
set -e
11+
12+
ARCH="$(uname -m)"
13+
OSNAME="$(uname -s)"
14+
PREFIX_PATH="/usr"
15+
BIN=""
16+
VERSION="latest"
17+
RELEASE_URL="https://github.com/NuruProgramming/Nuru/releases"
18+
TEMP=""
19+
20+
# Cleanup function to remove temp directory on exit
21+
cleanup() {
22+
if [ -n "$TEMP" ] && [ -d "$TEMP" ]; then
23+
rm -rf "$TEMP"
24+
fi
25+
}
26+
trap cleanup EXIT
27+
28+
# Check if command exists
29+
command_exists() {
30+
command -v "$1" >/dev/null 2>&1
31+
}
32+
33+
# Print usage information
34+
usage() {
35+
echo "Usage: $0 [OPTIONS]"
36+
echo ""
37+
echo "Options:"
38+
echo " -p, --prefix The base path to be used when installing (default: /usr)"
39+
echo " -v, --version The version to be downloaded from GitHub (default: latest)"
40+
echo " -h, --help Show this help message"
41+
echo ""
42+
}
43+
44+
# Normalize architecture names
45+
arch_name() {
46+
case "$ARCH" in
47+
x86_64)
48+
ARCH="amd64"
49+
;;
50+
i386|i686)
51+
ARCH="i386"
52+
;;
53+
arm64|aarch64)
54+
ARCH="arm64"
55+
;;
56+
*)
57+
echo "Unsupported architecture: $ARCH"
58+
exit 2
59+
;;
60+
esac
61+
}
62+
63+
# Validate OS name
64+
os_name() {
65+
case "$OSNAME" in
66+
Darwin|Linux|Android)
67+
;;
68+
*)
69+
echo "Unsupported Operating System: $OSNAME"
70+
exit 2
71+
;;
72+
esac
73+
}
74+
75+
# Parse command line arguments
76+
parse_args() {
77+
while [ "$#" -gt 0 ]; do
78+
case "$1" in
79+
-h|--help)
80+
usage
81+
exit 0
82+
;;
83+
-p|--prefix)
84+
shift
85+
if [ -z "$1" ]; then
86+
echo "Error: Missing argument for --prefix"
87+
exit 1
88+
fi
89+
PREFIX_PATH="$1"
90+
;;
91+
-v|--version)
92+
shift
93+
if [ -z "$1" ]; then
94+
echo "Error: Missing argument for --version"
95+
exit 1
96+
fi
97+
VERSION="$1"
98+
;;
99+
--)
100+
shift
101+
break
102+
;;
103+
*)
104+
echo "Unknown argument: $1"
105+
usage
106+
exit 1
107+
;;
108+
esac
109+
shift
110+
done
111+
BIN="$PREFIX_PATH/bin"
112+
}
113+
114+
# Download file using curl or wget
115+
download() {
116+
URL="$1"
117+
if command_exists curl; then
118+
curl -fSL "$URL"
119+
elif command_exists wget; then
120+
wget -qO- "$URL"
121+
else
122+
echo "Error: Neither curl nor wget is installed."
123+
exit 1
124+
fi
125+
}
126+
127+
main() {
128+
os_name
129+
arch_name
130+
parse_args "$@"
131+
132+
# Check required commands
133+
for cmd in jq tar cp; do
134+
if ! command_exists "$cmd"; then
135+
echo "Error: Required command '$cmd' not found."
136+
exit 1
137+
fi
138+
done
139+
140+
if [ "$VERSION" = "latest" ]; then
141+
echo "Fetching latest version tag from GitHub..."
142+
VERSION="$(download "https://api.github.com/repos/NuruProgramming/Nuru/releases/latest" | jq -r .tag_name)"
143+
if [ -z "$VERSION" ] || [ "$VERSION" = "null" ]; then
144+
echo "Error: Unable to determine latest version."
145+
exit 1
146+
fi
147+
fi
148+
149+
TAR_URL="$RELEASE_URL/download/$VERSION/nuru_${OSNAME}_${ARCH}.tar.gz"
150+
echo "Downloading Nuru version $VERSION for $OSNAME/$ARCH..."
151+
152+
TEMP="$(mktemp -d)"
153+
if ! download "$TAR_URL" | tar -xz -C "$TEMP"; then
154+
echo "Error: Failed to download or extract archive."
155+
exit 1
156+
fi
157+
158+
# Ensure bin directory exists
159+
if [ ! -d "$BIN" ]; then
160+
echo "Creating directory $BIN"
161+
mkdir -p "$BIN"
162+
fi
163+
164+
echo "Installing Nuru to $BIN/nuru"
165+
if ! cp "$TEMP/nuru" "$BIN/"; then
166+
echo "Error: Failed to copy binary to $BIN"
167+
exit 1
168+
fi
169+
170+
echo "Installation complete."
171+
}
172+
173+
main "$@"

0 commit comments

Comments
 (0)