Implement local file sources
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Elara 2023-10-06 15:40:17 -07:00
parent b758eb39f0
commit 383b886472
3 changed files with 38 additions and 15 deletions

View File

@ -87,6 +87,7 @@ type Options struct {
CacheDisabled bool
PostprocDisabled bool
Progress io.Writer
LocalDir string
}
func (opts Options) NewHash() (hash.Hash, error) {
@ -182,6 +183,7 @@ func Download(ctx context.Context, opts Options) (err error) {
URL: opts.URL,
Destination: cacheDir,
Progress: opts.Progress,
LocalDir: opts.LocalDir,
})
if err != nil {
return err
@ -229,6 +231,7 @@ func Download(ctx context.Context, opts Options) (err error) {
URL: opts.URL,
Destination: cacheDir,
Progress: opts.Progress,
LocalDir: opts.LocalDir,
})
if err != nil {
return err

View File

@ -68,13 +68,30 @@ func (FileDownloader) Download(opts Options) (Type, string, error) {
u.RawQuery = query.Encode()
res, err := http.Get(u.String())
if err != nil {
return 0, "", err
}
if name == "" {
name = getFilename(res)
var r io.ReadCloser
var size int64
if u.Scheme == "local" {
localFl, err := os.Open(filepath.Join(opts.LocalDir, u.Path))
if err != nil {
return 0, "", err
}
fi, err := localFl.Stat()
if err != nil {
return 0, "", err
}
r = localFl
size = fi.Size()
name = fi.Name()
} else {
res, err := http.Get(u.String())
if err != nil {
return 0, "", err
}
size = res.ContentLength
if name == "" {
name = getFilename(res)
}
r = res.Body
}
opts.PostprocDisabled = archive == "false"
@ -89,7 +106,7 @@ func (FileDownloader) Download(opts Options) (Type, string, error) {
var bar io.WriteCloser
if opts.Progress != nil {
bar = progressbar.NewOptions64(
res.ContentLength,
size,
progressbar.OptionSetDescription(name),
progressbar.OptionSetWriter(opts.Progress),
progressbar.OptionShowBytes(true),
@ -120,11 +137,11 @@ func (FileDownloader) Download(opts Options) (Type, string, error) {
w = io.MultiWriter(fl, bar)
}
_, err = io.Copy(w, res.Body)
_, err = io.Copy(w, r)
if err != nil {
return 0, "", err
}
res.Body.Close()
r.Close()
if opts.Hash != nil {
sum := h.Sum(nil)
@ -142,14 +159,14 @@ func (FileDownloader) Download(opts Options) (Type, string, error) {
return 0, "", err
}
format, r, err := archiver.Identify(name, fl)
format, ar, err := archiver.Identify(name, fl)
if err == archiver.ErrNoMatch {
return TypeFile, name, nil
} else if err != nil {
return 0, "", err
}
err = extractFile(r, format, name, opts)
err = extractFile(ar, format, name, opts)
if err != nil {
return 0, "", err
}

View File

@ -146,7 +146,7 @@ func BuildPackage(ctx context.Context, opts types.BuildOpts) ([]string, []string
log.Info("Downloading sources").Send()
err = getSources(ctx, dirs.SrcDir, vars)
err = getSources(ctx, dirs, vars)
if err != nil {
return nil, nil, err
}
@ -693,7 +693,7 @@ func createBuildEnvVars(info *distro.OSRelease, dirs types.Directories) []string
}
// getSources downloads the sources from the script.
func getSources(ctx context.Context, srcdir string, bv *types.BuildVars) error {
func getSources(ctx context.Context, dirs types.Directories, bv *types.BuildVars) error {
log := loggerctx.From(ctx)
if len(bv.Sources) != len(bv.Checksums) {
log.Fatal("The checksums array must be the same length as sources").Send()
@ -703,10 +703,13 @@ func getSources(ctx context.Context, srcdir string, bv *types.BuildVars) error {
opts := dl.Options{
Name: fmt.Sprintf("%s[%d]", bv.Name, i),
URL: src,
Destination: srcdir,
Destination: dirs.SrcDir,
Progress: os.Stderr,
LocalDir: dirs.ScriptDir,
}
println("ld", opts.LocalDir)
if !strings.EqualFold(bv.Checksums[i], "SKIP") {
// If the checksum contains a colon, use the part before the colon
// as the algorithm and the part after as the actual checksum.