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..f62aec2 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 (r *v8Runner) run() Process { +func Background(ctx context.Context, where Infobase, what Command, opts ...interface{}) (Process, error) { - args := getCmdArgs(r.Where, r.What, *r.Options) + return NewPlatformRunner(where, what, opts...).Background(ctx) - runner := prepareRunner(r.ctx, r.commandV8, args, *r.Options) +} - p := background(runner, r.ctx) +func (r *platformRunner) background(ctx context.Context) Process { + + if r.Options.Context == nil { + r.Options.Context = ctx + } + + cmdRunner := cmd.NewCmdRunner(r.command, r.args, + cmd.WithContext(r.Options.Context), + cmd.WithOutFilePath(r.Options.Out), + cmd.WithDumpResultFilePath(r.Options.DumpResult), + ) + + p := background(cmdRunner, ctx) return p @@ -131,14 +178,11 @@ func getCmdArgs(where Infobase, what Command, options Options) []string { if isCreateInfobase(what) { - whereParams, whereValues := getConnectionsStringParams(where.Values()) - whatParams, whatValues := getConnectionsStringParams(what.Values()) - - connectionString := joinConnectionStringParams(whatParams, whereParams) + connectionStringParams, values := getConnectionsStringParams(what.Values()) + connectionString := strings.Join(connectionStringParams, ";") params.Append(connectionString) - params.Append(whatValues...) - params.Append(whereValues...) + params.Append(values...) } else { @@ -153,21 +197,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 diff --git a/runner_test.go b/runner_test.go index 1313213..4daa45c 100644 --- a/runner_test.go +++ b/runner_test.go @@ -6,7 +6,6 @@ import ( "github.com/v8platform/marshaler" "io/ioutil" "os" - "path" "testing" ) @@ -30,15 +29,16 @@ func Test_runnerTestSuite(t *testing.T) { suite.Run(t, new(v8runnerTestSuite)) } -func (t *v8runnerTestSuite) TestCmdRunner() { +func (t *v8runnerTestSuite) TestCmdRunnerCreateInfobase() { - tempIB := NewTempIB() + runner := NewPlatformRunner(testInfoBase{}, CreateFileInfoBaseOptions{ + File: "./file_ib", + DBFormat: "8.3.8", + }) + args := runner.Args() + t.r().Contains(args, CreateInfobase) + t.r().Contains(args, "File='./file_ib';DBFormat=8.3.8") - err := Run(tempIB, CreateFileInfoBaseOptions{}) - t.r().NoError(err) - fileBaseCreated, err2 := Exists(path.Join(tempIB.File, "1Cv8.1CD")) - t.r().NoError(err2) - t.r().True(fileBaseCreated, "Файл базы должен быть создан") } func Exists(name string) (bool, error) { @@ -72,21 +72,9 @@ type testInfoBase struct { Locale string `v8:"Locale, optional, equal_sep" json:"locale"` } -func (d testInfoBase) Path() string { - - return d.File -} - -func (d testInfoBase) Values() []string { - - v, _ := marshaler.Marshal(d) - return v - -} - func (d testInfoBase) ConnectionString() string { - return "/F " + d.File + return "File=" + d.File } diff --git a/types.go b/types.go index 0207c7a..4bbf8dc 100644 --- a/types.go +++ b/types.go @@ -18,13 +18,11 @@ type Param interface { } type Infobase interface { - Path() string // Возвращает // - /IBConnectionString <СтрокаПодключения> // - /F<ПУтьКБазе> // - /S<ПутьКСервернойБазе> ConnectionString() string - Values() []string } type Command interface {