Skip to content

Commit

Permalink
feat: add history for kystrap testing
Browse files Browse the repository at this point in the history
  • Loading branch information
shifty11 committed Feb 7, 2024
1 parent bcf2da3 commit 47d5028
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
23 changes: 15 additions & 8 deletions tools/kystrap/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var (
}
)

func promptInput(cmd *cobra.Command, field protoreflect.FieldDescriptor, fieldLabel string) (string, error) {
func promptInput(cmd *cobra.Command, field protoreflect.FieldDescriptor, fieldLabel string, history *map[string]string) (string, error) {
if !commoncmd.IsInteractive(cmd) {
return "", nil
}
Expand All @@ -57,7 +57,7 @@ func promptInput(cmd *cobra.Command, field protoreflect.FieldDescriptor, fieldLa
var data string
for i := 0; i < field.Message().Fields().Len(); i++ {
subField := field.Message().Fields().Get(i)
input, err := promptInput(cmd, subField, fmt.Sprintf("%s -> %s", fieldLabel, subField.Name()))
input, err := promptInput(cmd, subField, fmt.Sprintf("%s -> %s", fieldLabel, subField.Name()), history)
if err != nil {
return "", err
}
Expand All @@ -73,17 +73,23 @@ func promptInput(cmd *cobra.Command, field protoreflect.FieldDescriptor, fieldLa
}
return fmt.Sprintf(`"%s": "%s"`, field.Name(), data), nil
}
historyValue := ""
if history != nil {
historyValue = (*history)[fieldLabel]
}
prompt := promptui.Prompt{
Label: fieldLabel,
Label: fieldLabel,
Default: historyValue,
}
result, err := prompt.Run()
if err != nil {
return "", err
}
(*history)[fieldLabel] = result
return fmt.Sprintf(`"%s": "%s"`, field.Name(), result), err
}

func runMethodPrompts(cmd *cobra.Command, method protoreflect.MethodDescriptor, address string, data string) (protoreflect.MethodDescriptor, bool, error) {
func runMethodPrompts(cmd *cobra.Command, method protoreflect.MethodDescriptor, address string, data string, history *map[string]string) (protoreflect.MethodDescriptor, bool, error) {
if method == nil {
methodOption, err := commoncmd.GetOptionFromPrompt(flagMethod)
if err != nil {
Expand All @@ -97,7 +103,7 @@ func runMethodPrompts(cmd *cobra.Command, method protoreflect.MethodDescriptor,
fields := method.Input().Fields()
for i := 0; i < fields.Len(); i++ {
field := fields.Get(i)
input, err := promptInput(cmd, field, string(field.Name()))
input, err := promptInput(cmd, field, string(field.Name()), history)
if err != nil {
return method, false, err
}
Expand Down Expand Up @@ -173,7 +179,8 @@ func CmdTestIntegration() *cobra.Command {
// Set as new default value to remember the cursor position in further prompts
flagMethod.DefaultValue = types.NewMethodOption(method)

method, success, err := runMethodPrompts(cmd, method, address, data)
history := make(map[string]string)
method, success, err := runMethodPrompts(cmd, method, address, data, &history)
if err != nil {
return err
}
Expand All @@ -192,13 +199,13 @@ func CmdTestIntegration() *cobra.Command {
}
switch actionResult {
case actionRetry:
method, success, err = runMethodPrompts(cmd, method, address, "")
method, success, err = runMethodPrompts(cmd, method, address, "", &history)
if err != nil {
return err
}
case actionTestAnother:
method = nil
method, success, err = runMethodPrompts(cmd, method, address, "")
method, success, err = runMethodPrompts(cmd, method, address, "", &history)
if err != nil {
return err
}
Expand Down
9 changes: 1 addition & 8 deletions tools/kystrap/grpcall/grpcall.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package grpcall
import (
"bytes"
"context"
"encoding/json"
"errors"
"strings"
"time"

Expand Down Expand Up @@ -120,12 +118,6 @@ func (gc *GrpcCaller) dial() (*grpc.ClientConn, error) {
// An error is only returned if the call could not be performed (e.g. connection issues)
// Invalid JSON is not considered an error
func (gc *GrpcCaller) PerformMethodCall(method protoreflect.MethodDescriptor, data string) (bool, error) {
if !json.Valid([]byte(data)) {
gc.printMethodDescription(method)
gc.printError(errors.New("invalid JSON"))
return false, nil
}

cc, err := gc.dial()
if err != nil {
return false, err
Expand All @@ -142,6 +134,7 @@ func (gc *GrpcCaller) PerformMethodCall(method protoreflect.MethodDescriptor, da
options := grpcurl.FormatOptions{
EmitJSONDefaultFields: true,
AllowUnknownFields: true,
IncludeTextSeparator: true,
}

rf, formatter, err := grpcurl.RequestParserAndFormatter(grpcurl.FormatJSON, types.Rdk.DescriptorSource, in, options)
Expand Down

0 comments on commit 47d5028

Please sign in to comment.