Files
ProjectOctopus/agent-wdd/utils/DownloadUtils.go
zeaslity 72bc56b5e5 Enhance Zsh and Config Commands, Update Network Configuration
- Implemented comprehensive Zsh installation command with multiple network scenarios
- Added 'config show' subcommand to display agent configuration
- Updated version command to print version information
- Modified Network configuration to clarify internet connectivity status
- Improved download utility with additional file existence checks
- Updated agent-wdd rules and documentation
2025-02-27 14:20:05 +08:00

49 lines
1017 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utils
import (
"fmt"
"io"
"net/http"
"os"
"time"
)
func DownloadFile(url string, path string) (bool, string) {
// 创建HTTP客户端
client := &http.Client{
Timeout: 5 * time.Second,
}
// 发送GET请求
resp, err := client.Get(url)
if err != nil {
return false, fmt.Sprintf("下载文件失败: %v", err)
}
defer resp.Body.Close()
// 检查响应状态码
if resp.StatusCode != http.StatusOK {
return false, fmt.Sprintf("下载文件失败HTTP状态码: %d", resp.StatusCode)
}
// 创建目标文件
out, err := os.Create(path)
if err != nil {
return false, fmt.Sprintf("创建文件失败: %v", err)
}
defer out.Close()
// 将响应内容写入文件
_, err = io.Copy(out, resp.Body)
if err != nil {
return false, fmt.Sprintf("写入文件失败: %v", err)
}
// 检查文件是否存在
if !FileExistAndNotNull(path) {
return false, fmt.Sprintf("文件下载失败: 文件为空 => %s", path)
}
return true, fmt.Sprintf("文件下载成功: %s", path)
}