From d45d913c9e0d088f339c007f14d2fcaa7b9c8c74 Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Wed, 27 Mar 2019 02:44:38 +0300 Subject: Flexible run policies Options refactoring --- mixins.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100755 mixins.go (limited to 'mixins.go') diff --git a/mixins.go b/mixins.go new file mode 100755 index 0000000..65309e4 --- /dev/null +++ b/mixins.go @@ -0,0 +1,41 @@ +package rutina + +import ( + "context" + "log" + "os" +) + +type Mixin interface { + apply(*Rutina) +} + +type MixinContext struct { + Context context.Context +} + +func WithContext(context context.Context) *MixinContext { + return &MixinContext{Context: context} +} + +func (o MixinContext) apply(r *Rutina) { + ctx, cancel := context.WithCancel(o.Context) + r.ctx = ctx + r.Cancel = cancel +} + +type MixinLogger struct { + Logger *log.Logger +} + +func WithLogger(logger *log.Logger) *MixinLogger { + return &MixinLogger{Logger: logger} +} + +func WithStdLogger() *MixinLogger { + return &MixinLogger{Logger: log.New(os.Stdout, "rutina", log.LstdFlags)} +} + +func (o MixinLogger) apply(r *Rutina) { + r.logger = o.Logger +} -- cgit v1.2.3