Skip to content

Commit

Permalink
Add --compact option for dump and sync and make default pritty print
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mizutani committed Sep 15, 2022
1 parent 4888b69 commit 470d7f6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
28 changes: 24 additions & 4 deletions cmd/goast/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,29 @@ func run(args []string) error {
}

func cmdDump() *cli.Command {
var opt inspectOptions
var (
opt inspectOptions
compact bool
)

return &cli.Command{
Name: "dump",
Usage: "output go codes as AST",
Aliases: []string{"d"},
Flags: opt.Flags(),
Flags: append(opt.Flags(), []cli.Flag{
&cli.BoolFlag{
Name: "compact",
Aliases: []string{"c"},
Usage: "Compact instead of pretty-printed output",
Destination: &compact,
},
}...),
Action: func(c *cli.Context) error {
codes := c.Args().Slice()

g := goast.New(
goast.WithInspectOptions(opt.Options()...),
goast.WithCompact(compact),
)
if err := walkCode(codes, func(filePath string, r io.Reader) error {
return g.Dump(filePath, r, os.Stdout)
Expand All @@ -134,13 +145,22 @@ func cmdDump() *cli.Command {
}

func cmdSync() *cli.Command {

var compact bool
return &cli.Command{
Name: "sync",
Usage: "sync dump of go codes to files",
Aliases: []string{"s"},
Flags: []cli.Flag{&cli.BoolFlag{
Name: "compact",
Aliases: []string{"c"},
Usage: "Compact instead of pretty-printed output",
Destination: &compact,
},
},
Action: func(c *cli.Context) error {
g := goast.New()
g := goast.New(
goast.WithCompact(compact),
)

for _, src := range c.Args().Slice() {
if err := g.Sync(src); err != nil {
Expand Down
13 changes: 7 additions & 6 deletions dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package goast

import (
"encoding/json"
"fmt"
"go/parser"
"go/token"
"io"
Expand All @@ -11,14 +10,16 @@ import (
)

func (x *Goast) Dump(filePath string, r io.Reader, w io.Writer) error {
encoder := json.NewEncoder(w)
if !x.dumpCompact {
encoder.SetIndent("", " ")
}

dump := func(data *Node) error {
if x.dumpHook == nil {
raw, err := json.Marshal(data)
if err != nil {
return goerr.Wrap(err)
if err := encoder.Encode(data); err != nil {
return goerr.Wrap(err, "failed to encode dump data")
}

fmt.Fprintln(w, string(raw))
return nil
} else {
return x.dumpHook(data, w)
Expand Down
9 changes: 8 additions & 1 deletion goast.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ type Goast struct {
mkdir func(path string, perm fs.FileMode) error
walk func(src string, cb func(fpath string, r io.Reader) error) error

dumpHook DumpHook
dumpCompact bool
dumpHook DumpHook
}

type Option func(g *Goast)
Expand Down Expand Up @@ -54,3 +55,9 @@ func WithDumpHook(hook DumpHook) Option {
}

type DumpHook func(node *Node, w io.Writer) error

func WithCompact(enable bool) Option {
return func(g *Goast) {
g.dumpCompact = enable
}
}
6 changes: 5 additions & 1 deletion sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ func (x *Goast) Sync(src string) error {
}
}()

if err := json.NewEncoder(fd).Encode(data); err != nil {
encoder := json.NewEncoder(fd)
if !x.dumpCompact {
encoder.SetIndent("", " ")
}
if err := encoder.Encode(data); err != nil {
return goerr.Wrap(err, "failed to encode goast.Node")
}

Expand Down

0 comments on commit 470d7f6

Please sign in to comment.