Skip to content

Commit

Permalink
ref: runner
Browse files Browse the repository at this point in the history
add: PlatformRunner interface
  • Loading branch information
khorevaa committed Nov 11, 2020
1 parent 3711570 commit 1d595a6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 54 deletions.
5 changes: 4 additions & 1 deletion cmd/cmd_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
138 changes: 85 additions & 53 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1d595a6

Please sign in to comment.