generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 11
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
feat: open cmd #4707
Draft
matt2e
wants to merge
3
commits into
main
Choose a base branch
from
matt2e/open
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
feat: open cmd #4707
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"connectrpc.com/connect" | ||
"github.com/alecthomas/kong" | ||
|
||
ftlv1 "github.com/block/ftl/backend/protos/xyz/block/ftl/v1" | ||
"github.com/block/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect" | ||
"github.com/block/ftl/common/reflection" | ||
"github.com/block/ftl/common/schema" | ||
"github.com/block/ftl/internal/exec" | ||
"github.com/block/ftl/internal/log" | ||
"github.com/block/ftl/internal/projectconfig" | ||
) | ||
|
||
type openCmd struct { | ||
Ref reflection.Ref `arg:"" help:"Language of the module to create." placeholder:"MODULE.ITEM" predictor:"decls"` | ||
Editor string `arg:"" help:"Editor to open the file with." enum:"auto,vscode,intellij" default:"auto"` | ||
|
||
TerminalProgram string `help:"Terminal program this command is running in which can influence 'auto' editor" env:"TERM_PROGRAM" hidden:""` | ||
TerminalEmulator string `help:"Terminal emulator can influence 'auto' editor" env:"TERMINAL_EMULATOR" hidden:""` | ||
} | ||
|
||
func (i openCmd) Run(ctx context.Context, ktctx *kong.Context, client ftlv1connect.AdminServiceClient, pc projectconfig.Config) error { | ||
ref := i.Ref.ToSchema() | ||
resp, err := client.GetSchema(ctx, connect.NewRequest(&ftlv1.GetSchemaRequest{})) | ||
if err != nil { | ||
return fmt.Errorf("could not get schema: %w", err) | ||
} | ||
// merge changesets into schema so we get the latest decl positions | ||
sch, err := mergedSchemaFromResp(resp.Msg) | ||
if err != nil { | ||
return err | ||
} | ||
decl, ok := sch.Resolve(ref).Get() | ||
if !ok { | ||
return fmt.Errorf("could not find %q", ref) | ||
} | ||
if decl.Position().Filename == "" { | ||
return fmt.Errorf("could not find location of %q", ref) | ||
} | ||
if i.Editor == "auto" { | ||
if i.TerminalProgram == "vscode" { | ||
i.Editor = "vscode" | ||
} else if strings.Contains(i.TerminalEmulator, "JetBrains") { | ||
i.Editor = "intellij" | ||
} else { | ||
return fmt.Errorf(`could not auto choose default editor, use one of the following values: | ||
vscode | ||
intellij`) | ||
} | ||
} | ||
switch i.Editor { | ||
case "vscode": | ||
return openVisualStudioCode(ctx, decl.Position(), pc.Root()) | ||
case "intellij": | ||
return openIntelliJ(ctx, decl.Position(), pc.Root()) | ||
default: | ||
// TODO: print out valid editors | ||
return fmt.Errorf("unsupported editor %q", i.Editor) | ||
} | ||
} | ||
|
||
// mergedSchemaFromResp parses the schema from the response and applies any changesets. | ||
// modules that are removed by a changeset stay in the schema. | ||
func mergedSchemaFromResp(resp *ftlv1.GetSchemaResponse) (*schema.Schema, error) { | ||
sch, err := schema.FromProto(resp.Schema) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not parse schema: %w", err) | ||
} | ||
for _, cs := range resp.Changesets { | ||
fmt.Printf("Considering changeset %q\n", cs.Key) | ||
|
||
for _, module := range cs.Modules { | ||
moduleSch, err := schema.ModuleFromProto(module) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not parse module: %w", err) | ||
} | ||
|
||
var found bool | ||
for i, m := range sch.Modules { | ||
if m.Name != module.Name { | ||
continue | ||
} | ||
sch.Modules[i] = moduleSch | ||
found = true | ||
break | ||
} | ||
if !found { | ||
sch.Modules = append(sch.Modules, moduleSch) | ||
} | ||
} | ||
} | ||
return sch, nil | ||
} | ||
|
||
func openVisualStudioCode(ctx context.Context, pos schema.Position, projectRoot string) error { | ||
// TODO: schema filepath is has a root of `ftl/modulename`. Replace it with module root | ||
path := pos.Filename + ":" + fmt.Sprint(pos.Line) | ||
if pos.Column > 0 { | ||
path += ":" + fmt.Sprint(pos.Column) | ||
} | ||
err := exec.Command(ctx, log.Debug, ".", "code", projectRoot, "--goto", path).RunBuffered(ctx) | ||
if err != nil { | ||
return fmt.Errorf("could not open visual studio code: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func openIntelliJ(ctx context.Context, pos schema.Position, projectRoot string) error { | ||
// TODO: schema filepath is has a root of `ftl/modulename`. Replace it with module root | ||
// TODO: if idea is not available, explain how to activate it | ||
err := exec.Command(ctx, log.Debug, ".", "idea", projectRoot, "--line", strconv.Itoa(pos.Line), "--column", strconv.Itoa(pos.Column), pos.Filename).RunBuffered(ctx) | ||
if err != nil { | ||
return fmt.Errorf("could not open IntelliJ IDEA: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func filePathForPosition(pos schema.Position, modulePath string) string { | ||
// TODO: implement | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,7 @@ type SharedCLI struct { | |
Goose gooseCmd `cmd:"" help:"Run a goose command."` | ||
Mysql mySQLCmd `cmd:"" help:"Manage MySQL databases."` | ||
Postgres postgresCmd `cmd:"" help:"Manage PostgreSQL databases."` | ||
Open openCmd `cmd:"" help:"Open a file in the editor at the location of a schema declaration."` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. eh reword this like a human |
||
} | ||
|
||
type CLI struct { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wut?