Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create steps and groups (KAC-93) #610

Merged
merged 17 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/pkg/cli/dialog/create_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ func (d *createTmplDialog) ask() (createTemplate.Options, error) {
}

// Ask for user inputs
objectInputs, allInputs, err := d.askTemplateInputs(d.deps, d.selectedBranch, d.selectedConfigs)
objectInputs, stepsGroups, err := d.askTemplateInputs(d.deps, d.selectedBranch, d.selectedConfigs)
if err != nil {
return d.out, err
}
objectInputs.setTo(d.out.Configs)
d.out.Inputs = allInputs
d.out.StepsGroups = stepsGroups

return d.out, nil
}
Expand Down
104 changes: 75 additions & 29 deletions internal/pkg/cli/dialog/create_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ func TestAskCreateTemplateInteractive(t *testing.T) {
_, err = console.Send(testhelper.Enter) // -> start editor
assert.NoError(t, err)

_, err = console.ExpectString("Please define steps and groups for user inputs specification.")
assert.NoError(t, err)

time.Sleep(20 * time.Millisecond)
_, err = console.Send(testhelper.Enter) // -> start editor
assert.NoError(t, err)

_, err = console.ExpectString("Please complete the user inputs specification.")
assert.NoError(t, err)

Expand Down Expand Up @@ -196,12 +203,25 @@ func TestAskCreateTemplateInteractive(t *testing.T) {
TemplateId: `config-3`,
},
},
Inputs: &template.Inputs{
StepsGroups: input.StepsGroups{
{
Id: "my-component-password",
Name: "Password",
Type: input.TypeString,
Kind: input.KindHidden,
Description: "Default Group",
Required: "all",
Steps: []*input.Step{
{
Icon: "common",
Name: "Default Step",
Description: "Default Step",
Inputs: input.Inputs{
{
Id: "my-component-password",
Name: "Password",
Type: input.TypeString,
Kind: input.KindHidden,
},
},
},
},
},
},
}, opts)
Expand Down Expand Up @@ -278,26 +298,39 @@ func TestAskCreateTemplateNonInteractive(t *testing.T) {
TemplateId: `config-3`,
},
},
Inputs: &template.Inputs{
{
Id: "my-component-password",
Name: "Password",
Type: input.TypeString,
Kind: input.KindHidden,
},
{
Id: "my-component-int",
Name: "Int",
Type: input.TypeInt,
Kind: input.KindInput,
Default: 123,
},
StepsGroups: input.StepsGroups{
{
Id: "my-component-string",
Name: "String",
Type: input.TypeString,
Kind: input.KindInput,
Default: "my string",
Description: "Default Group",
Required: "all",
Steps: []*input.Step{
{
Icon: "common",
Name: "Default Step",
Description: "Default Step",
Inputs: input.Inputs{
{
Id: "my-component-password",
Name: "Password",
Type: input.TypeString,
Kind: input.KindHidden,
},
{
Id: "my-component-int",
Name: "Int",
Type: input.TypeInt,
Kind: input.KindInput,
Default: 123,
},
{
Id: "my-component-string",
Name: "String",
Type: input.TypeString,
Kind: input.KindInput,
Default: "my string",
},
},
},
},
},
},
}, opts)
Expand Down Expand Up @@ -373,12 +406,25 @@ func TestAskCreateTemplateAllConfigs(t *testing.T) {
TemplateId: `config-3`,
},
},
Inputs: &template.Inputs{
StepsGroups: input.StepsGroups{
{
Id: "my-component-password",
Name: "Password",
Type: input.TypeString,
Kind: input.KindHidden,
Description: "Default Group",
Required: "all",
Steps: []*input.Step{
{
Icon: "common",
Name: "Default Step",
Description: "Default Step",
Inputs: input.Inputs{
{
Id: "my-component-password",
Name: "Password",
Type: input.TypeString,
Kind: input.KindHidden,
},
},
},
},
},
},
}, opts)
Expand Down
38 changes: 35 additions & 3 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 All @@ -21,7 +22,7 @@ type inputsDialogDeps interface {

// askTemplateInputs - dialog to define user inputs for a new template.
// Used in AskCreateTemplateOpts.
func (p *Dialogs) askTemplateInputs(deps inputsDialogDeps, branch *model.Branch, configs []*model.ConfigWithRows) (objectInputsMap, *template.Inputs, error) {
func (p *Dialogs) askTemplateInputs(deps inputsDialogDeps, branch *model.Branch, configs []*model.ConfigWithRows) (objectInputsMap, template.StepsGroups, error) {
// Create empty inputs map
inputs := newInputsMap()

Expand All @@ -42,12 +43,43 @@ func (p *Dialogs) askTemplateInputs(deps inputsDialogDeps, branch *model.Branch,
return nil, nil, err
}

// Define steps and steps groups for user inputs.
stepsDialog := newStepsDialog(p.Prompt)
stepsGroups, stepsToIds, err := stepsDialog.ask()
if err != nil {
return nil, nil, err
}

// Define name/description for each user input.
if err := newInputsDetailsDialog(p.Prompt, inputs).ask(); err != nil {
inputsToSteps, err := newInputsDetailsDialog(p.Prompt, inputs).ask(stepsGroups, stepsToIds)
if err != nil {
return nil, nil, err
}

return objectInputs, inputs.all(), nil
if err := addInputsToStepsGroups(stepsGroups, inputs, inputsToSteps, stepsToIds); err != nil {
return nil, nil, err
}

return objectInputs, stepsGroups, nil
}

func addInputsToStepsGroups(stepsGroups input.StepsGroups, inputs inputsMap, inputsToSteps *orderedmap.OrderedMap, stepsToIds map[input.StepIndex]string) error {
indices := stepsGroups.Indices(stepsToIds)
errors := utils.NewMultiError()
for _, inputId := range inputsToSteps.Keys() {
step, _ := inputsToSteps.Get(inputId)
index, found := indices[fmt.Sprintf("%v", step)]
if !found {
errors.Append(fmt.Errorf(`input "%s": step "%s" not found`, inputId, step))
continue
}
in, _ := inputs.get(inputId)
err := stepsGroups.AddInput(*in, index)
if err != nil {
errors.Append(err)
}
}
return errors.ErrorOrNil()
}

type inputFields map[string]input.ObjectField
Expand Down
37 changes: 31 additions & 6 deletions internal/pkg/cli/dialog/inputs_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"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"
"github.com/keboola/keboola-as-code/internal/pkg/utils/strhelper"
)

Expand All @@ -27,22 +28,24 @@ func newInputsDetailsDialog(prompt prompt.Prompt, inputs inputsMap) *inputsDetai
return &inputsDetailDialog{prompt: prompt, inputs: inputs}
}

func (d *inputsDetailDialog) ask() error {
func (d *inputsDetailDialog) ask(stepsGroups input.StepsGroups, stepsToIds map[input.StepIndex]string) (*orderedmap.OrderedMap, error) {
result, _ := d.prompt.Editor("md", &prompt.Question{
Description: `Please complete the user inputs specification.`,
Default: d.defaultValue(),
Default: d.defaultValue(stepsGroups, stepsToIds),
Validator: func(val interface{}) error {
if err := d.parse(val.(string)); err != nil {
_, err := d.parse(val.(string))
if err != nil {
// Print errors to new line
return utils.PrefixError("\n", err)
}

return nil
},
})
return d.parse(result)
}

func (d *inputsDetailDialog) parse(result string) error {
func (d *inputsDetailDialog) parse(result string) (*orderedmap.OrderedMap, error) {
result = strhelper.StripHtmlComments(result)
scanner := bufio.NewScanner(strings.NewReader(result))
errors := utils.NewMultiError()
Expand All @@ -53,6 +56,7 @@ func (d *inputsDetailDialog) parse(result string) error {

var currentInput *template.Input
var invalidDefinition bool
inputsToStepsMap := orderedmap.New()

for scanner.Scan() {
lineNum++
Expand Down Expand Up @@ -118,6 +122,8 @@ func (d *inputsDetailDialog) parse(result string) error {
errors.Append(fmt.Errorf(`line %d: options are not expected for kind "%s"`, lineNum, currentInput.Kind))
continue
}
case strings.HasPrefix(line, `step:`):
inputsToStepsMap.Set(currentInput.Id, strings.TrimSpace(strings.TrimPrefix(line, `step:`)))
default:
// Expected object definition
errors.Append(fmt.Errorf(`line %d: cannot parse "%s"`, lineNum, strhelper.Truncate(line, 10, "...")))
Expand Down Expand Up @@ -151,10 +157,10 @@ func (d *inputsDetailDialog) parse(result string) error {
})
})

return errors.ErrorOrNil()
return inputsToStepsMap, errors.ErrorOrNil()
}

func (d *inputsDetailDialog) defaultValue() string {
func (d *inputsDetailDialog) defaultValue(stepsGroups input.StepsGroups, stepsToIds map[input.StepIndex]string) string {
// File header - info for user
fileHeader := `
<!--
Expand Down Expand Up @@ -200,6 +206,23 @@ Options format:
kind: multiselect
default: id1, id3
options: {"id1":"Option 1","id2":"Option 2","id3":"Option 3"}

Preview of steps and groups you created:
`
var defaultStepId string
for gIdx, group := range stepsGroups {
fileHeader += fmt.Sprintf(`- Group %d
`, gIdx+1)
for sIdx := range group.Steps {
index := input.StepIndex{Step: sIdx, Group: gIdx}
fileHeader += fmt.Sprintf(` - Step "%s"
`, stepsToIds[index])
if gIdx == 0 && sIdx == 0 {
defaultStepId = stepsToIds[index]
}
}
}
fileHeader += `
-->


Expand Down Expand Up @@ -233,6 +256,8 @@ Options format:
lines.WriteString(fmt.Sprintf("options: %s\n", json.MustEncode(i.Options.Map(), false)))
}

lines.WriteString(fmt.Sprintf("step: %s\n", defaultStepId))

lines.WriteString("\n")
}

Expand Down
Loading