Skip to content

Commit 9b8dd0b

Browse files
committed
feature: add LocalPath to support use local resource to skip download
Signed-off-by: ningmingxiao <[email protected]>
1 parent 2bfc04a commit 9b8dd0b

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

pkg/fileutils/download.go

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ package fileutils
55

66
import (
77
"context"
8+
"crypto/sha256"
89
"errors"
910
"fmt"
11+
"io"
12+
"os"
1013
"path"
14+
"path/filepath"
1115

1216
"github.com/sirupsen/logrus"
1317

@@ -18,12 +22,67 @@ import (
1822
// ErrSkipped is returned when the downloader did not attempt to download the specified file.
1923
var ErrSkipped = errors.New("skipped to download")
2024

25+
func CopyFile(src, dst string) error {
26+
27+
source, err := os.Open(src)
28+
if err != nil {
29+
return err
30+
}
31+
defer source.Close()
32+
if err := os.MkdirAll(filepath.Dir(dst), 0660); err != nil {
33+
return err
34+
}
35+
36+
destination, err := os.Create(dst)
37+
if err != nil {
38+
return err
39+
}
40+
defer destination.Close()
41+
_, err = io.Copy(destination, source)
42+
return err
43+
}
44+
45+
func GetFileSHA256(filePath string) (string, error) {
46+
file, err := os.Open(filePath)
47+
if err != nil {
48+
return "", err
49+
}
50+
defer file.Close()
51+
hash := sha256.New()
52+
if _, err := io.Copy(hash, file); err != nil {
53+
return "", err
54+
}
55+
return fmt.Sprintf("sha256:%x", hash.Sum(nil)), nil
56+
}
57+
2158
// DownloadFile downloads a file to the cache, optionally copying it to the destination. Returns path in cache.
22-
func DownloadFile(ctx context.Context, dest string, f limayaml.File, decompress bool, description string, expectedArch limayaml.Arch) (string, error) {
59+
func DownloadFile(ctx context.Context, dest string, f limayaml.File, decompress bool, description string, expectedArch limayaml.Arch) (_ string, reterr error) {
2360
if f.Arch != expectedArch {
2461
return "", fmt.Errorf("%w: %q: unsupported arch: %q", ErrSkipped, f.Location, f.Arch)
2562
}
26-
fields := logrus.Fields{"location": f.Location, "arch": f.Arch, "digest": f.Digest}
63+
fields := logrus.Fields{"location": f.Location, "arch": f.Arch, "digest": f.Digest, "LocalPath": f.LocalPath}
64+
if f.LocalPath != "" {
65+
if _, err := os.Stat(f.LocalPath); err != nil {
66+
return "", err
67+
}
68+
logrus.WithFields(fields).Infof("Attempting to copy local file %s", description)
69+
if reterr != nil {
70+
defer os.Remove(dest)
71+
}
72+
if err := CopyFile(f.LocalPath, dest); err != nil {
73+
return "", fmt.Errorf("failed to copy file: %w", err)
74+
}
75+
sha256Sum, err := GetFileSHA256(dest)
76+
if err != nil {
77+
return "", fmt.Errorf("failed to getsha256: %w", err)
78+
}
79+
80+
if sha256Sum != f.Digest.String() {
81+
return "", fmt.Errorf("wrong sha256 for %s", dest)
82+
}
83+
return f.LocalPath, nil
84+
}
85+
2786
logrus.WithFields(fields).Infof("Attempting to download %s", description)
2887
res, err := downloader.Download(ctx, dest, f.Location,
2988
downloader.WithCache(),

pkg/limayaml/limayaml.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,10 @@ type Rosetta struct {
121121
}
122122

123123
type File struct {
124-
Location string `yaml:"location" json:"location"` // REQUIRED
125-
Arch Arch `yaml:"arch,omitempty" json:"arch,omitempty"`
126-
Digest digest.Digest `yaml:"digest,omitempty" json:"digest,omitempty"`
124+
Location string `yaml:"location" json:"location"` // REQUIRED
125+
Arch Arch `yaml:"arch,omitempty" json:"arch,omitempty"`
126+
Digest digest.Digest `yaml:"digest,omitempty" json:"digest,omitempty"`
127+
LocalPath string `yaml:"localPath,omitempty" json:"localPath,omitempty"`
127128
}
128129

129130
type FileWithVMType struct {

0 commit comments

Comments
 (0)