Separate download.Get() function and add ~name parameter to git downloads

This commit is contained in:
Elara 2022-10-03 15:38:38 -07:00
parent 2b6815e287
commit c0e535c630
1 changed files with 168 additions and 137 deletions

View File

@ -23,6 +23,7 @@ import (
"context"
"crypto/sha256"
"errors"
"hash"
"io"
"net/http"
"net/url"
@ -67,6 +68,22 @@ func Get(ctx context.Context, opts GetOptions) error {
}
query := src.Query()
if strings.HasPrefix(src.Scheme, "git+") {
err = getGit(ctx, src, query, opts)
if err != nil {
return err
}
} else {
err = getFile(ctx, src, query, opts)
if err != nil {
return err
}
}
return nil
}
func getGit(ctx context.Context, src *url.URL, query url.Values, opts GetOptions) (err error) {
tag := query.Get("~tag")
query.Del("~tag")
@ -79,6 +96,9 @@ func Get(ctx context.Context, opts GetOptions) error {
depthStr := query.Get("~depth")
query.Del("~depth")
name := query.Get("~name")
query.Del("~name")
var refName plumbing.ReferenceName
if tag != "" {
refName = plumbing.NewTagReferenceName(tag)
@ -86,12 +106,13 @@ func Get(ctx context.Context, opts GetOptions) error {
refName = plumbing.NewBranchReferenceName(branch)
}
if strings.HasPrefix(src.Scheme, "git+") {
src.Scheme = strings.TrimPrefix(src.Scheme, "git+")
src.RawQuery = query.Encode()
name := path.Base(src.Path)
if name == "" {
name = path.Base(src.Path)
name = strings.TrimSuffix(name, ".git")
}
dstDir := opts.Destination
if opts.EncloseGit {
@ -132,7 +153,9 @@ func Get(ctx context.Context, opts GetOptions) error {
}
return w.Checkout(checkoutOpts)
} else {
}
func getFile(ctx context.Context, src *url.URL, query url.Values, opts GetOptions) error {
name := query.Get("~name")
query.Del("~name")
@ -183,6 +206,16 @@ func Get(ctx context.Context, opts GetOptions) error {
} else if err != nil {
return err
} else {
err = extractFile(ctx, input, hash, format, name, opts)
if err != nil {
return err
}
}
return nil
}
func extractFile(ctx context.Context, input io.Reader, hash hash.Hash, format archiver.Format, name string, opts GetOptions) (err error) {
r := io.TeeReader(input, hash)
fname := format.Name()
@ -249,8 +282,6 @@ func Get(ctx context.Context, opts GetOptions) error {
return ErrChecksumMismatch
}
}
}
}
return nil
}