Implement local file sources
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
This commit is contained in:
parent
b758eb39f0
commit
383b886472
|
@ -87,6 +87,7 @@ type Options struct {
|
||||||
CacheDisabled bool
|
CacheDisabled bool
|
||||||
PostprocDisabled bool
|
PostprocDisabled bool
|
||||||
Progress io.Writer
|
Progress io.Writer
|
||||||
|
LocalDir string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (opts Options) NewHash() (hash.Hash, error) {
|
func (opts Options) NewHash() (hash.Hash, error) {
|
||||||
|
@ -182,6 +183,7 @@ func Download(ctx context.Context, opts Options) (err error) {
|
||||||
URL: opts.URL,
|
URL: opts.URL,
|
||||||
Destination: cacheDir,
|
Destination: cacheDir,
|
||||||
Progress: opts.Progress,
|
Progress: opts.Progress,
|
||||||
|
LocalDir: opts.LocalDir,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -229,6 +231,7 @@ func Download(ctx context.Context, opts Options) (err error) {
|
||||||
URL: opts.URL,
|
URL: opts.URL,
|
||||||
Destination: cacheDir,
|
Destination: cacheDir,
|
||||||
Progress: opts.Progress,
|
Progress: opts.Progress,
|
||||||
|
LocalDir: opts.LocalDir,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -68,13 +68,30 @@ func (FileDownloader) Download(opts Options) (Type, string, error) {
|
||||||
|
|
||||||
u.RawQuery = query.Encode()
|
u.RawQuery = query.Encode()
|
||||||
|
|
||||||
res, err := http.Get(u.String())
|
var r io.ReadCloser
|
||||||
if err != nil {
|
var size int64
|
||||||
return 0, "", err
|
if u.Scheme == "local" {
|
||||||
}
|
localFl, err := os.Open(filepath.Join(opts.LocalDir, u.Path))
|
||||||
|
if err != nil {
|
||||||
if name == "" {
|
return 0, "", err
|
||||||
name = getFilename(res)
|
}
|
||||||
|
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"
|
opts.PostprocDisabled = archive == "false"
|
||||||
|
@ -89,7 +106,7 @@ func (FileDownloader) Download(opts Options) (Type, string, error) {
|
||||||
var bar io.WriteCloser
|
var bar io.WriteCloser
|
||||||
if opts.Progress != nil {
|
if opts.Progress != nil {
|
||||||
bar = progressbar.NewOptions64(
|
bar = progressbar.NewOptions64(
|
||||||
res.ContentLength,
|
size,
|
||||||
progressbar.OptionSetDescription(name),
|
progressbar.OptionSetDescription(name),
|
||||||
progressbar.OptionSetWriter(opts.Progress),
|
progressbar.OptionSetWriter(opts.Progress),
|
||||||
progressbar.OptionShowBytes(true),
|
progressbar.OptionShowBytes(true),
|
||||||
|
@ -120,11 +137,11 @@ func (FileDownloader) Download(opts Options) (Type, string, error) {
|
||||||
w = io.MultiWriter(fl, bar)
|
w = io.MultiWriter(fl, bar)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = io.Copy(w, res.Body)
|
_, err = io.Copy(w, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, "", err
|
return 0, "", err
|
||||||
}
|
}
|
||||||
res.Body.Close()
|
r.Close()
|
||||||
|
|
||||||
if opts.Hash != nil {
|
if opts.Hash != nil {
|
||||||
sum := h.Sum(nil)
|
sum := h.Sum(nil)
|
||||||
|
@ -142,14 +159,14 @@ func (FileDownloader) Download(opts Options) (Type, string, error) {
|
||||||
return 0, "", err
|
return 0, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
format, r, err := archiver.Identify(name, fl)
|
format, ar, err := archiver.Identify(name, fl)
|
||||||
if err == archiver.ErrNoMatch {
|
if err == archiver.ErrNoMatch {
|
||||||
return TypeFile, name, nil
|
return TypeFile, name, nil
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return 0, "", err
|
return 0, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = extractFile(r, format, name, opts)
|
err = extractFile(ar, format, name, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, "", err
|
return 0, "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -146,7 +146,7 @@ func BuildPackage(ctx context.Context, opts types.BuildOpts) ([]string, []string
|
||||||
|
|
||||||
log.Info("Downloading sources").Send()
|
log.Info("Downloading sources").Send()
|
||||||
|
|
||||||
err = getSources(ctx, dirs.SrcDir, vars)
|
err = getSources(ctx, dirs, vars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
@ -693,7 +693,7 @@ func createBuildEnvVars(info *distro.OSRelease, dirs types.Directories) []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// getSources downloads the sources from the script.
|
// 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)
|
log := loggerctx.From(ctx)
|
||||||
if len(bv.Sources) != len(bv.Checksums) {
|
if len(bv.Sources) != len(bv.Checksums) {
|
||||||
log.Fatal("The checksums array must be the same length as sources").Send()
|
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{
|
opts := dl.Options{
|
||||||
Name: fmt.Sprintf("%s[%d]", bv.Name, i),
|
Name: fmt.Sprintf("%s[%d]", bv.Name, i),
|
||||||
URL: src,
|
URL: src,
|
||||||
Destination: srcdir,
|
Destination: dirs.SrcDir,
|
||||||
Progress: os.Stderr,
|
Progress: os.Stderr,
|
||||||
|
LocalDir: dirs.ScriptDir,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
println("ld", opts.LocalDir)
|
||||||
|
|
||||||
if !strings.EqualFold(bv.Checksums[i], "SKIP") {
|
if !strings.EqualFold(bv.Checksums[i], "SKIP") {
|
||||||
// If the checksum contains a colon, use the part before the colon
|
// If the checksum contains a colon, use the part before the colon
|
||||||
// as the algorithm and the part after as the actual checksum.
|
// as the algorithm and the part after as the actual checksum.
|
||||||
|
|
Loading…
Reference in New Issue