generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #1989 ``` ftl schema diff --help Usage: ftl schema diff <other-endpoint> [flags] Print any schema differences between this cluster and another cluster. Returns an exit code of 1 if there are differences. Arguments: <other-endpoint> Other endpoint URL to compare against. --endpoint=http://127.0.0.1:8892 FTL endpoint to bind/connect to ($FTL_ENDPOINT). --color Enable colored output regardless of TTY. ``` - Compares `<other-endpoint>` vs `--endpoint` into a unified diff with color. - Exits with 1 if there is a difference. - Detects tty and will not emit ANSI unless `--color` is passed in. <img width="1104" alt="image" src="https://github.com/TBD54566975/ftl/assets/31338/7aa704ae-9df0-48c7-90d5-56f97f4d556c">
- Loading branch information
Showing
4 changed files
with
90 additions
and
1 deletion.
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
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,85 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
|
||
"connectrpc.com/connect" | ||
"github.com/hexops/gotextdiff" | ||
"github.com/hexops/gotextdiff/myers" | ||
"github.com/hexops/gotextdiff/span" | ||
"github.com/mattn/go-isatty" | ||
|
||
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1" | ||
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect" | ||
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema" | ||
"github.com/TBD54566975/ftl/backend/schema" | ||
"github.com/TBD54566975/ftl/internal/log" | ||
"github.com/TBD54566975/ftl/internal/rpc" | ||
"github.com/alecthomas/chroma/v2/quick" | ||
) | ||
|
||
type schemaDiffCmd struct { | ||
OtherEndpoint url.URL `arg:"" help:"Other endpoint URL to compare against."` | ||
Color bool `help:"Enable colored output regardless of TTY."` | ||
} | ||
|
||
func (d *schemaDiffCmd) Run(ctx context.Context, currentURL *url.URL) error { | ||
other, err := schemaForURL(ctx, d.OtherEndpoint) | ||
if err != nil { | ||
return fmt.Errorf("failed to get other schema: %w", err) | ||
} | ||
current, err := schemaForURL(ctx, *currentURL) | ||
if err != nil { | ||
return fmt.Errorf("failed to get current schema: %w", err) | ||
} | ||
|
||
edits := myers.ComputeEdits(span.URIFromPath(""), other.String(), current.String()) | ||
diff := fmt.Sprint(gotextdiff.ToUnified(d.OtherEndpoint.String(), currentURL.String(), other.String(), edits)) | ||
|
||
color := d.Color || isatty.IsTerminal(os.Stdout.Fd()) | ||
if color { | ||
err = quick.Highlight(os.Stdout, diff, "diff", "terminal256", "solarized-dark") | ||
if err != nil { | ||
return fmt.Errorf("failed to highlight diff: %w", err) | ||
} | ||
} else { | ||
fmt.Print(diff) | ||
} | ||
|
||
// Similar to the `diff` command, exit with 1 if there are differences. | ||
if diff != "" { | ||
os.Exit(1) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func schemaForURL(ctx context.Context, url url.URL) (*schema.Schema, error) { | ||
client := rpc.Dial(ftlv1connect.NewControllerServiceClient, url.String(), log.Error) | ||
resp, err := client.PullSchema(ctx, connect.NewRequest(&ftlv1.PullSchemaRequest{})) | ||
if err != nil { | ||
return nil, fmt.Errorf("url %s: failed to pull schema: %w", url.String(), err) | ||
} | ||
|
||
pb := &schemapb.Schema{} | ||
for resp.Receive() { | ||
msg := resp.Msg() | ||
pb.Modules = append(pb.Modules, msg.Schema) | ||
if !msg.More { | ||
break | ||
} | ||
} | ||
if resp.Err() != nil { | ||
return nil, fmt.Errorf("url %s: failed to receive schema: %w", url.String(), resp.Err()) | ||
} | ||
|
||
s, err := schema.FromProto(pb) | ||
if err != nil { | ||
return nil, fmt.Errorf("url %s: failed to parse schema: %w", url.String(), err) | ||
} | ||
|
||
return s, nil | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.