跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

pull.go

128 行 · 2.7 KB历史下载
1package e2eutils
2
3import (
4 "compress/gzip"
5 "fmt"
6 "io"
7 "net/http"
8 "os"
9 "os/exec"
10 "strings"
11
12 "github.com/sirupsen/logrus"
13)
14
15// DownloadVMImage downloads a VM image from url to given path
16// with download status
17func DownloadVMImage(downloadURL string, localImagePath string) error {
18 fmt.Println("Downloading VM image: " + downloadURL)
19
20 out, err := os.Create(localImagePath)
21 if err != nil {
22 return err
23 }
24 defer func() {
25 if err := out.Close(); err != nil {
26 logrus.Error(err)
27 }
28 }()
29
30 // #nosec
31 resp, err := http.Get(downloadURL)
32 if err != nil {
33 return err
34 }
35 defer func() {
36 if err := resp.Body.Close(); err != nil {
37 logrus.Error(err)
38 }
39 }()
40
41 if resp.StatusCode != http.StatusOK {
42 return fmt.Errorf("error downloading VM image %s: %s", downloadURL, resp.Status)
43 }
44
45 if _, err := io.Copy(out, resp.Body); err != nil {
46 return err
47 }
48
49 return nil
50}
51
52func Decompress(localPath string) (string, error) {
53 uncompressedPath := ""
54 if strings.HasSuffix(localPath, ".xz") {
55 uncompressedPath = strings.TrimSuffix(localPath, ".xz")
56 } else if strings.HasSuffix(localPath, ".gz") {
57 uncompressedPath = strings.TrimSuffix(localPath, ".gz")
58 }
59
60 if uncompressedPath == "" {
61 return "", fmt.Errorf("unsupported compression for %s", localPath)
62 }
63
64 // we remove the uncompressed file if already exists. Maybe it has been used earlier and can affect the tests result
65 os.Remove(uncompressedPath)
66
67 uncompressedFileWriter, err := os.OpenFile(uncompressedPath, os.O_CREATE|os.O_RDWR, 0600)
68 if err != nil {
69 return "", err
70 }
71
72 fmt.Printf("Extracting %s\n", localPath)
73 if strings.HasSuffix(localPath, ".xz") {
74 err = decompressXZ(localPath, uncompressedFileWriter)
75 } else {
76 err = decompressGZ(localPath, uncompressedFileWriter)
77 }
78
79 if err != nil {
80 return "", err
81 }
82 return uncompressedPath, nil
83}
84
85// Will error out if file without .xz already exists
86// Maybe extracting then renameing is a good idea here..
87// depends on xz: not pre-installed on mac, so it becomes a brew dependency
88func decompressXZ(src string, output io.Writer) error {
89 cmd := exec.Command("xzcat", "-T0", "-k", src)
90 stdOut, err := cmd.StdoutPipe()
91 if err != nil {
92 return err
93 }
94 cmd.Stderr = os.Stderr
95 go func() {
96 if _, err := io.Copy(output, stdOut); err != nil {
97 logrus.Error(err)
98 }
99 }()
100 return cmd.Run()
101}
102
103func decompressGZ(src string, output io.Writer) error {
104 file, err := os.Open(src)
105 if err != nil {
106 return err
107 }
108 defer file.Close()
109
110 // Create a gzip reader
111 reader, err := gzip.NewReader(file)
112 if err != nil {
113 return err
114 }
115 defer reader.Close()
116
117 for {
118 _, err := io.CopyN(output, reader, 1024)
119 if err != nil {
120 if err == io.EOF {
121 break
122 }
123 return err
124 }
125 }
126
127 return nil
128}