Skip to content

Commit

Permalink
feat: added error handler check in connector build (#1720)
Browse files Browse the repository at this point in the history
* feat: added error handler check in connector build

* fix: remove saving schema to disk

* fix: typo in key

* chore: fix lint error

Co-authored-by: Wojciech Trocki <[email protected]>
  • Loading branch information
jackdelahunt and wtrocki authored Aug 18, 2022
1 parent a8f907b commit d988703
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@
"update"
]
},
{
"name": "Connector build",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/rhoas",
"env": {},
"args": [
"connector",
"build",
"--type=data_generator_0.1",
]
},
{
"name": "Whoami",
"type": "go",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/fatih/color v1.13.0
github.com/golang-jwt/jwt/v4 v4.4.2
github.com/google/go-github/v39 v39.2.0
github.com/jackdelahunt/survey-json-schema v0.9.0
github.com/jackdelahunt/survey-json-schema v0.12.0
github.com/kataras/tablewriter v0.0.0-20180708051242-e063d29b7c23 // indirect
github.com/landoop/tableprinter v0.0.0-20201125135848-89e81fc956e7
github.com/mattn/go-isatty v0.0.14
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf h1:WfD7V
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf/go.mod h1:hyb9oH7vZsitZCiBt0ZvifOrB+qc8PS5IiilCIb87rg=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jackdelahunt/survey-json-schema v0.9.0 h1:CbvKR0dlgIstI3DOGg2fXuG38+D9LM7YQGMACCpPWBw=
github.com/jackdelahunt/survey-json-schema v0.9.0/go.mod h1:pEx3fKFqHHzCMAu/tAkYpMAiMXtWYEbwJR8UBsWx2Vs=
github.com/jackdelahunt/survey-json-schema v0.12.0 h1:YqzvIspId/Gb1Iqf0x/pb9AC9wyQjw2yhBPKTkF5l/s=
github.com/jackdelahunt/survey-json-schema v0.12.0/go.mod h1:pEx3fKFqHHzCMAu/tAkYpMAiMXtWYEbwJR8UBsWx2Vs=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
Expand Down
31 changes: 31 additions & 0 deletions pkg/cmd/connector/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"os"

"github.com/AlecAivazis/survey/v2"

"github.com/jackdelahunt/survey-json-schema/pkg/surveyjson"
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/connectorcmdutil"
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
Expand Down Expand Up @@ -120,6 +122,9 @@ func runBuild(opts *options) error {

opts.f.Logger.Info(opts.f.Localizer.MustLocalize("connector.build.info.msg"))

overrides := make(map[string]func(o *surveyjson.JSONSchemaOptions, ctx surveyjson.SchemaContext) error)
overrides["error_handler"] = onErrorHandler

// Creates JSONSchema based of
schemaOptions := surveyjson.JSONSchemaOptions{
Out: os.Stdout,
Expand All @@ -129,6 +134,7 @@ func runBuild(opts *options) error {
AutoAcceptDefaults: false,
NoAsk: false,
IgnoreMissingValues: false,
Overrides: overrides,
}

initialValues := make(map[string]interface{})
Expand Down Expand Up @@ -186,3 +192,28 @@ func createConnectorObject(opts *options, connectorSpecification map[string]inte
}
return connector
}

// nolint:gocritic
func onErrorHandler(o *surveyjson.JSONSchemaOptions, ctx surveyjson.SchemaContext) error {

options := make([]string, len(ctx.SchemaType.OneOf))
for index, option := range ctx.SchemaType.OneOf {
options[index] = option.Required[0]
}

prompt := &survey.Select{
Message: ctx.Name,
Options: options,
}

var selectedIndex int
err := survey.AskOne(prompt, &selectedIndex)
if err != nil {
return err
}

ctx.Name = "error_handler"
ctx.ParentType = ctx.SchemaType
ctx.SchemaType = ctx.SchemaType.OneOf[selectedIndex]
return o.Recurse(ctx)
}

0 comments on commit d988703

Please sign in to comment.