Separate download.Get() function and add ~name parameter to git downloads
This commit is contained in:
parent
2b6815e287
commit
c0e535c630
|
@ -23,6 +23,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"errors"
|
"errors"
|
||||||
|
"hash"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -67,6 +68,22 @@ func Get(ctx context.Context, opts GetOptions) error {
|
||||||
}
|
}
|
||||||
query := src.Query()
|
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")
|
tag := query.Get("~tag")
|
||||||
query.Del("~tag")
|
query.Del("~tag")
|
||||||
|
|
||||||
|
@ -79,6 +96,9 @@ func Get(ctx context.Context, opts GetOptions) error {
|
||||||
depthStr := query.Get("~depth")
|
depthStr := query.Get("~depth")
|
||||||
query.Del("~depth")
|
query.Del("~depth")
|
||||||
|
|
||||||
|
name := query.Get("~name")
|
||||||
|
query.Del("~name")
|
||||||
|
|
||||||
var refName plumbing.ReferenceName
|
var refName plumbing.ReferenceName
|
||||||
if tag != "" {
|
if tag != "" {
|
||||||
refName = plumbing.NewTagReferenceName(tag)
|
refName = plumbing.NewTagReferenceName(tag)
|
||||||
|
@ -86,12 +106,13 @@ func Get(ctx context.Context, opts GetOptions) error {
|
||||||
refName = plumbing.NewBranchReferenceName(branch)
|
refName = plumbing.NewBranchReferenceName(branch)
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(src.Scheme, "git+") {
|
|
||||||
src.Scheme = strings.TrimPrefix(src.Scheme, "git+")
|
src.Scheme = strings.TrimPrefix(src.Scheme, "git+")
|
||||||
src.RawQuery = query.Encode()
|
src.RawQuery = query.Encode()
|
||||||
|
|
||||||
name := path.Base(src.Path)
|
if name == "" {
|
||||||
|
name = path.Base(src.Path)
|
||||||
name = strings.TrimSuffix(name, ".git")
|
name = strings.TrimSuffix(name, ".git")
|
||||||
|
}
|
||||||
|
|
||||||
dstDir := opts.Destination
|
dstDir := opts.Destination
|
||||||
if opts.EncloseGit {
|
if opts.EncloseGit {
|
||||||
|
@ -132,7 +153,9 @@ func Get(ctx context.Context, opts GetOptions) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
return w.Checkout(checkoutOpts)
|
return w.Checkout(checkoutOpts)
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
func getFile(ctx context.Context, src *url.URL, query url.Values, opts GetOptions) error {
|
||||||
name := query.Get("~name")
|
name := query.Get("~name")
|
||||||
query.Del("~name")
|
query.Del("~name")
|
||||||
|
|
||||||
|
@ -183,6 +206,16 @@ func Get(ctx context.Context, opts GetOptions) error {
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} 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)
|
r := io.TeeReader(input, hash)
|
||||||
fname := format.Name()
|
fname := format.Name()
|
||||||
|
|
||||||
|
@ -249,8 +282,6 @@ func Get(ctx context.Context, opts GetOptions) error {
|
||||||
return ErrChecksumMismatch
|
return ErrChecksumMismatch
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue