Skip to content

Commit

Permalink
export the context field of the client
Browse files Browse the repository at this point in the history
  • Loading branch information
azr committed Jan 16, 2019
1 parent 78f5262 commit bde4550
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions client_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
9 changes: 4 additions & 5 deletions cmd/go-getter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os/signal"
"sync"

"github.com/hashicorp/go-getter"
getter "github.com/hashicorp/go-getter"
)

func main() {
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion get_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit bde4550

Please sign in to comment.