From cd6aaf87dc01f92805d845f0b19a29715c57c6a3 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 8 Jun 2022 13:51:31 -0400 Subject: [PATCH] check proto for http getSubdir too Use the same protocol switching checks when there's a subdir, passing the limited set of getters if there is wrapping client. --- get_http.go | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/get_http.go b/get_http.go index 24641353f..a4d273f14 100644 --- a/get_http.go +++ b/get_http.go @@ -294,10 +294,6 @@ func (g *HttpGetter) Get(dst string, u *url.URL) error { // If there is a subdir component, then we download the root separately // into a temporary directory, then copy over the proper subdir. source, subDir := SourceDirSubdir(source) - if subDir != "" { - // We have a subdir, time to jump some hoops - return g.getSubdir(ctx, dst, source, subDir) - } var opts []ClientOption @@ -333,17 +329,29 @@ func (g *HttpGetter) Get(dst string, u *url.URL) error { opts = g.client.Options } - // If the client is nil, we know we're using the HttpGetter directly. In this case, - // we don't know exactly which protocols are configued, but we can make a good guess. + // If the client is nil, we know we're using the HttpGetter directly. In + // this case, we don't know exactly which protocols are configured, but we + // can make a good guess. // // This prevents all default getters from being allowed when only using the // HttpGetter directly. To enable protocol switching, a client "wrapper" must // be used. if g.client == nil { - opts = append(opts, WithGetters(map[string]Getter{ - "http": g, - "https": g, - })) + switch { + case subDir != "": + // If there's a subdirectory, we will also need a file getter to + // unpack it. + opts = append(opts, WithGetters(map[string]Getter{ + "file": new(FileGetter), + "http": g, + "https": g, + })) + default: + opts = append(opts, WithGetters(map[string]Getter{ + "http": g, + "https": g, + })) + } } // Ensure we pass along the context we constructed in this function. @@ -352,6 +360,11 @@ func (g *HttpGetter) Get(dst string, u *url.URL) error { // which could be setup, if configured, at the top of this function. opts = append(opts, WithContext(ctx)) + if subDir != "" { + // We have a subdir, time to jump some hoops + return g.getSubdir(ctx, dst, source, subDir, opts...) + } + // Note: this allows the protocol to be switched to another configured getters. return Get(dst, source, opts...) } @@ -495,7 +508,7 @@ func (g *HttpGetter) GetFile(dst string, src *url.URL) error { // getSubdir downloads the source into the destination, but with // the proper subdir. -func (g *HttpGetter) getSubdir(ctx context.Context, dst, source, subDir string) error { +func (g *HttpGetter) getSubdir(ctx context.Context, dst, source, subDir string, opts ...ClientOption) error { // Create a temporary directory to store the full source. This has to be // a non-existent directory. td, tdcloser, err := safetemp.Dir("", "getter") @@ -504,10 +517,6 @@ func (g *HttpGetter) getSubdir(ctx context.Context, dst, source, subDir string) } defer tdcloser.Close() - var opts []ClientOption - if g.client != nil { - opts = g.client.Options - } // Download that into the given directory if err := Get(td, source, opts...); err != nil { return err