Skip to content

Commit 6ee534c

Browse files
committed
refactor: optimize installation script code
1 parent 4f880fb commit 6ee534c

File tree

1 file changed

+86
-74
lines changed

1 file changed

+86
-74
lines changed

install.sh

Lines changed: 86 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -32,56 +32,36 @@ function get_os() {
3232
echo $(uname -s | awk '{print tolower($0)}')
3333
}
3434

35-
function main() {
36-
local release="1.8.0"
37-
local os=$(get_os)
38-
local arch=$(get_arch)
39-
local dest_file="${HOME}/.g/downloads/g${release}.${os}-${arch}.tar.gz"
40-
local url="https://github.com/voidint/g/releases/download/v${release}/g${release}.${os}-${arch}.tar.gz"
41-
42-
echo "[1/3] Downloading ${url}"
43-
rm -f "${dest_file}"
44-
if [ -x "$(command -v wget)" ]; then
45-
mkdir -p "${HOME}/.g/downloads"
46-
wget -q -P "${HOME}/.g/downloads" "${url}"
47-
else
48-
curl -s -S -L --create-dirs -o "${dest_file}" "${url}"
49-
fi
50-
51-
echo "[2/3] Install g to the ${HOME}/.g/bin"
52-
mkdir -p "${HOME}/.g/bin"
53-
tar -xz -f "${dest_file}" -C "${HOME}/.g/bin"
54-
chmod +x "${HOME}/.g/bin/g"
55-
56-
echo "[3/3] Set environment variables"
57-
cat >${HOME}/.g/env <<-'EOF'
58-
#!/bin/sh
59-
# g shell setup
60-
export GOROOT="${HOME}/.g/go"
61-
[ -z "$GOPATH" ] && export GOPATH="${HOME}/go"
62-
export PATH="${HOME}/.g/bin:${GOROOT}/bin:${GOPATH}/bin:$PATH"
63-
export G_MIRROR=https://golang.google.cn/dl/
64-
EOF
65-
66-
if [ -x "$(command -v bash)" ]; then
67-
cat >>${HOME}/.bashrc <<-'EOF'
35+
# Setup shell configuration for bash/zsh (POSIX-compatible shells)
36+
function setup_posix_shell() {
37+
local rc_file=$1
38+
cat >>"${rc_file}" <<'EOF'
6839
6940
[ -s "${HOME}/.g/env" ] && \. "${HOME}/.g/env" # g shell setup
41+
EOF
42+
}
7043

71-
EOF
72-
fi
73-
74-
if [ -x "$(command -v zsh)" ]; then
75-
cat >>${HOME}/.zshrc <<-'EOF'
44+
# Setup shell configuration for csh/tcsh
45+
function setup_csh_shell() {
46+
local rc_file=$1
47+
cat >>"${rc_file}" <<'EOF'
48+
setenv GOROOT "$HOME/.g/go"
7649
77-
[ -s "${HOME}/.g/env" ] && \. "${HOME}/.g/env" # g shell setup
50+
if ( ! $?GOPATH ) then
51+
setenv GOPATH "$HOME/go"
52+
else if ( "$GOPATH" == "" ) then
53+
setenv GOPATH "$HOME/go"
54+
endif
7855
79-
EOF
80-
fi
56+
setenv PATH "$HOME/.g/bin:$GOROOT/bin:$GOPATH/bin:$PATH"
57+
setenv G_MIRROR "https://golang.google.cn/dl/"
58+
EOF
59+
}
8160

82-
if [ -x "$(command -v fish)" ]; then
83-
# Create fish-specific env file
84-
cat >${HOME}/.g/env.fish <<-'EOF_ENV_FISH'
61+
# Setup shell configuration for fish
62+
function setup_fish_shell() {
63+
# Create fish-specific env file
64+
cat >"${HOME}/.g/env.fish" <<'EOF'
8565
# g shell setup for fish
8666
8767
set -gx GOROOT "$HOME/.g/go"
@@ -91,52 +71,84 @@ set -gx G_MIRROR "https://golang.google.cn/dl/"
9171
fish_add_path "$HOME/.g/bin"
9272
fish_add_path "$GOROOT/bin"
9373
94-
set -gx GOPATH "$HOME/go"
74+
# Set GOPATH only if not already set or empty (consistent with bash/zsh/csh behavior)
75+
if not set -q GOPATH; or test -z "$GOPATH"
76+
set -gx GOPATH "$HOME/go"
77+
end
9578
96-
if set -q GOPATH;
79+
if set -q GOPATH
9780
fish_add_path "$GOPATH/bin"
9881
end
99-
EOF_ENV_FISH
82+
EOF
10083

101-
# Configure fish to source env.fish
102-
local fish_conf_d_dir="${HOME}/.config/fish/conf.d"
103-
mkdir -p "${fish_conf_d_dir}"
104-
cat >"${fish_conf_d_dir}/g.fish" <<-'EOF_G_FISH_CONF'
84+
# Configure fish to source env.fish
85+
local fish_conf_d_dir="${HOME}/.config/fish/conf.d"
86+
mkdir -p "${fish_conf_d_dir}"
87+
cat >"${fish_conf_d_dir}/g.fish" <<'EOF'
10588
# g shell setup
10689
if test -s "$HOME/.g/env.fish"; and source "$HOME/.g/env.fish"; end
107-
EOF_G_FISH_CONF
90+
EOF
91+
}
92+
93+
# Configure all available shells
94+
function configure_shells() {
95+
# POSIX-compatible shells (bash, zsh)
96+
if [ -x "$(command -v bash)" ]; then
97+
setup_posix_shell "${HOME}/.bashrc"
10898
fi
10999

110-
if [ -x "$(command -v csh)" ]; then
111-
cat >>${HOME}/.cshrc <<-'EOF'
112-
setenv GOROOT "$HOME/.g/go"
100+
if [ -x "$(command -v zsh)" ]; then
101+
setup_posix_shell "${HOME}/.zshrc"
102+
fi
113103

114-
if ( ! $?GOPATH ) then
115-
setenv GOPATH "$HOME/go"
116-
else if ( "$GOPATH" == "" ) then
117-
setenv GOPATH "$HOME/go"
118-
endif
104+
# Fish shell
105+
if [ -x "$(command -v fish)" ]; then
106+
setup_fish_shell
107+
fi
119108

120-
setenv PATH "$HOME/.g/bin:$GOROOT/bin:$GOPATH/bin:$PATH"
121-
setenv G_MIRROR "https://golang.google.cn/dl/"
122-
EOF
109+
# C shell variants
110+
if [ -x "$(command -v csh)" ]; then
111+
setup_csh_shell "${HOME}/.cshrc"
123112
fi
124113

125-
if [ -x "$(command -v tcsh)" ]; then
126-
cat >>${HOME}/.tcshrc <<-'EOF'
127-
setenv GOROOT "$HOME/.g/go"
114+
if [ -x "$(command -v tcsh)" ]; then
115+
setup_csh_shell "${HOME}/.tcshrc"
116+
fi
117+
}
128118

129-
if ( ! $?GOPATH ) then
130-
setenv GOPATH "$HOME/go"
131-
else if ( "$GOPATH" == "" ) then
132-
setenv GOPATH "$HOME/go"
133-
endif
119+
function main() {
120+
local release="1.8.0"
121+
local os=$(get_os)
122+
local arch=$(get_arch)
123+
local dest_file="${HOME}/.g/downloads/g${release}.${os}-${arch}.tar.gz"
124+
local url="https://github.com/voidint/g/releases/download/v${release}/g${release}.${os}-${arch}.tar.gz"
134125

135-
setenv PATH "$HOME/.g/bin:$GOROOT/bin:$GOPATH/bin:$PATH"
136-
setenv G_MIRROR "https://golang.google.cn/dl/"
137-
EOF
126+
echo "[1/3] Downloading ${url}"
127+
rm -f "${dest_file}"
128+
if [ -x "$(command -v wget)" ]; then
129+
mkdir -p "${HOME}/.g/downloads"
130+
wget -q -P "${HOME}/.g/downloads" "${url}"
131+
else
132+
curl -s -S -L --create-dirs -o "${dest_file}" "${url}"
138133
fi
139134

135+
echo "[2/3] Install g to the ${HOME}/.g/bin"
136+
mkdir -p "${HOME}/.g/bin"
137+
tar -xz -f "${dest_file}" -C "${HOME}/.g/bin"
138+
chmod +x "${HOME}/.g/bin/g"
139+
140+
echo "[3/3] Set environment variables"
141+
cat >"${HOME}/.g/env" <<'EOF'
142+
#!/bin/sh
143+
# g shell setup
144+
export GOROOT="${HOME}/.g/go"
145+
[ -z "$GOPATH" ] && export GOPATH="${HOME}/go"
146+
export PATH="${HOME}/.g/bin:${GOROOT}/bin:${GOPATH}/bin:$PATH"
147+
export G_MIRROR=https://golang.google.cn/dl/
148+
EOF
149+
150+
configure_shells
151+
140152
echo -e "\nTo configure your current shell, run:\nsource \"$HOME/.g/env\""
141153

142154
exit 0

0 commit comments

Comments
 (0)