跳到主要内容

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

浏览文件

download.go

81 行 · 2.6 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package fileutils
5
6import (
7 "context"
8 "errors"
9 "fmt"
10 "path"
11
12 "github.com/sirupsen/logrus"
13
14 "github.com/lima-vm/lima/v2/pkg/downloader"
15 "github.com/lima-vm/lima/v2/pkg/limatype"
16)
17
18// ErrSkipped is returned when the downloader did not attempt to download the specified file.
19var ErrSkipped = errors.New("skipped to download")
20
21// DownloadFile downloads a file to the cache, optionally copying it to the destination. Returns path in cache.
22func DownloadFile(ctx context.Context, dest string, f limatype.File, decompress bool, description string, expectedArch limatype.Arch, supportedImageFormats []string) (string, error) {
23 if f.Arch != expectedArch {
24 return "", fmt.Errorf("%w: %#q: unsupported arch: %#q", ErrSkipped, f.Location, f.Arch)
25 }
26 fields := logrus.Fields{"location": f.Location, "arch": f.Arch, "digest": f.Digest}
27 logrus.WithFields(fields).Infof("Attempting to download %s", description)
28 opts := []downloader.Opt{
29 downloader.WithCache(),
30 downloader.WithDecompress(decompress),
31 downloader.WithDescription(fmt.Sprintf("%s (%s)", description, path.Base(f.Location))),
32 downloader.WithExpectedDigest(f.Digest),
33 }
34 if len(supportedImageFormats) > 0 {
35 opts = append(opts, downloader.WithImageFormats(supportedImageFormats))
36 }
37
38 res, err := downloader.Download(ctx, dest, f.Location, opts...)
39 if err != nil {
40 return "", fmt.Errorf("failed to download %#q: %w", f.Location, err)
41 }
42 logrus.Debugf("res.ValidatedDigest=%v", res.ValidatedDigest)
43 switch res.Status {
44 case downloader.StatusDownloaded:
45 logrus.Infof("Downloaded %s from %#q", description, f.Location)
46 case downloader.StatusUsedCache:
47 logrus.Infof("Using cache %#q", res.CachePath)
48 default:
49 logrus.Warnf("Unexpected result from downloader.Download(): %+v", res)
50 }
51 return res.CachePath, nil
52}
53
54// CachedFile checks if a file is in the cache, validating the digest if it is available. Returns path in cache.
55func CachedFile(f limatype.File) (string, error) {
56 res, err := downloader.Cached(f.Location,
57 downloader.WithCache(),
58 downloader.WithExpectedDigest(f.Digest))
59 if err != nil {
60 return "", fmt.Errorf("cache did not contain %#q: %w", f.Location, err)
61 }
62 return res.CachePath, nil
63}
64
65// Errors compose multiple into a single error.
66// Errors filters out ErrSkipped.
67func Errors(errs []error) error {
68 var finalErr error
69 for _, err := range errs {
70 if errors.Is(err, ErrSkipped) {
71 logrus.Debug(err)
72 } else {
73 finalErr = errors.Join(finalErr, err)
74 }
75 }
76 if len(errs) > 0 && finalErr == nil {
77 // errs only contains ErrSkipped
78 finalErr = fmt.Errorf("%v", errs)
79 }
80 return finalErr
81}