Skip to content

Commit

Permalink
Add inputs to StepsGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubMatejka committed Mar 28, 2022
1 parent f71bb41 commit d54467b
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 6 deletions.
1 change: 1 addition & 0 deletions internal/pkg/cli/dialog/create_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ func TestAskCreateTemplateAllConfigs(t *testing.T) {
Name: "Password",
Type: input.TypeString,
Kind: input.KindHidden,
Step: "Step 1",
},
},
}, opts)
Expand Down
25 changes: 23 additions & 2 deletions internal/pkg/cli/dialog/inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/keboola/keboola-as-code/internal/pkg/model"
"github.com/keboola/keboola-as-code/internal/pkg/template"
"github.com/keboola/keboola-as-code/internal/pkg/template/input"
"github.com/keboola/keboola-as-code/internal/pkg/utils"
"github.com/keboola/keboola-as-code/internal/pkg/utils/orderedmap"
)

Expand Down Expand Up @@ -44,7 +45,7 @@ func (p *Dialogs) askTemplateInputs(deps inputsDialogDeps, branch *model.Branch,

// Define steps and steps groups for user inputs.
stepsDialog := newStepsDialog(p.Prompt)
_, err = stepsDialog.ask()
stepsGroups, err := stepsDialog.ask()
if err != nil {
return nil, nil, err
}
Expand All @@ -54,11 +55,31 @@ func (p *Dialogs) askTemplateInputs(deps inputsDialogDeps, branch *model.Branch,
return nil, nil, err
}

// stepsGroups.AddInputs(inputs.all()))
if err := addInputsToStepsGroups(&stepsGroups, inputs); err != nil {
return nil, nil, err
}

return objectInputs, inputs.all(), nil
}

func addInputsToStepsGroups(stepsGroups *input.StepsGroups, inputs inputsMap) error {
indices := stepsGroups.Indices()
errors := utils.NewMultiError()
for _, i := range *inputs.all() {
if i.Step == "" {
errors.Append(fmt.Errorf(`input "%s": step is missing`, i.Id))
continue
}
index, found := indices[i.Step]
if !found {
errors.Append(fmt.Errorf(`input "%s": step "%s" not found`, i.Id, i.Step))
continue
}
_ = stepsGroups.AddInput(i, index)
}
return errors.ErrorOrNil()
}

type inputFields map[string]input.ObjectField

func (f inputFields) Write(out *strings.Builder) {
Expand Down
41 changes: 41 additions & 0 deletions internal/pkg/cli/dialog/inputs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dialog

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/keboola/keboola-as-code/internal/pkg/template"
"github.com/keboola/keboola-as-code/internal/pkg/template/input"
)

func TestInputsAddInputsToStepsGroups(t *testing.T) {
t.Parallel()

stepsGroups := &input.StepsGroups{
&input.StepsGroup{Id: "Group 1", Description: "desc", Required: "all", Steps: []*input.Step{
{Id: "Step 1", Icon: "common", Name: "Step One", Description: "Description"},
}},
&input.StepsGroup{Id: "Group 2", Required: "all", Steps: []*input.Step{
{Id: "Step 2", Icon: "common", Name: "Step Two", Description: "Description"},
{Id: "Step 3", Icon: "common", Name: "Step Three", Description: "Description"},
}},
}
inputs := newInputsMap()
input1 := &template.Input{
Id: "i1",
Step: "Step 2",
}
inputs.add(input1)
input2 := &template.Input{
Id: "i2",
Step: "Step 4",
}
inputs.add(input2)
err := addInputsToStepsGroups(stepsGroups, inputs)
assert.Error(t, err)
assert.Equal(t, err.Error(), "input \"i2\": step \"Step 4\" not found")
i, f := stepsGroups.InputsForStep(input.StepIndex{Step: 0, Group: 1})
assert.True(t, f)
assert.Equal(t, input.Inputs{*input1}, i)
}
53 changes: 49 additions & 4 deletions internal/pkg/template/input/step.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,63 @@
package input

type StepsGroups []StepsGroup
import (
"fmt"
)

type StepsGroups []*StepsGroup

type StepIndex struct {
Step int
Group int
}

func (g StepsGroups) Indices() map[string]StepIndex {
res := make(map[string]StepIndex)
for gIdx, group := range g {
for sIdx, step := range group.Steps {
res[step.Id] = StepIndex{
Step: sIdx,
Group: gIdx,
}
}
}
return res
}

func (g StepsGroups) AddInput(input Input, index StepIndex) error {
if len(g) < index.Group {
return fmt.Errorf("group at index %d not found", index.Group)
}
if len(g[index.Group].Steps) < index.Step {
return fmt.Errorf("step at index %d for group at index %d not found", index.Step, index.Group)
}
g[index.Group].Steps[index.Step].Inputs = append(g[index.Group].Steps[index.Step].Inputs, input)
return nil
}

func (g StepsGroups) InputsForStep(index StepIndex) (Inputs, bool) {
if len(g) < index.Group {
return nil, false
}
if len(g[index.Group].Steps) < index.Step {
return nil, false
}
return g[index.Group].Steps[index.Step].Inputs, true
}

func (g *StepsGroups) Validate() error {
return validate(g)
}

type StepsGroup struct {
Description string `json:"description" validate:"max=80"`
Required string `json:"required" validate:"oneof=all atLeastOne exactOne zeroOrOne optional"`
Steps []Step `json:"steps" validate:"min=1,dive"`
Id string `json:"id"`
Description string `json:"description" validate:"max=80"`
Required string `json:"required" validate:"oneof=all atLeastOne exactOne zeroOrOne optional"`
Steps []*Step `json:"steps" validate:"min=1,dive"`
}

type Step struct {
Id string `json:"id"`
Icon string `json:"icon" validate:"required"`
Name string `json:"name" validate:"required,max=20"`
Description string `json:"description" validate:"max=40"`
Expand Down

0 comments on commit d54467b

Please sign in to comment.