Skip to content

Commit

Permalink
internal/cmd/cue-ast-tool: add "join" command
Browse files Browse the repository at this point in the history
When reducing CUE bug reproducers, I often begin from the evaluation
of a CUE package instance. It is easier to reduce a single CUE file
rather than multiple files, as one can load it into an editor
and quickly delete chunks of it or search through it.

While this is not hard to do, it's tedious for a human;
the package declarations need to be removed, and the imports joined
at the top of the new file. This is, however, easy to do in Go.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Ibf89de45ae6ec5d88037f7417d00aa2129cd73e0
Dispatch-Trailer: {"type":"trybot","CL":1200356,"patchset":2,"ref":"refs/changes/56/1200356/2","targetBranch":"master"}
  • Loading branch information
mvdan authored and cueckoo committed Aug 30, 2024
1 parent 0047626 commit 2cb4611
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions internal/cmd/cue-ast-tool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ import (
"fmt"
"log"
"os"
"slices"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/load"
"cuelang.org/go/internal/astinternal"
)
Expand All @@ -39,6 +42,10 @@ usage of cue-ast-tool:
-omitempty omit empty and invalid values
cue-ast-tool join [flags] [inputs]
Join the input as a single file.
See 'cue help inputs' as well.
`[1:])
}
Expand Down Expand Up @@ -71,6 +78,36 @@ See 'cue help inputs' as well.
os.Stdout.Write(out)
}
}
case "join":
// TODO: add a flag drop comments, which is useful when reducing bug reproducers.
flag.CommandLine.Parse(args)

var jointImports []*ast.ImportSpec
var jointFields []ast.Decl
insts := load.Instances(flag.Args(), &load.Config{})
if len(insts) != 1 {
log.Fatal("joining multiple instances is not possible yet")
}
inst := insts[0]
if err := inst.Err; err != nil {
log.Fatal(errors.Details(err, nil))
}
for _, file := range inst.Files {
jointImports = slices.Concat(jointImports, file.Imports)

fields := file.Decls[len(file.Preamble()):]
jointFields = slices.Concat(jointFields, fields)
}
// TODO: we should sort and deduplicate imports.
joint := &ast.File{Decls: slices.Concat([]ast.Decl{
&ast.ImportDecl{Specs: jointImports},
}, jointFields)}

out, err := format.Node(joint)
if err != nil {
log.Fatal(err)
}
os.Stdout.Write(out)
default:
flag.Usage()
}
Expand Down

0 comments on commit 2cb4611

Please sign in to comment.