Skip to content

Commit

Permalink
cmd/cue: implement cue import --dry-run
Browse files Browse the repository at this point in the history
The flag had been documented and declared for a while,
but it never did anything - nor did we have any tests for it.
Its presence aligns with the docs, and makes a new encoding test easier.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: If149e079a8310bbd9d06330256cbd471bfe0f95a
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198967
Reviewed-by: Roger Peppe <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
mvdan committed Aug 7, 2024
1 parent 334a6fe commit bcbc7cd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
13 changes: 11 additions & 2 deletions cmd/cue/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Example:
addOrphanFlags(cmd.Flags())

cmd.Flags().Bool(string(flagFiles), false, "split multiple entries into different files")
cmd.Flags().Bool(string(flagDryRun), false, "only run simulation")
cmd.Flags().Bool(string(flagDryRun), false, "show what files would be created")
cmd.Flags().BoolP(string(flagRecursive), "R", false, "recursively parse string values")
cmd.Flags().StringArray(string(flagExt), nil, "match files with these extensions")

Expand Down Expand Up @@ -442,7 +442,7 @@ func getFilename(b *buildPlan, f *ast.File, root string, force bool) (filename s
if !force {
// TODO: mimic old behavior: write to stderr, but do not exit
// with error code. Consider what is best to do here.
stderr := b.cmd.Command.OutOrStderr()
stderr := b.cmd.OutOrStderr()
if root != "" {
cueFile, _ = filepath.Rel(root, cueFile)
}
Expand Down Expand Up @@ -478,6 +478,15 @@ func handleFile(b *buildPlan, f *ast.File) (err error) {
}

func writeFile(p *buildPlan, f *ast.File, cueFile string) error {
if flagDryRun.Bool(p.cmd) {
cueFile, err := filepath.Rel(rootWorkingDir, cueFile)
if err != nil {
return err
}
stderr := p.cmd.OutOrStderr()
fmt.Fprintf(stderr, "importing into %s\n", cueFile)
return nil
}
b, err := format.Node(f, format.Simplify())
if err != nil {
return fmt.Errorf("error formatting file: %v", err)
Expand Down
20 changes: 9 additions & 11 deletions cmd/cue/cmd/testdata/script/encoding_jsonl.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,15 @@ cmp stdout export-one.stdout
# TODO(mvdan): note that `cue import --dry-run` does not work,
# so we need to use rm and stderr checks to ensure we create the files we expect.

! exec cue import --list .
! exec cue import --list --dry-run .
stderr 'invalid JSON .*input-many.json.* after top-level value'
rm input-many.json

rm input-jsonl.cue input-ldjson.cue input-one.cue
exec cue import --list .
! stderr 'Skipping file.*already exists'
exists input-jsonl.cue input-ldjson.cue input-one.cue
! exists input-none.cue input-ndjson.cue
exec cue import --list --dry-run .
cmp stderr import.stderr

rm input-jsonl.cue input-ldjson.cue input-one.cue
exec cue import json --list .
! stderr 'Skipping file.*already exists'
exists input-jsonl.cue input-ldjson.cue input-one.cue
! exists input-none.cue input-ndjson.cue
exec cue import json --list --dry-run .
cmp stderr import.stderr

-- input-none.none --
{"a": "one"}
Expand Down Expand Up @@ -81,3 +75,7 @@ exists input-jsonl.cue input-ldjson.cue input-one.cue
{"a": "json one"}
{"b": "json two"}
{"c1": "json three", "c2": "json four"}
-- import.stderr --
importing into input-jsonl.cue
importing into input-ldjson.cue
importing into input-one.cue

0 comments on commit bcbc7cd

Please sign in to comment.