github.com/bkosm/gompose/v2@v2.3.1/options.go (about)

     1  package gompose
     2  
     3  import (
     4  	"net/http"
     5  	"os"
     6  	"time"
     7  )
     8  
     9  type (
    10  	customFile string
    11  	timeBased  struct {
    12  		times        uint
    13  		timeout      time.Duration
    14  		pollInterval time.Duration
    15  	}
    16  	retry struct {
    17  		times    uint
    18  		interval time.Duration
    19  	}
    20  	responseVerifier func(*http.Response) (bool, error)
    21  	up               struct {
    22  		wait           ReadyOrErrChan
    23  		onSignal       func(os.Signal)
    24  		customServices []string
    25  	}
    26  
    27  	// Option is a struct that configures gompose options, shared by all gompose commands.
    28  	Option struct {
    29  		withCustomFileFunc       func(*customFile)
    30  		withTimeBasedFunc        func(*timeBased)
    31  		withResponseVerifierFunc func(*responseVerifier)
    32  		withUpFunc               func(*up)
    33  		withRetryFunc            func(*retry)
    34  	}
    35  )
    36  
    37  // CustomFile sets the path of a custom compose file to be used by gompose.
    38  func CustomFile(filepath string) Option {
    39  	return Option{
    40  		withCustomFileFunc: func(opt *customFile) {
    41  			if opt == nil {
    42  				return
    43  			}
    44  			*opt = customFile(filepath)
    45  		},
    46  	}
    47  }
    48  
    49  // Times sets the amount of retries that should take place before failing.
    50  func Times(times uint) Option {
    51  	return Option{
    52  		withTimeBasedFunc: func(opt *timeBased) {
    53  			opt.times = times
    54  		},
    55  	}
    56  }
    57  
    58  // Timeout sets the duration after which lack of success should propagate failure.
    59  func Timeout(timeout time.Duration) Option {
    60  	return Option{
    61  		withTimeBasedFunc: func(opt *timeBased) {
    62  			opt.timeout = timeout
    63  		},
    64  	}
    65  }
    66  
    67  // PollInterval sets the duration that is waited between successive attempts.
    68  func PollInterval(pollInterval time.Duration) Option {
    69  	return Option{
    70  		withTimeBasedFunc: func(opt *timeBased) {
    71  			opt.pollInterval = pollInterval
    72  		},
    73  	}
    74  }
    75  
    76  // ResponseVerifier sets the function that verifies a http.Response obtained through ReadyOnHttp.
    77  func ResponseVerifier(verifier func(response *http.Response) (bool, error)) Option {
    78  	return Option{
    79  		withResponseVerifierFunc: func(opt *responseVerifier) {
    80  			if opt == nil {
    81  				return
    82  			}
    83  			*opt = verifier
    84  		},
    85  	}
    86  }
    87  
    88  // Wait sets the wait channel for a gompose command.
    89  func Wait(channel ReadyOrErrChan) Option {
    90  	return Option{
    91  		withUpFunc: func(opt *up) {
    92  			opt.wait = channel
    93  		},
    94  	}
    95  }
    96  
    97  // SignalCallback sets the callback that is executed whenever a system interrupt happens while the command
    98  // is executing or when the system awaits readiness.
    99  func SignalCallback(callback func(os.Signal)) Option {
   100  	return Option{
   101  		withUpFunc: func(opt *up) {
   102  			opt.onSignal = callback
   103  		},
   104  	}
   105  }
   106  
   107  // CustomServices sets a list of services, picked from the default spec or the one provided through CustomFile,
   108  // that should be started with Up.
   109  func CustomServices(services ...string) Option {
   110  	return Option{
   111  		withUpFunc: func(opt *up) {
   112  			opt.customServices = services
   113  		},
   114  	}
   115  }
   116  
   117  // RetryCommand will attempt to run the command again in case of failure (e.g. when running Up)
   118  // given amount of times, each after specified interval.
   119  func RetryCommand(times uint, interval time.Duration) Option {
   120  	return Option{
   121  		withRetryFunc: func(opt *retry) {
   122  			opt.times = times
   123  			opt.interval = interval
   124  		},
   125  	}
   126  }