diff options
author | Alexander Kiryukhin <alexander@kiryukhin.su> | 2019-01-17 07:54:39 +0300 |
---|---|---|
committer | Alexander Kiryukhin <alexander@kiryukhin.su> | 2019-01-17 07:54:39 +0300 |
commit | c5776ba6a3d8303650208ee8abc8f63cadf5127a (patch) | |
tree | cf92f3d64a46f7167b9857688e5344554b2a8d69 /options.go | |
parent | d64ca3bd06c6a2c702f3604d95658566907a9beb (diff) |
Many changesv0.2.0
Diffstat (limited to 'options.go')
-rwxr-xr-x | options.go | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/options.go b/options.go new file mode 100755 index 0000000..8144dde --- /dev/null +++ b/options.go @@ -0,0 +1,51 @@ +package rutina + +import ( + "context" + "log" + "os" +) + +type Option interface { + apply(*Rutina) +} + +type OptionContext struct { + Context context.Context +} + +func WithContext(context context.Context) *OptionContext { + return &OptionContext{Context: context} +} + +func (o OptionContext) apply(r *Rutina) { + ctx, cancel := context.WithCancel(o.Context) + r.ctx = ctx + r.Cancel = cancel +} + +type OptionLogger struct { + Logger *log.Logger +} + +func WithLogger(logger *log.Logger) *OptionLogger { + return &OptionLogger{Logger: logger} +} + +func WithStdLogger() *OptionLogger { + return &OptionLogger{Logger: log.New(os.Stdout, "rutina", log.LstdFlags)} +} + +func (o OptionLogger) apply(r *Rutina) { + r.logger = o.Logger +} + +type OptionCancelByError struct{} + +func WithCancelByError() *OptionCancelByError { + return &OptionCancelByError{} +} + +func (OptionCancelByError) apply(r *Rutina) { + r.cancelByError = true +} |