Skip to content

Commit

Permalink
cmd/export: add --preserve-comments flag
Browse files Browse the repository at this point in the history
Closes #1180

Signed-off-by: Felix Geisendörfer <[email protected]>
  • Loading branch information
felixge committed Feb 24, 2022
1 parent 71e9d03 commit 9b0de10
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 29 deletions.
17 changes: 9 additions & 8 deletions cmd/cue/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,14 +719,15 @@ 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(),
ProtoPath: flagProtoPath.StringArray(b.cmd),
AllErrors: flagAllErrors.Bool(b.cmd),
PkgName: flagPackage.String(b.cmd),
Strict: flagStrict.Bool(b.cmd),
Force: flagForce.Bool(b.cmd),
Mode: b.cfg.outMode,
Stdin: b.cmd.InOrStdin(),
Stdout: b.cmd.OutOrStdout(),
ProtoPath: flagProtoPath.StringArray(b.cmd),
AllErrors: flagAllErrors.Bool(b.cmd),
PkgName: flagPackage.String(b.cmd),
Strict: flagStrict.Bool(b.cmd),
PreserveComments: flagPreserveComments.Bool(b.cmd),
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions cmd/cue/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ yaml output as YAML
addOrphanFlags(cmd.Flags())
addInjectionFlags(cmd.Flags(), false)

cmd.Flags().Bool(string(flagPreserveComments), false, "include comments in output")
cmd.Flags().Bool(string(flagEscape), false, "use HTML escaping")
cmd.Flags().StringArrayP(string(flagExpression), "e", nil, "export this expression only")

Expand Down
25 changes: 13 additions & 12 deletions cmd/cue/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ import (

// Common flags
const (
flagAll flagName = "all"
flagDryrun flagName = "dryrun"
flagVerbose flagName = "verbose"
flagAllErrors flagName = "all-errors"
flagTrace flagName = "trace"
flagForce flagName = "force"
flagIgnore flagName = "ignore"
flagStrict flagName = "strict"
flagSimplify flagName = "simplify"
flagPackage flagName = "package"
flagInject flagName = "inject"
flagInjectVars flagName = "inject-vars"
flagAll flagName = "all"
flagDryrun flagName = "dryrun"
flagVerbose flagName = "verbose"
flagAllErrors flagName = "all-errors"
flagTrace flagName = "trace"
flagForce flagName = "force"
flagIgnore flagName = "ignore"
flagStrict flagName = "strict"
flagSimplify flagName = "simplify"
flagPackage flagName = "package"
flagInject flagName = "inject"
flagInjectVars flagName = "inject-vars"
flagPreserveComments flagName = "preserve-comments"

flagExpression flagName = "expression"
flagSchema flagName = "schema"
Expand Down
18 changes: 18 additions & 0 deletions cmd/cue/cmd/testdata/script/export_yaml_comments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cue export --preserve-comments --out yaml ./hello
cmp stdout expect-stdout
-- expect-stdout --
# cloud-config
foo: bar
hello:
# occupy
world: from mars?
-- hello/hello.cue --
package hello

//cloud-config
foo: "bar"

hello: {
// occupy
world: "from mars?"
}
2 changes: 1 addition & 1 deletion internal/encoding/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func NewEncoder(f *build.File, cfg *Config) (*Encoder, error) {
}
streamed = true

str, err := yaml.Marshal(v)
str, err := yaml.Marshal(v, cue.Docs(e.cfg.PreserveComments))
if err != nil {
return err
}
Expand Down
9 changes: 5 additions & 4 deletions internal/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,11 @@ type Config struct {

PkgName string // package name for files to generate

Force bool // overwrite existing files.
Strict bool
Stream bool // will potentially write more than one document per file
AllErrors bool
Force bool // overwrite existing files.
Strict bool
Stream bool // will potentially write more than one document per file
AllErrors bool
PreserveComments bool

Schema cue.Value // used for schema-based decoding

Expand Down
10 changes: 6 additions & 4 deletions pkg/encoding/yaml/manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

// Marshal returns the YAML encoding of v.
func Marshal(v cue.Value) (string, error) {
func Marshal(v cue.Value, opts ...cue.Option) (string, error) {
if err := v.Validate(cue.Concrete(true)); err != nil {
if err := v.Validate(); err != nil {
return "", err
Expand All @@ -35,13 +35,14 @@ func Marshal(v cue.Value) (string, error) {
// messages can be passed.
return "", internal.ErrIncomplete
}
n := v.Syntax(cue.Final(), cue.Concrete(true))
opts = append([]cue.Option{cue.Final(), cue.Concrete(true)}, opts...)
n := v.Syntax(opts...)
b, err := cueyaml.Encode(n)
return string(b), err
}

// MarshalStream returns the YAML encoding of v.
func MarshalStream(v cue.Value) (string, error) {
func MarshalStream(v cue.Value, opts ...cue.Option) (string, error) {
// TODO: return an io.Reader and allow asynchronous processing.
iter, err := v.List()
if err != nil {
Expand All @@ -61,7 +62,8 @@ func MarshalStream(v cue.Value) (string, error) {
// messages can be passed.
return "", internal.ErrIncomplete
}
n := v.Syntax(cue.Final(), cue.Concrete(true))
opts = append([]cue.Option{cue.Final(), cue.Concrete(true)}, opts...)
n := v.Syntax(opts...)
b, err := cueyaml.Encode(n)
if err != nil {
return "", err
Expand Down

0 comments on commit 9b0de10

Please sign in to comment.