Repository navigation
Expand file tree
/
Copy pathgomvm
More file actions
executable file
·378 lines (320 loc) · 12.8 KB
/
Copy pathgomvm
File metadata and controls
executable file
·378 lines (320 loc) · 12.8 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/bin/bash
# gomvm - Go Multi Version Manager
# A wrapper script for managing multiple Go versions
# 色の定義
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# 安全な終了処理のための関数
safe_exit() {
# スクリプトが source で実行された場合は return を使用
# そうでない場合は exit を使用
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
return "$1"
else
exit "$1"
fi
}
# メッセージ表示のヘルパー関数
print_info() {
echo -e "${BLUE}ℹ️ $1${NC}"
}
print_success() {
echo -e "${GREEN}✅ $1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_error() {
echo -e "${RED}❌ $1${NC}"
}
# Help function
show_help() {
echo -e "${PURPLE}🔧 Go Multi Version Manager (gomvm)${NC}"
echo ""
echo -e "${PURPLE}使用方法:${NC}"
echo -e " ${GREEN}gomvm install <version>${NC} - Go バージョンをインストール"
echo -e " ${GREEN}gomvm switch <version>${NC} - Go バージョンを切り替え (source コマンドと共に使用)"
echo -e " ${GREEN}gomvm list${NC} - 利用可能な Go バージョンを一覧表示"
echo -e " ${GREEN}gomvm installed${NC} - インストール済みの Go バージョンを一覧表示"
echo -e " ${GREEN}gomvm uninstall <version>${NC} - Go バージョンをアンインストール"
echo -e " ${GREEN}gomvm setup${NC} - 現在のユーザー用に gomvm をセットアップ"
echo -e " ${GREEN}gomvm uninstall-self${NC} - gomvm 自体をアンインストール"
echo -e " ${GREEN}gomvm help${NC} - このヘルプメッセージを表示"
echo ""
echo -e "${PURPLE}例:${NC}"
echo -e " ${GREEN}gomvm install 1.24.1${NC} - Go 1.24.1 をインストール"
echo -e " ${GREEN}source gomvm switch 1.24.1${NC} - Go 1.24.1 に切り替え"
}
# Dynamic configuration - automatically find the script directory
# Try to load the configuration file if it exists
GOMVM_CONFIG_DIR="$HOME/.config/gomvm"
GOMVM_CONFIG_FILE="$GOMVM_CONFIG_DIR/config"
if [ -f "$GOMVM_CONFIG_FILE" ]; then
# shellcheck source=/dev/null
source "$GOMVM_CONFIG_FILE" || print_warning "設定ファイルの読み込みに失敗しました"
fi
# 現在のディレクトリを最優先
REAL_PATH="$(readlink -f "$0")"
CURRENT_DIR_SCRIPTS="$(dirname "$REAL_PATH")/scripts/ubuntu"
# Try various possible locations for the scripts, prioritizing current directory
for dir in \
"$CURRENT_DIR_SCRIPTS" \
"$(dirname "$(dirname "$REAL_PATH")")/scripts/ubuntu" \
"$GOMVM_SCRIPTS_DIR" \
"$HOME/go-multi-version-manager/scripts/ubuntu" \
"$HOME/golang/go-multi-version-manager/scripts/ubuntu"; do
if [ -d "$dir" ]; then
SCRIPT_DIR="$dir"
break
fi
done
# Verify the SCRIPT_DIR exists and contains required scripts
if [ -z "$SCRIPT_DIR" ] || [ ! -d "$SCRIPT_DIR" ] || [ ! -f "$SCRIPT_DIR/switch_go_version.sh" ]; then
print_error "スクリプトディレクトリが見つからないか、必要なスクリプトがありません。"
print_info "GOMVM_SCRIPTS_DIR 環境変数をubuntuスクリプトディレクトリのパスに設定してください。"
echo -e "${CYAN}例: export GOMVM_SCRIPTS_DIR=/path/to/go-multi-version-manager/scripts/ubuntu${NC}"
safe_exit 1
fi
# Function to execute a script with arguments
run_script() {
local script="$1"
shift
# Check if the script exists
if [ ! -f "${SCRIPT_DIR}/${script}" ]; then
print_error "スクリプト ${script} が ${SCRIPT_DIR} に見つかりません"
safe_exit 1
fi
if [[ "$script" == "switch_go_version.sh" ]]; then
# 安全に実行するための処理
# 元のスクリプトの代わりに直接処理を行う
switch_go_directly "$@"
else
# For other scripts, we can execute them directly
"${SCRIPT_DIR}/${script}" "$@" || {
print_error "スクリプト ${script} は終了コード $? で失敗しました"
safe_exit 1
}
fi
}
# Safer alternative to switch_go_version.sh
switch_go_directly() {
local GO_VERSION=$1
if [ -z "$GO_VERSION" ]; then
print_error "Go バージョンの引数がありません。"
print_info "使用法: source gomvm switch <go_version>"
safe_exit 1
fi
# Temporarily add $HOME/go/bin to PATH
if ! echo "$PATH" | grep -q "$HOME/go/bin"; then
export PATH="$HOME/go/bin:$PATH"
fi
# Check if goX.X.X command exists
if ! command -v "go${GO_VERSION}" &> /dev/null; then
print_info "Go バージョン $GO_VERSION はインストールされていません。インストールします..."
"${SCRIPT_DIR}/install_go_with_command.sh" "${GO_VERSION}" || {
print_error "Go $GO_VERSION のインストールに失敗しました。セットアップを確認してください。"
safe_exit 1
}
# Verify installation after setting PATH
if ! command -v "go${GO_VERSION}" &> /dev/null; then
print_error "インストール確認に失敗しました。PATH設定を確認してください。"
safe_exit 1
fi
fi
# Get the correct GOROOT for the specified version
# First, find the binary location
local GO_BINARY_PATH
GO_BINARY_PATH=$(command -v "go${GO_VERSION}")
if [ -z "$GO_BINARY_PATH" ]; then
print_error "go${GO_VERSION} バイナリが見つかりません。"
safe_exit 1
fi
# Get GOROOT from the specific version binary
local NEW_GOROOT
NEW_GOROOT=$("go${GO_VERSION}" env GOROOT)
if [ -z "$NEW_GOROOT" ] || [ ! -d "$NEW_GOROOT" ]; then
print_error "Go ${GO_VERSION} の GOROOT が取得できません。"
safe_exit 1
fi
# Update environment variables in the correct order
export GOROOT="$NEW_GOROOT"
export PATH="$NEW_GOROOT/bin:$HOME/go/bin:$PATH"
# Remove duplicate entries from PATH
PATH_TEMP=$(echo "$PATH" | awk -v RS=':' '!a[$1]++' | paste -sd:)
export PATH="$PATH_TEMP"
# Set GOPATH to default if not set
if [ -z "$GOPATH" ]; then
export GOPATH="$HOME/go"
fi
# Save version selection for persistence
local GO_SELECTED_VERSION_FILE="$HOME/.go_selected_version"
echo "$GO_VERSION" > "$GO_SELECTED_VERSION_FILE"
# Confirm version switch
print_success "Go $GO_VERSION に切り替えました。"
print_info "現在の Go バージョン: $(go version)"
print_info "GOROOT: $GOROOT"
print_info "GOPATH: $GOPATH"
print_info "バイナリの場所: $(which go)"
}
# Main command handler
if [ $# -eq 0 ]; then
# 引数なしで実行された場合はヘルプを表示
show_help
safe_exit 0
fi
case "$1" in
"install")
if [ -z "$2" ]; then
print_error "バージョン番号が必要です。"
print_info "使用法: gomvm install <version>"
safe_exit 1
fi
run_script "install_go_with_command.sh" "$2"
;;
"replace-default")
if [ -z "$2" ]; then
print_error "バージョン番号が必要です。"
print_info "使用法: gomvm replace-default <version>"
safe_exit 1
fi
run_script "install_go_replace_default.sh" "$2"
;;
"install-specific")
if [ -z "$2" ]; then
print_error "バージョン番号が必要です。"
print_info "使用法: gomvm install-specific <version>"
safe_exit 1
fi
run_script "install_go_specific.sh" "$2"
;;
"switch")
if [ -z "$2" ]; then
print_error "バージョン番号が必要です。"
print_info "使用法: source gomvm switch <version>"
safe_exit 1
fi
# スクリプトが source で実行されたかを確認
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
print_warning "'gomvm switch' は現在のシェルに影響するため、'source' と共に実行する必要があります。"
print_info "以下を実行してください: source gomvm switch $2"
safe_exit 1
fi
switch_go_directly "$2"
;;
"list")
run_script "list_go_versions.sh"
;;
"installed")
run_script "go_versions.sh"
;;
"uninstall")
if [ -z "$2" ]; then
print_error "バージョン番号が必要です。"
print_info "使用法: gomvm uninstall <version>"
safe_exit 1
fi
run_script "uninstall_go_with_command.sh" "$2"
;;
"setup")
# Setup gomvm for current user
print_info "現在のユーザー用に gomvm をセットアップしています..."
# Create bin directory if it doesn't exist
mkdir -p "$HOME/.local/bin"
# Copy or create a symlink to this script
SCRIPT_PATH="$(readlink -f "$0")"
TARGET_PATH="$HOME/.local/bin/gomvm"
if [ "$SCRIPT_PATH" != "$TARGET_PATH" ]; then
cp "$SCRIPT_PATH" "$TARGET_PATH"
chmod +x "$TARGET_PATH"
print_success "gomvm を $TARGET_PATH にコピーしました"
else
print_info "gomvm はすでに $TARGET_PATH にインストールされています"
fi
# Check if PATH includes ~/.local/bin
if ! echo "$PATH" | grep -q "$HOME/.local/bin"; then
echo "export PATH=\"\$HOME/.local/bin:\$PATH\"" >> "$HOME/.bashrc"
print_success "\$HOME/.local/bin を ~/.bashrc の PATH に追加しました"
else
print_info "\$HOME/.local/bin はすでに PATH に含まれています"
fi
# Store the scripts directory in user config
mkdir -p "$GOMVM_CONFIG_DIR"
echo "GOMVM_SCRIPTS_DIR=\"$SCRIPT_DIR\"" > "$GOMVM_CONFIG_FILE"
print_success "設定を $GOMVM_CONFIG_FILE に保存しました"
# Copy go-env.sh from repository to user config directory
REPO_ROOT=$(dirname "$(dirname "$SCRIPT_DIR")") # scripts/ubuntu から2つ上がリポジトリルート
GO_ENV_SOURCE="$REPO_ROOT/go-env.sh"
GO_ENV_TARGET="$GOMVM_CONFIG_DIR/go-env.sh"
if [ -f "$GO_ENV_SOURCE" ]; then
cp "$GO_ENV_SOURCE" "$GO_ENV_TARGET"
chmod +x "$GO_ENV_TARGET"
print_success "Go環境設定スクリプトをコピーしました: $GO_ENV_TARGET"
print_info "最新バージョンチェックはデフォルトで無効です"
print_info "有効にするには $GO_ENV_TARGET を編集してコメントを解除してください"
else
print_error "Go環境設定スクリプトが見つかりません: $GO_ENV_SOURCE"
print_info "リポジトリに go-env.sh ファイルが存在することを確認してください"
fi
# Add go-env.sh sourcing to .bashrc if not already present
BASHRC_LINE="[ -f \"\$HOME/.config/gomvm/go-env.sh\" ] && source \"\$HOME/.config/gomvm/go-env.sh\""
if ! grep -q "gomvm/go-env.sh" "$HOME/.bashrc"; then
{
echo ""
echo "# Go環境設定 - gomvm"
echo "$BASHRC_LINE"
} >> "$HOME/.bashrc"
print_success "Go環境設定を ~/.bashrc に追加しました"
else
print_info "Go環境設定はすでに ~/.bashrc に設定されています"
fi
echo ""
print_success "gomvm のセットアップが完了しました!"
print_info "これで任意の場所から 'gomvm' を使用できます。"
print_info "Go バージョンを切り替えるには: source gomvm switch <version>"
echo -e "${CYAN}例: source gomvm switch 1.24.0${NC}"
print_info "現在のスクリプトディレクトリ: $SCRIPT_DIR"
print_info "設定を反映するには: source ~/.bashrc"
;;
"uninstall-self")
# Uninstall gomvm
print_info "gomvm をアンインストールしています..."
# Confirm uninstallation
read -r -p "本当に gomvm をアンインストールしますか? (y/N): " confirm
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
print_info "アンインストールをキャンセルしました。"
safe_exit 0
fi
# Remove gomvm binary
if [ -f "$HOME/.local/bin/gomvm" ]; then
rm -f "$HOME/.local/bin/gomvm"
print_success "gomvm バイナリを $HOME/.local/bin/gomvm から削除しました"
else
print_info "gomvm バイナリが $HOME/.local/bin に見つかりません"
fi
# Remove configuration
if [ -d "$GOMVM_CONFIG_DIR" ]; then
rm -rf "$GOMVM_CONFIG_DIR"
print_success "設定ディレクトリ $GOMVM_CONFIG_DIR を削除しました"
fi
echo ""
print_success "gomvm のアンインストールが完了しました。"
print_info "注: Go バージョンのインストールとリポジトリはそのまま残されています。"
print_info "すべてを完全に削除するには、以下も削除できます:"
echo -e "${CYAN}1. スクリプトを含むリポジトリディレクトリ${NC}"
echo -e "${CYAN}2. $HOME/go/bin/ の Go バージョン('gomvm install' でインストールした場合)${NC}"
;;
"help"|"--help"|"-h")
show_help
;;
*)
print_error "不明なコマンド: $1"
show_help
safe_exit 1
;;
esac
safe_exit 0