From 9b0de10d2637c6e1605725aa85b31c7887d1805e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisend=C3=B6rfer?= Date: Fri, 25 Feb 2022 00:07:59 +0100 Subject: [PATCH 1/2] cmd/export: add --preserve-comments flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #1180 Signed-off-by: Felix Geisendörfer --- cmd/cue/cmd/common.go | 17 +++++++------ cmd/cue/cmd/export.go | 1 + cmd/cue/cmd/flags.go | 25 ++++++++++--------- .../testdata/script/export_yaml_comments.txt | 18 +++++++++++++ internal/encoding/encoder.go | 2 +- internal/encoding/encoding.go | 9 ++++--- pkg/encoding/yaml/manual.go | 10 +++++--- 7 files changed, 53 insertions(+), 29 deletions(-) create mode 100644 cmd/cue/cmd/testdata/script/export_yaml_comments.txt diff --git a/cmd/cue/cmd/common.go b/cmd/cue/cmd/common.go index 9e0d16c0381..735e643b950 100644 --- a/cmd/cue/cmd/common.go +++ b/cmd/cue/cmd/common.go @@ -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 } diff --git a/cmd/cue/cmd/export.go b/cmd/cue/cmd/export.go index bec67046452..ea97b90bd6d 100644 --- a/cmd/cue/cmd/export.go +++ b/cmd/cue/cmd/export.go @@ -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") diff --git a/cmd/cue/cmd/flags.go b/cmd/cue/cmd/flags.go index 579cbd2c458..e47b3d7d649 100644 --- a/cmd/cue/cmd/flags.go +++ b/cmd/cue/cmd/flags.go @@ -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" diff --git a/cmd/cue/cmd/testdata/script/export_yaml_comments.txt b/cmd/cue/cmd/testdata/script/export_yaml_comments.txt new file mode 100644 index 00000000000..6b350d35659 --- /dev/null +++ b/cmd/cue/cmd/testdata/script/export_yaml_comments.txt @@ -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?" +} diff --git a/internal/encoding/encoder.go b/internal/encoding/encoder.go index 4d9b0ba5e89..21474331bdf 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) + str, err := yaml.Marshal(v, cue.Docs(e.cfg.PreserveComments)) if err != nil { return err } diff --git a/internal/encoding/encoding.go b/internal/encoding/encoding.go index 522f023c71f..fc4003aaba1 100644 --- a/internal/encoding/encoding.go +++ b/internal/encoding/encoding.go @@ -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 diff --git a/pkg/encoding/yaml/manual.go b/pkg/encoding/yaml/manual.go index ee228f93a2e..4b37025198b 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) (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 @@ -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 { @@ -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 From 754120447cf2cca87b56be07bb86356562fc2921 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 2/2] cmd/export: emit comments by default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Felix Geisendörfer --- 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