Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cmd/cue: support --force for all commands that allow output flags
Browse files Browse the repository at this point in the history
Somewhat consolidates this logic.

Add tests for each for completeness (because not all codepaths are the
same).

Change-Id: If6296eef1bfd4f36a755546c9bb4de21a4c40cbc
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9567
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
  • Loading branch information
myitcv committed Apr 29, 2021
1 parent 2198ac3 commit b449c0f
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/cue/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ func (b *buildPlan) parseFlags() (err error) {
b.cfg.fileFilter = s
}
b.encConfig = &encoding.Config{
Force: flagForce.Bool(b.cmd),
Mode: b.cfg.outMode,
Stdin: b.cmd.InOrStdin(),
Stdout: b.cmd.OutOrStdout(),
Expand Down
2 changes: 0 additions & 2 deletions cmd/cue/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ yaml output as YAML
addOrphanFlags(cmd.Flags())

cmd.Flags().Bool(string(flagEscape), false, "use HTML escaping")

cmd.Flags().StringArrayP(string(flagExpression), "e", nil, "export this expression only")

cmd.Flags().StringArrayP(string(flagInject), "t", nil,
"set the value of a tagged field")

Expand Down
1 change: 1 addition & 0 deletions cmd/cue/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func addOutFlags(f *pflag.FlagSet, allowNonCUE bool) {
}
f.StringP(string(flagOutFile), "o", "",
`filename or - for stdout with optional file prefix (run 'cue filetypes' for more info)`)
f.BoolP(string(flagForce), "f", false, "force overwriting existing files")
}

func addGlobalFlags(f *pflag.FlagSet) {
Expand Down
1 change: 0 additions & 1 deletion cmd/cue/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ Example:
addOrphanFlags(cmd.Flags())

cmd.Flags().Bool(string(flagFiles), false, "split multiple entries into different files")
cmd.Flags().BoolP(string(flagForce), "f", false, "force overwriting existing files")
cmd.Flags().Bool(string(flagDryrun), false, "only run simulation")
cmd.Flags().BoolP(string(flagRecursive), "R", false, "recursively parse string values")

Expand Down
19 changes: 19 additions & 0 deletions cmd/cue/cmd/testdata/script/def_force.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Verify that def with --force works

# First time should simply succeed
cue def -o test.cue file.cue
cmp test.cue file.cue

# Second time will fail without --force
! cue def -o test.cue file.cue
stderr 'error writing "test.cue": file already exists'

# Second time with --force should succeed
cue def --force -o test.cue file.cue
cmp test.cue file.cue

-- file.cue --
package hello

#who: "World"
message: "Hello \(#who)!"
22 changes: 22 additions & 0 deletions cmd/cue/cmd/testdata/script/eval_force.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Verify that eval with --force works

# First time should simply succeed
cue eval -o eval.cue file.cue
cmp eval.cue eval.golden

# Second time will fail without --force
! cue eval -o eval.cue file.cue
stderr 'error writing "eval.cue": file already exists'

# Second time with --force should succeed
cue eval --force -o eval.cue file.cue
cmp eval.cue eval.golden

-- file.cue --
package hello

#who: "World"
message: "Hello \(#who)!"
-- eval.golden --
#who: "World"
message: "Hello World!"
21 changes: 21 additions & 0 deletions cmd/cue/cmd/testdata/script/export_force.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Verify that export with --force works

# First time should simply succeed
cue export -o test.yml file.cue
cmp test.yml test.yml.golden

# Second time will fail without --force
! cue export -o test.yml file.cue
stderr 'error writing "test.yml": file already exists'

# Second time with --force should succeed
cue export --force -o test.yml file.cue
cmp test.yml test.yml.golden

-- file.cue --
package hello

#who: "World"
message: "Hello \(#who)!"
-- test.yml.golden --
message: Hello World!
19 changes: 19 additions & 0 deletions cmd/cue/cmd/testdata/script/trim_force.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Verify that trim with --force works

# First time should simply succeed
cue trim -o test.cue file.cue
cmp test.cue file.cue

# Second time will fail without --force
! cue trim -o test.cue file.cue
stderr 'error writing "test.cue": file already exists'

# Second time with --force should succeed
cue trim --force -o test.cue file.cue
cmp test.cue file.cue

-- file.cue --
package hello

#who: "World"
message: "Hello \(#who)!"
5 changes: 2 additions & 3 deletions cmd/cue/cmd/trim.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,11 @@ func runTrim(cmd *Command, args []string) error {
instances := buildInstances(cmd, binst)

dst := flagOutFile.String(cmd)
if dst != "" && dst != "-" {
if dst != "" && dst != "-" && !flagForce.Bool(cmd) {
switch _, err := os.Stat(dst); {
case os.IsNotExist(err):
case err == nil:
default:
return fmt.Errorf("error creating file: %v", err)
return fmt.Errorf("error writing %q: file already exists", dst)
}
}

Expand Down

0 comments on commit b449c0f

Please sign in to comment.