Skip to content

Commit e1c0e91

Browse files
committed
feat: open cmd
1 parent 46267a6 commit e1c0e91

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

cmd/ftl/cmd_open.go

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strconv"
7+
"strings"
8+
9+
"connectrpc.com/connect"
10+
"github.com/alecthomas/kong"
11+
ftlv1 "github.com/block/ftl/backend/protos/xyz/block/ftl/v1"
12+
"github.com/block/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
13+
"github.com/block/ftl/common/reflection"
14+
"github.com/block/ftl/common/schema"
15+
"github.com/block/ftl/internal/exec"
16+
"github.com/block/ftl/internal/log"
17+
"github.com/block/ftl/internal/projectconfig"
18+
)
19+
20+
type openCmd struct {
21+
Ref reflection.Ref `arg:"" help:"Language of the module to create." placeholder:"MODULE.ITEM" predictor:"decls"`
22+
Editor string `arg:"" help:"Editor to open the file with." enum:"auto,vscode,intellij" default:"auto"`
23+
24+
TerminalProgram string `help:"Terminal program this command is running in which can influence 'auto' editor" env:"TERM_PROGRAM" hidden:""`
25+
TerminalEmulator string `help:"Terminal emulator can influence 'auto' editor" env:"TERMINAL_EMULATOR" hidden:""`
26+
}
27+
28+
func (i openCmd) Run(ctx context.Context, ktctx *kong.Context, client ftlv1connect.AdminServiceClient, pc projectconfig.Config) error {
29+
ref := i.Ref.ToSchema()
30+
resp, err := client.GetSchema(ctx, connect.NewRequest(&ftlv1.GetSchemaRequest{}))
31+
if err != nil {
32+
return fmt.Errorf("could not get schema: %w", err)
33+
}
34+
sch, err := mergedSchemaFromResp(resp.Msg)
35+
if err != nil {
36+
return err
37+
}
38+
decl, ok := sch.Resolve(ref).Get()
39+
if !ok {
40+
return fmt.Errorf("could not find %q", ref)
41+
}
42+
if decl.Position().Filename == "" {
43+
return fmt.Errorf("could not find location of %q", ref)
44+
}
45+
if i.Editor == "auto" {
46+
if i.TerminalProgram == "vscode" {
47+
i.Editor = "vscode"
48+
} else if strings.Contains(i.TerminalEmulator, "JetBrains") {
49+
i.Editor = "intellij"
50+
} else {
51+
return fmt.Errorf(`could not auto choose default editor, use one of the following values:
52+
vscode
53+
intellij`)
54+
}
55+
}
56+
switch i.Editor {
57+
case "vscode":
58+
return openVisualStudioCode(ctx, decl.Position(), pc.Root())
59+
case "intellij":
60+
return openIntelliJ(ctx, decl.Position(), pc.Root())
61+
default:
62+
// TODO: print out valid editors
63+
return fmt.Errorf("unsupported editor %q", i.Editor)
64+
}
65+
}
66+
67+
// mergedSchemaFromResp parses the schema from the response and applies any changesets.
68+
// modules that are removed by a changeset stay in the schema.
69+
func mergedSchemaFromResp(resp *ftlv1.GetSchemaResponse) (*schema.Schema, error) {
70+
sch, err := schema.FromProto(resp.Schema)
71+
if err != nil {
72+
return nil, fmt.Errorf("could not parse schema: %w", err)
73+
}
74+
for _, cs := range resp.Changesets {
75+
fmt.Printf("Considering changeset %q\n", cs.Key)
76+
77+
for _, module := range cs.Modules {
78+
moduleSch, err := schema.ModuleFromProto(module)
79+
if err != nil {
80+
return nil, fmt.Errorf("could not parse module: %w", err)
81+
}
82+
83+
var found bool
84+
for i, m := range sch.Modules {
85+
if m.Name != module.Name {
86+
continue
87+
}
88+
sch.Modules[i] = moduleSch
89+
found = true
90+
break
91+
}
92+
if !found {
93+
sch.Modules = append(sch.Modules, moduleSch)
94+
}
95+
}
96+
}
97+
return sch, nil
98+
}
99+
100+
func openVisualStudioCode(ctx context.Context, pos schema.Position, projectRoot string) error {
101+
// TODO: schema filepath is has a root of `ftl/modulename`. Replace it with module root
102+
path := pos.Filename + ":" + fmt.Sprint(pos.Line)
103+
if pos.Column > 0 {
104+
path += ":" + fmt.Sprint(pos.Column)
105+
}
106+
err := exec.Command(ctx, log.Debug, ".", "code", projectRoot, "--goto", path).RunBuffered(ctx)
107+
if err != nil {
108+
return fmt.Errorf("could not open visual studio code: %w", err)
109+
}
110+
return nil
111+
}
112+
113+
func openIntelliJ(ctx context.Context, pos schema.Position, projectRoot string) error {
114+
// TODO: schema filepath is has a root of `ftl/modulename`. Replace it with module root
115+
// TODO: if idea is not available, explain how to activate it
116+
err := exec.Command(ctx, log.Debug, ".", "idea", projectRoot, "--line", strconv.Itoa(pos.Line), "--column", strconv.Itoa(pos.Column), pos.Filename).RunBuffered(ctx)
117+
if err != nil {
118+
return fmt.Errorf("could not open IntelliJ IDEA: %w", err)
119+
}
120+
return nil
121+
}

cmd/ftl/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type SharedCLI struct {
6969
Goose gooseCmd `cmd:"" help:"Run a goose command."`
7070
Mysql mySQLCmd `cmd:"" help:"Manage MySQL databases."`
7171
Postgres postgresCmd `cmd:"" help:"Manage PostgreSQL databases."`
72+
Open openCmd `cmd:"" help:"Open a file in the editor at the location of a schema declaration."`
7273
}
7374

7475
type CLI struct {

internal/terminal/predictors.go

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
func Predictors(view *schemaeventsource.View) map[string]complete.Predictor {
1313
return map[string]complete.Predictor{
14+
"decls": &declPredictor[schema.Decl]{view: *view},
1415
"verbs": &declPredictor[*schema.Verb]{view: *view},
1516
"configs": &declPredictor[*schema.Config]{view: *view},
1617
"secrets": &declPredictor[*schema.Secret]{view: *view},

0 commit comments

Comments
 (0)