From d74f28de52d339f2bdb82a8d167b2cb16ca934b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisend=C3=B6rfer?= Date: Mon, 28 Feb 2022 14:17:36 +0100 Subject: [PATCH] cmd/export: emit comments by default --- cmd/cue/cmd/common.go | 17 ++++++------- cmd/cue/cmd/export.go | 1 - cmd/cue/cmd/flags.go | 25 +++++++++---------- .../testdata/script/export_yaml_comments.txt | 2 +- cue/types.go | 2 +- internal/encoding/encoder.go | 2 +- internal/encoding/encoding.go | 9 +++---- pkg/encoding/yaml/manual.go | 12 ++++----- 8 files changed, 32 insertions(+), 38 deletions(-) diff --git a/cmd/cue/cmd/common.go b/cmd/cue/cmd/common.go index 735e643b950..9e0d16c0381 100644 --- a/cmd/cue/cmd/common.go +++ b/cmd/cue/cmd/common.go @@ -719,15 +719,14 @@ 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), - PreserveComments: flagPreserveComments.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), } return nil } diff --git a/cmd/cue/cmd/export.go b/cmd/cue/cmd/export.go index ea97b90bd6d..bec67046452 100644 --- a/cmd/cue/cmd/export.go +++ b/cmd/cue/cmd/export.go @@ -96,7 +96,6 @@ 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") diff --git a/cmd/cue/cmd/flags.go b/cmd/cue/cmd/flags.go index e47b3d7d649..579cbd2c458 100644 --- a/cmd/cue/cmd/flags.go +++ b/cmd/cue/cmd/flags.go @@ -21,19 +21,18 @@ 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" - flagPreserveComments flagName = "preserve-comments" + 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" flagExpression flagName = "expression" flagSchema flagName = "schema" diff --git a/cmd/cue/cmd/testdata/script/export_yaml_comments.txt b/cmd/cue/cmd/testdata/script/export_yaml_comments.txt index 6b350d35659..9a92c959e0a 100644 --- a/cmd/cue/cmd/testdata/script/export_yaml_comments.txt +++ b/cmd/cue/cmd/testdata/script/export_yaml_comments.txt @@ -1,4 +1,4 @@ -cue export --preserve-comments --out yaml ./hello +cue export --out yaml ./hello cmp stdout expect-stdout -- expect-stdout -- # cloud-config diff --git a/cue/types.go b/cue/types.go index c82737975b4..59933d46198 100644 --- a/cue/types.go +++ b/cue/types.go @@ -2132,7 +2132,7 @@ func All() Option { // Docs indicates whether docs should be included. func Docs(include bool) Option { - return func(p *options) { p.docs = true } + return func(p *options) { p.docs = include } } // Definitions indicates whether definitions should be included. diff --git a/internal/encoding/encoder.go b/internal/encoding/encoder.go index 21474331bdf..4d9b0ba5e89 100644 --- a/internal/encoding/encoder.go +++ b/internal/encoding/encoder.go @@ -182,7 +182,7 @@ func NewEncoder(f *build.File, cfg *Config) (*Encoder, error) { } streamed = true - str, err := yaml.Marshal(v, cue.Docs(e.cfg.PreserveComments)) + str, err := yaml.Marshal(v) if err != nil { return err } diff --git a/internal/encoding/encoding.go b/internal/encoding/encoding.go index fc4003aaba1..522f023c71f 100644 --- a/internal/encoding/encoding.go +++ b/internal/encoding/encoding.go @@ -151,11 +151,10 @@ 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 - PreserveComments bool + Force bool // overwrite existing files. + Strict bool + Stream bool // will potentially write more than one document per file + AllErrors bool Schema cue.Value // used for schema-based decoding diff --git a/pkg/encoding/yaml/manual.go b/pkg/encoding/yaml/manual.go index 4b37025198b..7db33cb7224 100644 --- a/pkg/encoding/yaml/manual.go +++ b/pkg/encoding/yaml/manual.go @@ -26,7 +26,7 @@ import ( ) // Marshal returns the YAML encoding of v. -func Marshal(v cue.Value, opts ...cue.Option) (string, error) { +func Marshal(v cue.Value) (string, error) { if err := v.Validate(cue.Concrete(true)); err != nil { if err := v.Validate(); err != nil { return "", err @@ -35,14 +35,13 @@ func Marshal(v cue.Value, opts ...cue.Option) (string, error) { // messages can be passed. return "", internal.ErrIncomplete } - opts = append([]cue.Option{cue.Final(), cue.Concrete(true)}, opts...) - n := v.Syntax(opts...) + n := v.Syntax(cue.Final(), cue.Concrete(true), cue.Docs(true)) b, err := cueyaml.Encode(n) return string(b), err } // MarshalStream returns the YAML encoding of v. -func MarshalStream(v cue.Value, opts ...cue.Option) (string, error) { +func MarshalStream(v cue.Value) (string, error) { // TODO: return an io.Reader and allow asynchronous processing. iter, err := v.List() if err != nil { @@ -54,7 +53,7 @@ func MarshalStream(v cue.Value, opts ...cue.Option) (string, error) { buf.WriteString("---\n") } v := iter.Value() - if err := v.Validate(cue.Concrete(true)); err != nil { + if err := v.Validate(cue.Concrete(true), cue.Docs(true)); err != nil { if err := v.Validate(); err != nil { return "", err } @@ -62,8 +61,7 @@ func MarshalStream(v cue.Value, opts ...cue.Option) (string, error) { // messages can be passed. return "", internal.ErrIncomplete } - opts = append([]cue.Option{cue.Final(), cue.Concrete(true)}, opts...) - n := v.Syntax(opts...) + n := v.Syntax(cue.Final(), cue.Concrete(true)) b, err := cueyaml.Encode(n) if err != nil { return "", err