-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
243 lines (207 loc) · 6.89 KB
/
worker.js
File metadata and controls
243 lines (207 loc) · 6.89 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// SPDX-FileCopyrightText: Copyright (C) 2025 Akshat Kotpalliwar (alias IntegerAlex) <inquiry.akshatkotpalliwar@gmail.com>
// SPDX-License-Identifier: GPL-3.0-only
/**
* Cloudflare Worker to serve the rig installer script
* Usage:
* curl https://rig-installer.gossorg.in | bash
*/
const REPO = "IntegerAlex/rig";
const BINARY_NAME = "rig";
const INSTALL_DIR = "$HOME/.local/bin";
const INSTALLER_SCRIPT = `#!/bin/bash
# SPDX-FileCopyrightText: Copyright (C) 2025 Akshat Kotpalliwar (alias IntegerAlex) <inquiry.akshatkotpalliwar@gmail.com>
# SPDX-License-Identifier: GPL-3.0-only
set -e
BINARY_NAME="` + BINARY_NAME + `"
INSTALL_DIR="` + INSTALL_DIR + `"
RELEASE_URL="https://github.com/` + REPO + `/releases/latest"
echo "Installing rig - Opinionated system setup tool..."
echo "Repository: ` + REPO + `"
echo "Install directory: ` + INSTALL_DIR + `"
# Detect architecture
ARCH=$(uname -m)
case $ARCH in
x86_64)
ARCH="x86_64"
;;
aarch64|arm64)
ARCH="aarch64"
;;
*)
echo "❌ Unsupported architecture: $ARCH"
echo "rig currently supports x86_64 and aarch64 architectures"
exit 1
;;
esac
# Detect OS - only support Linux
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
if [ "$OS" != "linux" ]; then
echo "❌ Unsupported operating system: $OS"
echo "rig is designed for Linux systems only"
exit 1
fi
echo "✅ Detected: Linux $ARCH"
# Check if running on Debian-based system (has apt or apt-get)
if ! command -v apt &> /dev/null && ! command -v apt-get &> /dev/null; then
echo "❌ This installer requires a Debian-based Linux distribution"
echo " Supported: Ubuntu, Debian, Linux Mint, and other Debian derivatives"
echo "apt/apt-get command not found. Please install rig manually from:"
echo "https://github.com/` + REPO + `/releases"
exit 1
fi
echo "✅ Debian-based system detected (Ubuntu/Debian/Linux Mint/etc.)"
# Get latest release tag
echo "📦 Fetching latest release..."
LATEST_TAG=$(curl -s "https://api.github.com/repos/` + REPO + `/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\\1/')
if [ -z "$LATEST_TAG" ]; then
echo "❌ Error: Could not fetch latest release tag"
echo "Please check your internet connection and try again"
exit 1
fi
echo "📋 Latest release: $LATEST_TAG"
# Construct download URL
ASSET_NAME="rig"
DOWNLOAD_URL="https://github.com/` + REPO + `/releases/download/$LATEST_TAG/$ASSET_NAME"
echo "⬇️ Downloading from: $DOWNLOAD_URL"
# Create install directory if it doesn't exist
mkdir -p "$INSTALL_DIR"
# Download binary
TEMP_FILE=$(mktemp)
if curl -L -f -o "$TEMP_FILE" "$DOWNLOAD_URL"; then
echo "✅ Download successful"
else
echo "❌ Error: Failed to download binary"
echo "Tried URL: $DOWNLOAD_URL"
echo "Available releases: https://github.com/` + REPO + `/releases"
rm -f "$TEMP_FILE"
exit 1
fi
# Make binary executable
chmod +x "$TEMP_FILE"
# Install to target directory
INSTALL_PATH="$INSTALL_DIR/$BINARY_NAME"
mv "$TEMP_FILE" "$INSTALL_PATH"
echo "✅ Installed to: $INSTALL_PATH"
# Configure shell for permanent PATH setup
echo ""
echo "🔧 Configuring shell..."
# Detect shell
SHELL_TYPE=""
if [[ "$SHELL" == *"zsh"* ]]; then
SHELL_TYPE="zsh"
elif [[ "$SHELL" == *"bash"* ]]; then
SHELL_TYPE="bash"
else
# Fallback: check current shell process
if ps -p $$ -o cmd= | grep -q zsh; then
SHELL_TYPE="zsh"
else
SHELL_TYPE="bash"
fi
fi
echo "→ Detected shell: $SHELL_TYPE"
# Determine config file
CONFIG_FILE=""
if [[ "$SHELL_TYPE" == "zsh" ]]; then
# Check for existing zsh config files in order of preference
if [[ -f "$HOME/.zshrc" ]]; then
CONFIG_FILE="$HOME/.zshrc"
elif [[ -f "$HOME/.zprofile" ]]; then
CONFIG_FILE="$HOME/.zprofile"
elif [[ -f "$HOME/.zshenv" ]]; then
CONFIG_FILE="$HOME/.zshenv"
else
CONFIG_FILE="$HOME/.zshrc"
fi
else
# Check for existing bash config files in order of preference
if [[ -f "$HOME/.bashrc" ]]; then
CONFIG_FILE="$HOME/.bashrc"
elif [[ -f "$HOME/.bash_profile" ]]; then
CONFIG_FILE="$HOME/.bash_profile"
elif [[ -f "$HOME/.profile" ]]; then
CONFIG_FILE="$HOME/.profile"
else
CONFIG_FILE="$HOME/.bashrc"
fi
fi
echo "→ Config file: $(basename "$CONFIG_FILE")"
# Check if PATH entry already exists
PATH_ENTRY="export PATH=\"\$HOME/.local/bin:\$PATH\""
if grep -q "$PATH_ENTRY" "$CONFIG_FILE" 2>/dev/null; then
echo "→ PATH entry already exists in $(basename "$CONFIG_FILE")"
else
# Add PATH entry to config file
echo "" >> "$CONFIG_FILE"
echo "# Added by rig installer" >> "$CONFIG_FILE"
echo "$PATH_ENTRY" >> "$CONFIG_FILE"
echo "✅ Added PATH entry to $(basename "$CONFIG_FILE")"
fi
# Add to current session PATH if not already there
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
export PATH="$INSTALL_DIR:$PATH"
echo "→ Added to current session PATH"
fi
echo ""
echo "🎉 Installation complete!"
echo ""
# Check if we can access the terminal (even when stdin is piped)
if [ -t 1 ] && [ -c /dev/tty ] 2>/dev/null; then
echo "🚀 Running rig..."
echo ""
# Execute rig automatically - redirect stdin from /dev/tty to allow interactive prompts
"$INSTALL_PATH" < /dev/tty
else
echo "📖 To run rig, execute:"
echo " rig"
echo ""
echo "📚 rig is an opinionated system setup tool for Linux"
echo " It helps you install essential development tools with a beautiful UI"
fi
`;
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
// Handle CORS for preflight requests
if (request.method === 'OPTIONS') {
return new Response(null, {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
},
});
}
// Only allow GET requests
if (request.method !== 'GET') {
return new Response('Method not allowed', { status: 405 });
}
// Serve installer script
if (url.pathname === '/' || url.pathname === '/install' || url.pathname === '/install.sh') {
return new Response(INSTALLER_SCRIPT, {
headers: {
'Content-Type': 'text/plain; charset=utf-8',
'Content-Disposition': 'inline; filename="install.sh"',
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'public, max-age=3600', // Cache for 1 hour
},
});
}
// Health check endpoint
if (url.pathname === '/health') {
return new Response(JSON.stringify({
status: 'ok',
service: 'rig-installer',
repo: REPO,
binary: BINARY_NAME
}), {
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
});
}
// 404 for other paths
return new Response('Not found', { status: 404 });
},
};