Skip to content

Commit

Permalink
pretty print json output
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 committed Apr 6, 2024
1 parent 94f9092 commit 3467312
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
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/hashicorp/go-version v1.2.1 // indirect
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKL
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f h1:7LYC+Yfkj3CTRcShK0KOL/w6iTiKyqqBA9a41Wnggw8=
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down
1 change: 1 addition & 0 deletions internal/state/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (s *Service) Reload(workspaceID resource.ID) (*task.Task, error) {

task, err := s.createTask(workspaceID, task.CreateOptions{
Command: []string{"show", "-json"},
JSON: true,
AfterError: func(t *task.Task) {
s.logger.Error("reloading state", "error", t.Err, "workspace", ws)
},
Expand Down
4 changes: 4 additions & 0 deletions internal/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Task struct {
Blocking bool
State Status
Env []string
JSON bool

program string
exclusive bool
Expand Down Expand Up @@ -94,6 +95,8 @@ type CreateOptions struct {
Blocking bool
// Globally exclusive task - at most only one such task can be running
Exclusive bool
// Set to true to indicate that the task produces JSON output
JSON bool
// Call this function after the task has successfully finished
AfterExited func(*Task)
// Call this function after the task is enqueued.
Expand Down Expand Up @@ -123,6 +126,7 @@ func (f *factory) newTask(opts CreateOptions) (*Task, error) {
Path: opts.Path,
Args: opts.Args,
Env: opts.Env,
JSON: opts.JSON,
Blocking: opts.Blocking,
exclusive: opts.Exclusive,
AfterExited: opts.AfterExited,
Expand Down
24 changes: 23 additions & 1 deletion internal/tui/task/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/hokaccha/go-prettyjson"
"github.com/leg100/pug/internal/resource"
"github.com/leg100/pug/internal/task"
"github.com/leg100/pug/internal/tui"
Expand Down Expand Up @@ -112,7 +113,28 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.content = wordwrap.String(m.content, m.width)
m.viewport.SetContent(m.content)
m.viewport.GotoBottom()
if !msg.eof {
if msg.eof {
if m.task.JSON {
// Prettify JSON output from task. This can only be done once
// the task has finished and has produced complete and
// syntactically valid json object(s).
//
// Note: terraform commands such as `state -json` can produce
// json strings with embedded newlines, which is invalid json
// and breaks the pretty printer. So we escape the newlines.
//
// TODO: avoid casting to string and back, thereby avoiding
// unnecessary allocations.
m.content = strings.ReplaceAll(m.content, "\n", "\\n")
if b, err := prettyjson.Format([]byte(m.content)); err != nil {
cmds = append(cmds, tui.ReportError(err, "pretty printing task json output"))
} else {
m.content = string(b)
m.viewport.SetContent(string(b))
m.viewport.GotoBottom()
}
}
} else {
cmds = append(cmds, m.getOutput)
}
case resource.Event[*task.Task]:
Expand Down

0 comments on commit 3467312

Please sign in to comment.