-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·334 lines (289 loc) · 8.96 KB
/
install.sh
File metadata and controls
executable file
·334 lines (289 loc) · 8.96 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env bash
#
# gsheet-cli installer
# https://github.com/dipankar/gsheet-cli
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/dipankar/gsheet-cli/main/install.sh | bash
# or
# ./install.sh [OPTIONS]
#
# Options:
# --prefix DIR Installation prefix (default: ~/.local)
# --completions Install shell completions
# --no-modify-path Don't modify shell profile
# --help Show this help message
#
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
BOLD='\033[1m'
NC='\033[0m' # No Color
# Defaults
PREFIX="${HOME}/.local"
INSTALL_COMPLETIONS=false
MODIFY_PATH=true
BIN_NAME="gsheet"
# Print functions
info() {
echo -e "${BLUE}==>${NC} ${BOLD}$1${NC}"
}
success() {
echo -e "${GREEN}==>${NC} ${BOLD}$1${NC}"
}
warn() {
echo -e "${YELLOW}Warning:${NC} $1"
}
error() {
echo -e "${RED}Error:${NC} $1" >&2
exit 1
}
# Show help
show_help() {
cat << EOF
gsheet-cli installer
Usage: ./install.sh [OPTIONS]
Options:
--prefix DIR Installation prefix (default: ~/.local)
--completions Install shell completions
--no-modify-path Don't modify shell profile
--help Show this help message
Examples:
./install.sh
./install.sh --prefix /usr/local --completions
./install.sh --prefix ~/.local --completions --no-modify-path
EOF
exit 0
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--prefix)
PREFIX="$2"
shift 2
;;
--completions)
INSTALL_COMPLETIONS=true
shift
;;
--no-modify-path)
MODIFY_PATH=false
shift
;;
--help|-h)
show_help
;;
*)
error "Unknown option: $1. Use --help for usage."
;;
esac
done
# Detect OS
detect_os() {
case "$(uname -s)" in
Linux*) OS=linux;;
Darwin*) OS=macos;;
MINGW*|MSYS*|CYGWIN*) OS=windows;;
*) OS=unknown;;
esac
echo "$OS"
}
# Detect shell
detect_shell() {
basename "${SHELL:-/bin/bash}"
}
# Check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check Rust installation
check_rust() {
if ! command_exists rustc; then
warn "Rust is not installed."
info "Installing Rust via rustup..."
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source "${HOME}/.cargo/env"
fi
RUST_VERSION=$(rustc --version | cut -d' ' -f2)
info "Rust version: $RUST_VERSION"
}
# Build from source
build_from_source() {
info "Building gsheet-cli from source..."
# Check if we're in the repo directory
if [[ -f "Cargo.toml" ]] && grep -q 'name = "gsheet-cli"' Cargo.toml 2>/dev/null; then
info "Building in current directory..."
cargo build --release
else
# Clone the repo
TEMP_DIR=$(mktemp -d)
info "Cloning repository to $TEMP_DIR..."
git clone --depth 1 https://github.com/dipankar/gsheet-cli.git "$TEMP_DIR"
cd "$TEMP_DIR"
cargo build --release
# Copy binary
mkdir -p "${PREFIX}/bin"
cp "target/release/${BIN_NAME}" "${PREFIX}/bin/"
# Cleanup
cd - >/dev/null
rm -rf "$TEMP_DIR"
return
fi
# Copy binary from local build
mkdir -p "${PREFIX}/bin"
cp "target/release/${BIN_NAME}" "${PREFIX}/bin/"
}
# Install shell completions
install_completions() {
local shell=$(detect_shell)
local bin="${PREFIX}/bin/${BIN_NAME}"
if [[ ! -x "$bin" ]]; then
warn "Binary not found at $bin, skipping completions"
return
fi
info "Installing completions for $shell..."
case "$shell" in
bash)
COMP_DIR="${HOME}/.local/share/bash-completion/completions"
mkdir -p "$COMP_DIR"
"$bin" completions bash > "${COMP_DIR}/${BIN_NAME}"
success "Bash completions installed to ${COMP_DIR}/${BIN_NAME}"
;;
zsh)
COMP_DIR="${HOME}/.zfunc"
mkdir -p "$COMP_DIR"
"$bin" completions zsh > "${COMP_DIR}/_${BIN_NAME}"
success "Zsh completions installed to ${COMP_DIR}/_${BIN_NAME}"
echo ""
echo "Add to your ~/.zshrc if not already present:"
echo " fpath=(~/.zfunc \$fpath)"
echo " autoload -Uz compinit && compinit"
;;
fish)
COMP_DIR="${HOME}/.config/fish/completions"
mkdir -p "$COMP_DIR"
"$bin" completions fish > "${COMP_DIR}/${BIN_NAME}.fish"
success "Fish completions installed to ${COMP_DIR}/${BIN_NAME}.fish"
;;
*)
warn "Unknown shell: $shell. Skipping completions."
;;
esac
}
# Add to PATH in shell profile
add_to_path() {
local shell=$(detect_shell)
local bin_dir="${PREFIX}/bin"
local profile=""
local path_line="export PATH=\"${bin_dir}:\$PATH\""
# Check if already in PATH
if echo "$PATH" | tr ':' '\n' | grep -q "^${bin_dir}$"; then
info "Directory already in PATH"
return
fi
case "$shell" in
bash)
if [[ -f "${HOME}/.bashrc" ]]; then
profile="${HOME}/.bashrc"
elif [[ -f "${HOME}/.bash_profile" ]]; then
profile="${HOME}/.bash_profile"
fi
;;
zsh)
profile="${HOME}/.zshrc"
;;
fish)
profile="${HOME}/.config/fish/config.fish"
path_line="set -gx PATH ${bin_dir} \$PATH"
;;
esac
if [[ -n "$profile" ]]; then
if ! grep -q "${bin_dir}" "$profile" 2>/dev/null; then
echo "" >> "$profile"
echo "# Added by gsheet-cli installer" >> "$profile"
echo "$path_line" >> "$profile"
success "Added ${bin_dir} to PATH in ${profile}"
echo ""
echo "Run this to update your current session:"
echo " source ${profile}"
else
info "PATH already configured in ${profile}"
fi
else
warn "Could not detect shell profile. Add this to your shell config:"
echo " $path_line"
fi
}
# Create config directory
create_config_dir() {
local config_dir="${HOME}/.config/gsheet-cli"
if [[ ! -d "$config_dir" ]]; then
mkdir -p "$config_dir"
info "Created config directory: $config_dir"
fi
}
# Print post-install instructions
print_instructions() {
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
success "gsheet-cli installed successfully!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "Binary location: ${PREFIX}/bin/${BIN_NAME}"
echo ""
echo "Next steps:"
echo ""
echo " 1. Authenticate with Google Sheets:"
echo " ${BIN_NAME} auth login"
echo ""
echo " 2. Add a spreadsheet profile:"
echo " ${BIN_NAME} config profile add mysheet --spreadsheet \"YOUR_SPREADSHEET_ID\""
echo ""
echo " 3. Start using:"
echo " ${BIN_NAME} -p mysheet sheet list"
echo ""
echo "Documentation: https://github.com/dipankar/gsheet-cli"
echo ""
}
# Main installation flow
main() {
echo ""
echo "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"
echo "┃ gsheet-cli installer ┃"
echo "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"
echo ""
OS=$(detect_os)
info "Detected OS: $OS"
info "Installation prefix: $PREFIX"
# Check dependencies
if ! command_exists git; then
error "git is required but not installed. Please install git first."
fi
# Check/install Rust
check_rust
# Build and install
build_from_source
# Create config directory
create_config_dir
# Install completions if requested
if [[ "$INSTALL_COMPLETIONS" == "true" ]]; then
install_completions
fi
# Add to PATH if requested
if [[ "$MODIFY_PATH" == "true" ]]; then
add_to_path
fi
# Verify installation
if [[ -x "${PREFIX}/bin/${BIN_NAME}" ]]; then
VERSION=$("${PREFIX}/bin/${BIN_NAME}" --version 2>/dev/null || echo "unknown")
success "Installed: $VERSION"
else
error "Installation failed. Binary not found at ${PREFIX}/bin/${BIN_NAME}"
fi
print_instructions
}
# Run main
main