|
1 | 1 | #!/bin/bash |
2 | 2 | # 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 课程 |
4 | 7 |
|
5 | 8 | set -e |
6 | 9 |
|
7 | 10 | # 颜色定义 |
8 | 11 | RED='\033[0;31m' |
9 | 12 | GREEN='\033[0;32m' |
10 | 13 | YELLOW='\033[1;33m' |
| 14 | +BLUE='\033[0;34m' |
11 | 15 | NC='\033[0m' # No Color |
12 | 16 |
|
| 17 | +# 获取课程参数 |
| 18 | +COURSE="${1:-}" |
| 19 | + |
13 | 20 | echo -e "${GREEN}🚀 Installing BootCS CLI...${NC}" |
14 | 21 |
|
15 | 22 | # 检查 Docker 是否安装 |
|
29 | 36 | INSTALL_DIR="${HOME}/.local/bin" |
30 | 37 | mkdir -p "$INSTALL_DIR" |
31 | 38 |
|
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 脚本 |
33 | 48 | BOOTCS_SCRIPT="$INSTALL_DIR/bootcs" |
34 | 49 |
|
35 | 50 | cat > "$BOOTCS_SCRIPT" << 'EOF' |
36 | 51 | #!/bin/bash |
37 | 52 | # BootCS CLI Wrapper |
38 | 53 | # https://bootcs.cn |
39 | 54 |
|
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 |
42 | 81 |
|
43 | 82 | # 运行容器 |
44 | | -docker run --rm -v "$(pwd)":/workspace "$BOOTCS_IMAGE" "$@" |
| 83 | +docker run --rm -v "$(pwd)":/workspace "$IMAGE" "$@" |
45 | 84 | EOF |
46 | 85 |
|
47 | 86 | chmod +x "$BOOTCS_SCRIPT" |
48 | 87 |
|
| 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 | + |
49 | 94 | # 检查 PATH |
50 | 95 | if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then |
51 | 96 | echo "" |
|
69 | 114 | echo -e "${GREEN}✅ BootCS CLI installed successfully!${NC}" |
70 | 115 | echo "" |
71 | 116 | 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 # 查看帮助" |
74 | 120 | 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