From bde455061ffa8f9b2a8a8cf4fedbbdb32dc7b9e3 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 16 Jan 2019 10:43:02 +0100 Subject: [PATCH] export the context field of the client --- client.go | 6 +++--- client_option.go | 6 ++++-- cmd/go-getter/main.go | 9 ++++----- get_base.go | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/client.go b/client.go index b4cdca44b..007a78ba7 100644 --- a/client.go +++ b/client.go @@ -19,8 +19,8 @@ import ( // Using a client directly allows more fine-grained control over how downloading // is done, as well as customizing the protocols supported. type Client struct { - // ctx for cancellation - ctx context.Context + // Ctx for cancellation + Ctx context.Context // Src is the source URL to get. // @@ -291,7 +291,7 @@ func (c *Client) Get() error { return err } - return copyDir(c.ctx, realDst, subDir, false) + return copyDir(c.Ctx, realDst, subDir, false) } return nil diff --git a/client_option.go b/client_option.go index 99da2fa44..c1ee413b0 100644 --- a/client_option.go +++ b/client_option.go @@ -7,7 +7,9 @@ type ClientOption func(*Client) error // Configure configures a client with options. func (c *Client) Configure(opts ...ClientOption) error { - c.ctx = context.Background() + if c.Ctx == nil { + c.Ctx = context.Background() + } c.Options = opts for _, opt := range opts { err := opt(c) @@ -38,7 +40,7 @@ func (c *Client) Configure(opts ...ClientOption) error { // in order to be able to cancel a download in progress. func WithContext(ctx context.Context) func(*Client) error { return func(c *Client) error { - c.ctx = ctx + c.Ctx = ctx return nil } } diff --git a/cmd/go-getter/main.go b/cmd/go-getter/main.go index 01ce4e3d7..3cd028641 100644 --- a/cmd/go-getter/main.go +++ b/cmd/go-getter/main.go @@ -8,7 +8,7 @@ import ( "os/signal" "sync" - "github.com/hashicorp/go-getter" + getter "github.com/hashicorp/go-getter" ) func main() { @@ -41,16 +41,15 @@ func main() { log.Fatalf("Error getting wd: %s", err) } - ctx, cancel := context.WithCancel(context.Background()) - opts := []getter.ClientOption{ - getter.WithContext(ctx), - } + opts := []getter.ClientOption{} if *progress { opts = append(opts, getter.WithProgress(defaultProgressBar)) } + ctx, cancel := context.WithCancel(context.Background()) // Build the client client := &getter.Client{ + Ctx: ctx, Src: args[0], Dst: args[1], Pwd: pwd, diff --git a/get_base.go b/get_base.go index 47c7c630b..09e9b6313 100644 --- a/get_base.go +++ b/get_base.go @@ -16,5 +16,5 @@ func (g *getter) Context() context.Context { if g == nil || g.client == nil { return context.Background() } - return g.client.ctx + return g.client.Ctx }