-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·175 lines (142 loc) · 4.04 KB
/
install.sh
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
#!/bin/bash
set -e
# Configuration
BINARY_NAME="peek"
INSTALL_DIR="/usr/local/bin"
GITHUB_REPO="0verread/peek"
VERSION="latest"
MAIN_PATH="cmd/peek/main.go"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'
# Print step information
info() {
echo -e "${BLUE}==>${NC} $1"
}
# Print success messages
success() {
echo -e "${GREEN}==>${NC} $1"
}
# Print error messages
error() {
echo -e "${RED}==>${NC} $1"
}
# Check for required tools
check_requirements() {
info "Checking system requirements..."
if ! command -v go >/dev/null 2>&1; then
error "Go is not installed. Please install Go first."
exit 1
fi
if ! command -v git >/dev/null 2>&1; then
error "Git is not installed. Please install Git first."
exit 1
fi
}
# Parse command line arguments
parse_args() {
while getopts "v:d:" opt; do
case $opt in
v) VERSION="$OPTARG";;
d) INSTALL_DIR="$OPTARG";;
\?) error "Invalid option -$OPTARG"; exit 1;;
esac
done
}
# Detect OS and architecture
detect_platform() {
info "Detecting platform..."
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
# Convert architecture names
case $ARCH in
x86_64) ARCH="amd64";;
aarch64) ARCH="arm64";;
armv7l) ARCH="arm";;
esac
success "Detected: $OS/$ARCH"
}
# Create temporary directory
setup_temp_dir() {
TEMP_DIR=$(mktemp -d)
trap 'rm -rf "$TEMP_DIR"' EXIT
info "Created temporary directory: $TEMP_DIR"
}
# Download and build the project
build_project() {
info "Building $BINARY_NAME..."
cd "$TEMP_DIR"
if [ "$VERSION" = "latest" ]; then
git clone "https://github.com/$GITHUB_REPO" .
else
git clone -b "$VERSION" "https://github.com/$GITHUB_REPO" .
fi
# Check if the main.go exists in the expected location
if [ -f "$MAIN_PATH" ]; then
info "Building from $MAIN_PATH"
go build -o "$BINARY_NAME" "./$MAIN_PATH"
else
# Try to find main.go recursively
MAIN_FILES=$(find . -name "main.go")
if [ -z "$MAIN_FILES" ]; then
error "No main.go found in the repository"
error "Repository structure:"
ls -R
exit 1
fi
# Use the first main.go found
MAIN_FILE=$(echo "$MAIN_FILES" | head -n 1)
info "Building from found main.go at: $MAIN_FILE"
go build -o "$BINARY_NAME" "$(dirname "$MAIN_FILE")"
fi
if [ ! -f "$BINARY_NAME" ]; then
error "Build failed: Binary was not created"
exit 1
fi
success "Build completed successfully"
}
# Install the binary
install_binary() {
info "Installing to $INSTALL_DIR..."
# Create install directory if it doesn't exist
mkdir -p "$INSTALL_DIR"
# Check if we have write permissions
if [ ! -w "$INSTALL_DIR" ]; then
error "No write permission to $INSTALL_DIR. Please run with sudo."
exit 1
fi
# Move binary to install directory
mv "$TEMP_DIR/$BINARY_NAME" "$INSTALL_DIR/"
chmod +x "$INSTALL_DIR/$BINARY_NAME"
success "Installation completed!"
success "You can now run '$BINARY_NAME' from your terminal."
}
# Check if the binary is properly installed
verify_installation() {
if command -v "$BINARY_NAME" >/dev/null 2>&1; then
success "Verification successful: $BINARY_NAME is installed and accessible"
"$BINARY_NAME" --version 2>/dev/null || true
else
error "Verification failed: $BINARY_NAME is not accessible in PATH"
error "Please check your installation"
exit 1
fi
}
# Main installation process
main() {
echo "Installing $BINARY_NAME..."
parse_args "$@"
check_requirements
detect_platform
setup_temp_dir
build_project
install_binary
verify_installation
echo
success "Installation completed successfully!"
echo "Run '$BINARY_NAME --help' to get started"
}
# Run main function
main "$@"