Skip to content

Commit 7ff81b1

Browse files
author
William Yang
committed
feat: 安装脚本支持多课程,自动从 slug 推断镜像
1 parent d0a0592 commit 7ff81b1

File tree

1 file changed

+54
-9
lines changed

1 file changed

+54
-9
lines changed

scripts/install.sh

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
#!/bin/bash
22
# BootCS CLI 安装脚本
3-
# 用法: curl -fsSL https://bootcs.cn/install.sh | bash
3+
# 用法:
4+
# curl -fsSL https://bootcs.cn/install.sh | bash # 安装通用版
5+
# curl -fsSL https://bootcs.cn/install.sh | bash -s -- cs50 # 安装 CS50 课程
6+
# curl -fsSL https://bootcs.cn/install.sh | bash -s -- python # 安装 Python 课程
47

58
set -e
69

710
# 颜色定义
811
RED='\033[0;31m'
912
GREEN='\033[0;32m'
1013
YELLOW='\033[1;33m'
14+
BLUE='\033[0;34m'
1115
NC='\033[0m' # No Color
1216

17+
# 获取课程参数
18+
COURSE="${1:-}"
19+
1320
echo -e "${GREEN}🚀 Installing BootCS CLI...${NC}"
1421

1522
# 检查 Docker 是否安装
@@ -29,23 +36,61 @@ fi
2936
INSTALL_DIR="${HOME}/.local/bin"
3037
mkdir -p "$INSTALL_DIR"
3138

32-
# 创建 bootcs 脚本
39+
# 可用课程列表(镜像标签)
40+
declare -A COURSES=(
41+
["cs50"]="ghcr.io/bootcs-cn/bootcs-cli:cs50"
42+
["python"]="ghcr.io/bootcs-cn/bootcs-cli:python"
43+
["java"]="ghcr.io/bootcs-cn/bootcs-cli:java"
44+
# 新课程在这里添加
45+
)
46+
47+
# 创建通用 bootcs 脚本
3348
BOOTCS_SCRIPT="$INSTALL_DIR/bootcs"
3449

3550
cat > "$BOOTCS_SCRIPT" << 'EOF'
3651
#!/bin/bash
3752
# BootCS CLI Wrapper
3853
# https://bootcs.cn
3954
40-
# 默认使用 cs50 镜像,可通过环境变量覆盖
41-
BOOTCS_IMAGE="${BOOTCS_IMAGE:-ghcr.io/bootcs-cn/bootcs-cli:cs50}"
55+
# 从 slug 推断课程(如 cs50/credit -> cs50)
56+
infer_course() {
57+
local slug="$1"
58+
echo "${slug%%/*}"
59+
}
60+
61+
# 课程镜像映射
62+
get_image() {
63+
local course="$1"
64+
case "$course" in
65+
cs50) echo "ghcr.io/bootcs-cn/bootcs-cli:cs50" ;;
66+
python) echo "ghcr.io/bootcs-cn/bootcs-cli:python" ;;
67+
java) echo "ghcr.io/bootcs-cn/bootcs-cli:java" ;;
68+
*) echo "ghcr.io/bootcs-cn/bootcs-cli:latest" ;;
69+
esac
70+
}
71+
72+
# 解析命令
73+
if [[ "$1" == "check" && -n "$2" ]]; then
74+
COURSE=$(infer_course "$2")
75+
IMAGE=$(get_image "$COURSE")
76+
elif [[ -n "$BOOTCS_IMAGE" ]]; then
77+
IMAGE="$BOOTCS_IMAGE"
78+
else
79+
IMAGE="ghcr.io/bootcs-cn/bootcs-cli:latest"
80+
fi
4281
4382
# 运行容器
44-
docker run --rm -v "$(pwd)":/workspace "$BOOTCS_IMAGE" "$@"
83+
docker run --rm -v "$(pwd)":/workspace "$IMAGE" "$@"
4584
EOF
4685

4786
chmod +x "$BOOTCS_SCRIPT"
4887

88+
# 如果指定了课程,预拉取镜像
89+
if [[ -n "$COURSE" && -n "${COURSES[$COURSE]}" ]]; then
90+
echo -e "${BLUE}📦 Pulling ${COURSE} course image...${NC}"
91+
docker pull "${COURSES[$COURSE]}" || true
92+
fi
93+
4994
# 检查 PATH
5095
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
5196
echo ""
@@ -69,8 +114,8 @@ fi
69114
echo -e "${GREEN}✅ BootCS CLI installed successfully!${NC}"
70115
echo ""
71116
echo "Usage:"
72-
echo " bootcs check cs50/credit # 检查作业"
73-
echo " bootcs --help # 查看帮助"
117+
echo " bootcs check cs50/credit # 自动使用 CS50 镜像"
118+
echo " bootcs check python/hello # 自动使用 Python 镜像"
119+
echo " bootcs --help # 查看帮助"
74120
echo ""
75-
echo "To use a different course image:"
76-
echo " BOOTCS_IMAGE=ghcr.io/bootcs-cn/bootcs-cli:other bootcs check ..."
121+
echo "Available courses: ${!COURSES[*]}"

0 commit comments

Comments
 (0)