Skip to content

Commit

Permalink
Finish v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
khorevaa committed Nov 21, 2020
2 parents 1922c46 + 7fb656c commit af46a90
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 83 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
147 changes: 88 additions & 59 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 (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

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

Expand All @@ -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
Expand Down
30 changes: 9 additions & 21 deletions runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/v8platform/marshaler"
"io/ioutil"
"os"
"path"
"testing"
)

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

}

Expand Down
2 changes: 0 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ type Param interface {
}

type Infobase interface {
Path() string
// Возвращает
// - /IBConnectionString <СтрокаПодключения>
// - /F<ПУтьКБазе>
// - /S<ПутьКСервернойБазе>
ConnectionString() string
Values() []string
}

type Command interface {
Expand Down

0 comments on commit af46a90

Please sign in to comment.