Skip to content

Commit

Permalink
Pass inputs to action
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Dec 21, 2021
1 parent 484435a commit ac7c881
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 29 deletions.
3 changes: 1 addition & 2 deletions krab/action_migrate_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ func (a *ActionMigrateDown) Run(args []string) int {
cmd := &CmdMigrateDown{
Set: a.Set,
Connection: a.Connection,
Inputs: flags.Values(),
}
resp, err := cmd.Do(context.Background(), CmdOpts{})
resp, err := cmd.Do(context.Background(), CmdOpts{Inputs: flags.Values()})
result := resp.([]ResponseMigrateDown)

if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions krab/action_migrate_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ func (a *ActionMigrateStatus) Run(args []string) int {
cmd := &CmdMigrateStatus{
Set: a.Set,
Connection: a.Connection,
Inputs: flags.Values(),
}
resp, err := cmd.Do(context.Background(), CmdOpts{})
resp, err := cmd.Do(context.Background(), CmdOpts{Inputs: flags.Values()})

if err != nil {
ui.Error(err.Error())
Expand Down
3 changes: 1 addition & 2 deletions krab/action_migrate_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ func (a *ActionMigrateUp) Run(args []string) int {
cmd := &CmdMigrateUp{
Set: a.Set,
Connection: a.Connection,
Inputs: flags.Values(),
}
resp, err := cmd.Do(context.Background(), CmdOpts{})
resp, err := cmd.Do(context.Background(), CmdOpts{Inputs: flags.Values()})
result := resp.([]ResponseMigrateUp)

if len(result) > 0 {
Expand Down
4 changes: 3 additions & 1 deletion krab/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ type Cmd interface {
}

// CmdOpts are options passed to command.
type CmdOpts struct{}
type CmdOpts struct {
Inputs
}
19 changes: 9 additions & 10 deletions krab/cmd_migrate_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
type CmdMigrateDown struct {
Set *MigrationSet
Connection krabdb.Connection
Inputs
}

// ResponseMigrateDown json
Expand Down Expand Up @@ -43,48 +42,48 @@ func (c *CmdMigrateDown) HttpMethod() string { return http.MethodPost }

func (c *CmdMigrateDown) Do(ctx context.Context, o CmdOpts) (interface{}, error) {
for _, arg := range c.Set.Arguments.Args {
_, ok := c.Inputs[arg.Name]
_, ok := o.Inputs[arg.Name]
if !ok {
return nil, fmt.Errorf("Command is missing an input for argument `%s`", arg.Name)
}
}
// default arguments always take precedence over custom ones
for _, arg := range c.Arguments().Args {
_, ok := c.Inputs[arg.Name]
_, ok := o.Inputs[arg.Name]
if !ok {
return nil, fmt.Errorf("Command is missing an input for argument `%s`", arg.Name)
}
}

err := c.Set.Arguments.Validate(c.Inputs)
err := c.Set.Arguments.Validate(o.Inputs)
if err != nil {
return nil, err
}
err = c.Arguments().Validate(c.Inputs)
err = c.Arguments().Validate(o.Inputs)
if err != nil {
return nil, err
}

var result []ResponseMigrateDown
err = c.Connection.Get(func(db krabdb.DB) error {
resp, err := c.run(ctx, db)
resp, err := c.run(ctx, db, o.Inputs)
result = resp
return err
})

return result, err
}

func (c *CmdMigrateDown) run(ctx context.Context, db krabdb.DB) ([]ResponseMigrateDown, error) {
func (c *CmdMigrateDown) run(ctx context.Context, db krabdb.DB, inputs Inputs) ([]ResponseMigrateDown, error) {
result := []ResponseMigrateDown{}

tpl := tpls.New(c.Inputs, krabtpl.Functions)
tpl := tpls.New(inputs, krabtpl.Functions)
versions := NewSchemaMigrationTable(tpl.Render(c.Set.Schema))

migration := c.Set.FindMigrationByVersion(c.Inputs["version"].(string))
migration := c.Set.FindMigrationByVersion(inputs["version"].(string))
if migration == nil {
return nil, fmt.Errorf("Migration `%s` not found in `%s` set",
c.Inputs["version"].(string),
inputs["version"].(string),
c.Set.RefName)
}
lockID := int64(1)
Expand Down
11 changes: 5 additions & 6 deletions krab/cmd_migrate_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
type CmdMigrateStatus struct {
Set *MigrationSet
Connection krabdb.Connection
Inputs
}

// ResponseMigrateStatus json
Expand All @@ -32,30 +31,30 @@ func (c *CmdMigrateStatus) HttpMethod() string { return http.MethodGet }

func (c *CmdMigrateStatus) Do(ctx context.Context, o CmdOpts) (interface{}, error) {
for _, arg := range c.Set.Arguments.Args {
_, ok := c.Inputs[arg.Name]
_, ok := o.Inputs[arg.Name]
if !ok {
return nil, fmt.Errorf("Command is missing an input for argument `%s`", arg.Name)
}
}
err := c.Set.Arguments.Validate(c.Inputs)
err := c.Set.Arguments.Validate(o.Inputs)
if err != nil {
return nil, err
}

var result []ResponseMigrateStatus
err = c.Connection.Get(func(db krabdb.DB) error {
resp, err := c.run(ctx, db)
resp, err := c.run(ctx, db, o.Inputs)
result = resp
return err
})

return result, err
}

func (c *CmdMigrateStatus) run(ctx context.Context, db krabdb.DB) ([]ResponseMigrateStatus, error) {
func (c *CmdMigrateStatus) run(ctx context.Context, db krabdb.DB, inputs Inputs) ([]ResponseMigrateStatus, error) {
result := []ResponseMigrateStatus{}

tpl := tpls.New(c.Inputs, krabtpl.Functions)
tpl := tpls.New(inputs, krabtpl.Functions)
versions := NewSchemaMigrationTable(tpl.Render(c.Set.Schema))

hooksRunner := HookRunner{}
Expand Down
11 changes: 5 additions & 6 deletions krab/cmd_migrate_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
type CmdMigrateUp struct {
Set *MigrationSet
Connection krabdb.Connection
Inputs
}

// ResponseMigrateUp json
Expand All @@ -31,31 +30,31 @@ func (c *CmdMigrateUp) HttpMethod() string { return http.MethodPost }

func (c *CmdMigrateUp) Do(ctx context.Context, o CmdOpts) (interface{}, error) {
for _, arg := range c.Set.Arguments.Args {
_, ok := c.Inputs[arg.Name]
_, ok := o.Inputs[arg.Name]
if !ok {
return nil, fmt.Errorf("Command is missing an input for argument `%s`", arg.Name)
}
}

err := c.Set.Arguments.Validate(c.Inputs)
err := c.Set.Arguments.Validate(o.Inputs)
if err != nil {
return nil, err
}

var result []ResponseMigrateUp
err = c.Connection.Get(func(db krabdb.DB) error {
resp, err := c.run(ctx, db)
resp, err := c.run(ctx, db, o.Inputs)
result = resp
return err
})

return result, err
}

func (c *CmdMigrateUp) run(ctx context.Context, db krabdb.DB) ([]ResponseMigrateUp, error) {
func (c *CmdMigrateUp) run(ctx context.Context, db krabdb.DB, inputs Inputs) ([]ResponseMigrateUp, error) {
result := []ResponseMigrateUp{}

tpl := tpls.New(c.Inputs, krabtpl.Functions)
tpl := tpls.New(inputs, krabtpl.Functions)
versions := NewSchemaMigrationTable(tpl.Render(c.Set.Schema))

// locking
Expand Down

0 comments on commit ac7c881

Please sign in to comment.