大量的更新
This commit is contained in:
@@ -208,4 +208,116 @@ func addDockerSubcommands(cmd *cobra.Command) {
|
||||
|
||||
func addDockerComposeSubcommands(cmd *cobra.Command) {
|
||||
|
||||
installCmd := &cobra.Command{
|
||||
Use: "online [version]",
|
||||
Short: "安装Docker Compose",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
log.Info("Installing Docker Compose...")
|
||||
|
||||
// 检查参数
|
||||
if len(args) > 0 {
|
||||
fmt.Printf("Installing Docker Compose version: %s\n", args[0])
|
||||
}
|
||||
|
||||
// 默认安装最新版本
|
||||
version := "latest"
|
||||
if len(args) > 0 {
|
||||
version = args[0]
|
||||
}
|
||||
|
||||
// 如果是最新版本,则通过GitHub API查询
|
||||
if version == "latest" {
|
||||
latestVersion, err := op.GetLatestGithubReleaseVersion("docker", "compose")
|
||||
if err != nil {
|
||||
log.Error("获取Docker Compose最新版本失败: %s", err)
|
||||
} else {
|
||||
version = latestVersion
|
||||
log.Info("获取到Docker Compose最新版本: %s", version)
|
||||
}
|
||||
}
|
||||
|
||||
log.Info("安装 Docker Compose 版本: %s", version)
|
||||
|
||||
// 检查是否可以连接互联网
|
||||
if config.CanConnectInternet() <= 1 {
|
||||
log.Error("服务器无法连接互联网,无法在线安装 Docker Compose")
|
||||
return
|
||||
}
|
||||
|
||||
// 检查 Docker 是否已安装
|
||||
dockerExists := op.CommandExistsByPath("docker")
|
||||
if !dockerExists {
|
||||
log.Error("Docker 未安装,请先安装 Docker")
|
||||
return
|
||||
}
|
||||
|
||||
// 根据架构选择合适的下载链接
|
||||
arch := runtime.GOARCH
|
||||
downloadURL := fmt.Sprintf("https://github.com/docker/compose/releases/download/%s/docker-compose-linux-%s", version, arch)
|
||||
|
||||
log.Info("Downloading Docker Compose from: %s", downloadURL)
|
||||
// 下载 Docker Compose
|
||||
ok, ccc := op.DownloadFile(downloadURL, "/usr/local/bin/docker-compose")
|
||||
if !ok {
|
||||
log.Error("下载 Docker Compose 失败: %s", ccc)
|
||||
return
|
||||
}
|
||||
|
||||
// 设置执行权限
|
||||
chmodCmd := []string{"chmod", "+x", "/usr/local/bin/docker-compose"}
|
||||
ok, resultLog := op.SingleLineCommandExecutor(chmodCmd)
|
||||
if !ok {
|
||||
log.Error("设置 Docker Compose 权限失败: %s", resultLog)
|
||||
return
|
||||
}
|
||||
|
||||
// 创建软链接
|
||||
linkCmd := []string{"ln", "-sf", "/usr/local/bin/docker-compose", "/usr/bin/docker-compose"}
|
||||
op.SingleLineCommandExecutor(linkCmd)
|
||||
|
||||
// 验证安装
|
||||
verifyCmd := []string{"docker-compose", "--version"}
|
||||
ok, resultLog = op.SingleLineCommandExecutor(verifyCmd)
|
||||
if !ok {
|
||||
log.Error("Docker Compose 安装验证失败: %s", resultLog)
|
||||
return
|
||||
}
|
||||
|
||||
log.Info("Docker Compose 安装成功: %s", resultLog)
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
removeCmd := &cobra.Command{
|
||||
Use: "remove",
|
||||
Short: "卸载Docker Compose",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
log.Info("Removing Docker Compose...")
|
||||
},
|
||||
}
|
||||
|
||||
localCmd := &cobra.Command{
|
||||
Use: "local [path]",
|
||||
Short: "本地安装Docker Compose",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
log.Info("Installing Docker Compose from local file...")
|
||||
},
|
||||
}
|
||||
|
||||
versionCmd := &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "查看Docker Compose版本",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
log.Info("Docker Compose version...")
|
||||
},
|
||||
}
|
||||
|
||||
cmd.AddCommand(
|
||||
installCmd,
|
||||
removeCmd,
|
||||
localCmd,
|
||||
versionCmd,
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user