github.com/weiwenhao/getter@v1.30.1/client_option.go (about)

     1  package getter
     2  
     3  import "context"
     4  
     5  // A ClientOption allows to configure a client
     6  type ClientOption func(*Client) error
     7  
     8  // Configure configures a client with options.
     9  func (c *Client) Configure(opts ...ClientOption) error {
    10  	if c.Ctx == nil {
    11  		c.Ctx = context.Background()
    12  	}
    13  	c.Options = opts
    14  	for _, opt := range opts {
    15  		err := opt(c)
    16  		if err != nil {
    17  			return err
    18  		}
    19  	}
    20  	// Default decompressor values
    21  	if c.Decompressors == nil {
    22  		c.Decompressors = Decompressors
    23  	}
    24  	// Default detector values
    25  	if c.Detectors == nil {
    26  		c.Detectors = Detectors
    27  	}
    28  	// Default getter values
    29  	if c.Getters == nil {
    30  		c.Getters = Getters
    31  	}
    32  
    33  	for _, getter := range c.Getters {
    34  		getter.SetClient(c)
    35  	}
    36  	return nil
    37  }
    38  
    39  // WithContext allows to pass a context to operation
    40  // in order to be able to cancel a download in progress.
    41  func WithContext(ctx context.Context) func(*Client) error {
    42  	return func(c *Client) error {
    43  		c.Ctx = ctx
    44  		return nil
    45  	}
    46  }