Skip to content

Commit

Permalink
feat: color JSON output (#2748)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
stuartwdouglas and github-actions[bot] authored Sep 20, 2024
1 parent 91ce79d commit 597b985
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 3 deletions.
3 changes: 2 additions & 1 deletion frontend/cli/cmd_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/TBD54566975/ftl/go-runtime/ftl/reflection"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/rpc"
"github.com/TBD54566975/ftl/internal/status"
)

type callCmd struct {
Expand Down Expand Up @@ -77,7 +78,7 @@ func callVerb(ctx context.Context, client ftlv1connect.VerbServiceClient, ctlCli
return fmt.Errorf("verb error: %s", resp.Error.Message)

case *ftlv1.CallResponse_Body:
fmt.Println(string(resp.Body))
status.PrintJSON(ctx, resp.Body)
}
return nil
}
Expand Down
5 changes: 4 additions & 1 deletion frontend/cli/cmd_replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/TBD54566975/ftl/go-runtime/ftl/reflection"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/rpc"
"github.com/TBD54566975/ftl/internal/status"
)

type replayCmd struct {
Expand Down Expand Up @@ -97,6 +98,8 @@ func (c *replayCmd) Run(ctx context.Context, client ftlv1connect.VerbServiceClie
}
requestJSON := events.Msg.GetEvents()[0].GetCall().Request

logger.Infof("Calling %s with body:\n%s", c.Verb, requestJSON)
logger.Infof("Calling %s with body:", c.Verb)
status.PrintJSON(ctx, []byte(requestJSON))
logger.Infof("Response:")
return callVerb(ctx, client, ctlCli, c.Verb, []byte(requestJSON))
}
3 changes: 2 additions & 1 deletion frontend/cli/cmd_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1"
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
console "github.com/TBD54566975/ftl/internal/status"
)

type statusCmd struct {
Expand All @@ -35,6 +36,6 @@ func (s *statusCmd) Run(ctx context.Context, client ftlv1connect.ControllerServi
if err != nil {
return fmt.Errorf("failed to marshal status: %w", err)
}
fmt.Printf("%s\n", data)
console.PrintJSON(ctx, data)
return nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ require (
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/sqlc-dev/pqtype v0.3.0
github.com/swaggest/jsonschema-go v0.3.72
github.com/tidwall/pretty v1.2.1
github.com/tink-crypto/tink-go-awskms v0.0.0-20230616072154-ba4f9f22c3e9
github.com/tink-crypto/tink-go/v2 v2.2.0
github.com/titanous/json5 v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions internal/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"unicode/utf8"

"github.com/alecthomas/atomic"
"github.com/tidwall/pretty"
"golang.org/x/term"
)

Expand Down Expand Up @@ -165,6 +166,18 @@ func UpdateModuleState(ctx context.Context, module string, state BuildState) {
sm.SetModuleState(module, state)
}

// PrintJSON prints a json string to the terminal
// It probably doesn't belong here, but it will be moved later with the interactive terminal work
func PrintJSON(ctx context.Context, json []byte) {
sm := FromContext(ctx)
if _, ok := sm.(*terminalStatusManager); ok {
// ANSI enabled
fmt.Printf("%s\n", pretty.Color(pretty.Pretty(json), nil))
} else {
fmt.Printf("%s\n", json)
}
}

func (r *terminalStatusManager) gotoCoords(line int, col int) {
r.underlyingWrite(fmt.Sprintf("\033[%d;%dH", line, col))
}
Expand Down

0 comments on commit 597b985

Please sign in to comment.