Skip to content

Commit 90ae03a

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

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

pkg/fileutils/download.go

Lines changed: 59 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,65 @@ 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+
source, err := os.Open(src)
27+
if err != nil {
28+
return err
29+
}
30+
defer source.Close()
31+
if err := os.MkdirAll(filepath.Dir(dst), 0o660); err != nil {
32+
return err
33+
}
34+
destination, err := os.Create(dst)
35+
if err != nil {
36+
return err
37+
}
38+
defer destination.Close()
39+
_, err = io.Copy(destination, source)
40+
return err
41+
}
42+
43+
func GetFileSHA256(filePath string) (string, error) {
44+
file, err := os.Open(filePath)
45+
if err != nil {
46+
return "", err
47+
}
48+
defer file.Close()
49+
hash := sha256.New()
50+
if _, err := io.Copy(hash, file); err != nil {
51+
return "", err
52+
}
53+
return fmt.Sprintf("sha256:%x", hash.Sum(nil)), nil
54+
}
55+
2156
// 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) {
57+
func DownloadFile(ctx context.Context, dest string, f limayaml.File, decompress bool, description string, expectedArch limayaml.Arch) (_ string, reterr error) {
2358
if f.Arch != expectedArch {
2459
return "", fmt.Errorf("%w: %q: unsupported arch: %q", ErrSkipped, f.Location, f.Arch)
2560
}
26-
fields := logrus.Fields{"location": f.Location, "arch": f.Arch, "digest": f.Digest}
61+
fields := logrus.Fields{"location": f.Location, "arch": f.Arch, "digest": f.Digest, "LocalPath": f.LocalPath}
62+
if f.LocalPath != "" {
63+
if _, err := os.Stat(f.LocalPath); err != nil {
64+
return "", err
65+
}
66+
logrus.WithFields(fields).Infof("Attempting to copy local file %s", description)
67+
if reterr != nil {
68+
defer os.Remove(dest)
69+
}
70+
if err := CopyFile(f.LocalPath, dest); err != nil {
71+
return "", fmt.Errorf("failed to copy file: %w", err)
72+
}
73+
sha256Sum, err := GetFileSHA256(dest)
74+
if err != nil {
75+
return "", fmt.Errorf("failed to getsha256: %w", err)
76+
}
77+
78+
if sha256Sum != f.Digest.String() {
79+
return "", fmt.Errorf("wrong sha256 for %s", dest)
80+
}
81+
return f.LocalPath, nil
82+
}
83+
2784
logrus.WithFields(fields).Infof("Attempting to download %s", description)
2885
res, err := downloader.Download(ctx, dest, f.Location,
2986
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)