generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
cmd_call.go
166 lines (139 loc) 路 4.8 KB
/
cmd_call.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"unicode/utf8"
"connectrpc.com/connect"
"github.com/jpillora/backoff"
"github.com/titanous/json5"
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1"
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/go-runtime/ftl/reflection"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/rpc"
)
type callCmd struct {
Wait time.Duration `short:"w" help:"Wait up to this elapsed time for the FTL cluster to become available." default:"1m"`
Verb reflection.Ref `arg:"" required:"" help:"Full path of Verb to call."`
Request string `arg:"" optional:"" help:"JSON5 request payload." default:"{}"`
}
func (c *callCmd) Run(ctx context.Context, client ftlv1connect.VerbServiceClient, ctlCli ftlv1connect.ControllerServiceClient) error {
ctx, cancel := context.WithTimeout(ctx, c.Wait)
defer cancel()
if err := rpc.Wait(ctx, backoff.Backoff{Max: time.Second * 2}, client); err != nil {
return err
}
logger := log.FromContext(ctx)
request := map[string]any{}
err := json5.Unmarshal([]byte(c.Request), &request)
if err != nil {
return fmt.Errorf("invalid request: %w", err)
}
requestJSON, err := json.Marshal(request)
if err != nil {
return fmt.Errorf("invalid request: %w", err)
}
logger.Debugf("Calling %s", c.Verb)
// otherwise, we have a match so call the verb
resp, err := client.Call(ctx, connect.NewRequest(&ftlv1.CallRequest{
Verb: c.Verb.ToProto(),
Body: requestJSON,
}))
if cerr := new(connect.Error); errors.As(err, &cerr) && cerr.Code() == connect.CodeNotFound {
suggestions, err := c.findSuggestions(ctx, ctlCli)
// if we have suggestions, return a helpful error message. otherwise continue to the original error
if err == nil {
return fmt.Errorf("verb not found: %s\n\nDid you mean one of these?\n%s", c.Verb, strings.Join(suggestions, "\n"))
}
}
if err != nil {
return err
}
switch resp := resp.Msg.Response.(type) {
case *ftlv1.CallResponse_Error_:
if resp.Error.Stack != nil && logger.GetLevel() <= log.Debug {
fmt.Println(*resp.Error.Stack)
}
return fmt.Errorf("verb error: %s", resp.Error.Message)
case *ftlv1.CallResponse_Body:
fmt.Println(string(resp.Body))
}
return nil
}
// findSuggestions looks up the schema and finds verbs that are similar to the one that was not found
// it uses the levenshtein distance to determine similarity - if the distance is less than 40% of the length of the verb,
// it returns an error if no closely matching suggestions are found
func (c *callCmd) findSuggestions(ctx context.Context, client ftlv1connect.ControllerServiceClient) ([]string, error) {
logger := log.FromContext(ctx)
// lookup the verbs
res, err := client.GetSchema(ctx, connect.NewRequest(&ftlv1.GetSchemaRequest{}))
if err != nil {
return nil, err
}
modules := make([]*schema.Module, 0, len(res.Msg.GetSchema().GetModules()))
for _, pbmodule := range res.Msg.GetSchema().GetModules() {
module, err := schema.ModuleFromProto(pbmodule)
if err != nil {
logger.Errorf(err, "failed to convert module from protobuf")
continue
}
modules = append(modules, module)
}
verbs := []string{}
// build a list of all the verbs
for _, module := range modules {
for _, v := range module.Verbs() {
verbName := fmt.Sprintf("%s.%s", module.Name, v.Name)
if verbName == fmt.Sprintf("%s.%s", c.Verb.Module, c.Verb.Name) {
break
}
verbs = append(verbs, module.Name+"."+v.Name)
}
}
suggestions := []string{}
logger.Debugf("Found %d verbs", len(verbs))
needle := fmt.Sprintf("%s.%s", c.Verb.Module, c.Verb.Name)
// only consider suggesting verbs that are within 40% of the length of the needle
distanceThreshold := int(float64(len(needle))*0.4) + 1
for _, verb := range verbs {
d := levenshtein(verb, needle)
logger.Debugf("Verb %s distance %d", verb, d)
if d <= distanceThreshold {
suggestions = append(suggestions, verb)
}
}
if len(suggestions) > 0 {
return suggestions, nil
}
return nil, fmt.Errorf("no suggestions found")
}
// Levenshtein computes the Levenshtein distance between two strings.
//
// credit goes to https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Go
func levenshtein(a, b string) int {
f := make([]int, utf8.RuneCountInString(b)+1)
for j := range f {
f[j] = j
}
for _, ca := range a {
j := 1
fj1 := f[0] // fj1 is the value of f[j - 1] in last iteration
f[0]++
for _, cb := range b {
mn := min(f[j]+1, f[j-1]+1) // delete & insert
if cb != ca {
mn = min(mn, fj1+1) // change
} else {
mn = min(mn, fj1) // matched
}
fj1, f[j] = f[j], mn // save f[j] to fj1(j is about to increase), update f[j] to mn
j++
}
}
return f[len(f)-1]
}