From 1d595a68a54726df14f5878c15d4e50740de7ba0 Mon Sep 17 00:00:00 2001 From: Aleksey Khorev Date: Thu, 12 Nov 2020 00:31:50 +0300 Subject: [PATCH] ref: runner add: PlatformRunner interface --- cmd/cmd_runner.go | 5 +- runner.go | 138 ++++++++++++++++++++++++++++------------------ 2 files changed, 89 insertions(+), 54 deletions(-) diff --git a/cmd/cmd_runner.go b/cmd/cmd_runner.go index b978236..8dabcf3 100644 --- a/cmd/cmd_runner.go +++ b/cmd/cmd_runner.go @@ -106,12 +106,15 @@ func (runner *CmdRunner) Run(signals <-chan os.Signal, ready chan<- struct{}) er if len(outLog) > 0 { return errors.Internal.Wrap(errRun, outLog) } - + //errorWithOut := errors.AddErrorContext(errRun, "out", outLog) return errRun } return errRun case <-runner.ctx.Done(): + + _ = runner.cmd.Process.Signal(os.Interrupt) + return runner.ctx.Err() } } diff --git a/runner.go b/runner.go index c1ef27f..69cc463 100644 --- a/runner.go +++ b/runner.go @@ -13,79 +13,126 @@ const CreateInfobase = "CREATEINFOBASE" var defaultVersion = "8.3" -type v8Runner struct { - Options *Options - Where Infobase - What Command - ctx context.Context - commandV8 string +type PlatformRunner interface { + + //CreateInfobase() + Run(ctx context.Context) error + Background(ctx context.Context) (Process, error) + Check() error + Args() []string + Opts() Options } -func newRunner(ctx context.Context, where Infobase, what Command, opts ...interface{}) v8Runner { +func NewPlatformRunner(where Infobase, what Command, opts ...interface{}) PlatformRunner { - options := defaultOptions() + runner := newRunner(where, what, opts...) - inlineOptions := getOptions(opts...) - if inlineOptions != nil { - options = inlineOptions - } + return &runner +} - o := clearOpts(opts) +type platformRunner struct { + Options *Options + Where Infobase + What Command + command string + args []string +} - options.Options(o...) +func (r *platformRunner) Run(ctx context.Context) error { - r := v8Runner{ - Where: where, - What: what, - Options: options, - ctx: ctx, + p, err := r.Background(ctx) + + if err != nil { + return err } - return r + return <-p.Wait() } -func Run(where Infobase, what Command, opts ...interface{}) error { +func (r *platformRunner) Background(ctx context.Context) (Process, error) { - ctx := context.Background() + if err := r.Check(); err != nil { + return nil, err + } - p, err := Background(ctx, where, what, opts...) + p := r.background(ctx) + + return p, nil +} + +func (r *platformRunner) Check() error { + + _, err := getV8Path(*r.Options) if err != nil { return err } - return <-p.Wait() + return checkCommand(r.What) } -func Background(ctx context.Context, where Infobase, what Command, opts ...interface{}) (Process, error) { +func (r *platformRunner) Args() []string { - r := newRunner(ctx, where, what, opts...) + commandV8, _ := getV8Path(*r.Options) + return append([]string{ + commandV8}, r.args...) - err := checkCommand(r.What) +} - if err != nil { - return nil, err +func (r *platformRunner) Opts() Options { + return *r.Options +} + +func newRunner(where Infobase, what Command, opts ...interface{}) platformRunner { + + options := defaultOptions() + + inlineOptions := getOptions(opts...) + if inlineOptions != nil { + options = inlineOptions } - r.commandV8, err = getV8Path(*r.Options) + o := clearOpts(opts) - if err != nil { - return nil, err + options.Options(o...) + + args := getCmdArgs(where, what, *options) + + r := platformRunner{ + Where: where, + What: what, + Options: options, + args: args, } - p := r.run() + return r +} - return p, nil +func Run(where Infobase, what Command, opts ...interface{}) error { + + return NewPlatformRunner(where, what, opts...).Run(context.Background()) + +} + +func Background(ctx context.Context, where Infobase, what Command, opts ...interface{}) (Process, error) { + + return NewPlatformRunner(where, what, opts...).Background(ctx) } -func (r *v8Runner) run() Process { +func (r *platformRunner) background(ctx context.Context) Process { - args := getCmdArgs(r.Where, r.What, *r.Options) + if r.Options.Context == nil { + r.Options.Context = ctx + } - runner := prepareRunner(r.ctx, r.commandV8, args, *r.Options) + cmdRunner := cmd.NewCmdRunner(r.command, r.args, + cmd.WithContext(r.Options.Context), + cmd.WithOutFilePath(r.Options.Out), + cmd.WithDumpResultFilePath(r.Options.DumpResult), + ) - p := background(runner, r.ctx) + p := background(cmdRunner, ctx) return p @@ -153,21 +200,6 @@ func getCmdArgs(where Infobase, what Command, options Options) []string { return params.Values() } -func prepareRunner(ctx context.Context, command string, args []string, options Options) Runner { - - if options.Context == nil { - options.Context = ctx - } - - r := cmd.NewCmdRunner(command, args, - cmd.WithContext(options.Context), - cmd.WithOutFilePath(options.Out), - cmd.WithDumpResultFilePath(options.DumpResult), - ) - - return r -} - func getV8Path(options Options) (string, error) { if len(options.v8path) > 0 { return options.v8path, nil