-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat(mdatagen): add gofmt validation to prevent template formatting regressions #14721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
atilsensalduz
wants to merge
1
commit into
open-telemetry:main
Choose a base branch
from
atilsensalduz:feature/mdatagen-gofmt-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+31
−14
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -58,19 +58,21 @@ func NewCommand() (*cobra.Command, error) { | |||||
| if err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
| noformat := os.Getenv("MDATAGEN_NOFORMAT") == "1" | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use env vars to set flags anywhere within mdatagen. I don't think starting this pattern is a good fit for this PR.
Suggested change
|
||||||
| rootCmd := &cobra.Command{ | ||||||
| Use: "mdatagen", | ||||||
| Version: ver, | ||||||
| SilenceUsage: true, | ||||||
| Args: cobra.ExactArgs(1), | ||||||
| RunE: func(_ *cobra.Command, args []string) error { | ||||||
| return run(args[0]) | ||||||
| return run(args[0], noformat) | ||||||
| }, | ||||||
| } | ||||||
| rootCmd.Flags().BoolVar(&noformat, "noformat", noformat, "skip gofmt formatting of generated Go files") | ||||||
| return rootCmd, nil | ||||||
| } | ||||||
|
|
||||||
| func run(ymlPath string) error { | ||||||
| func run(ymlPath string, noformat bool) error { | ||||||
| if ymlPath == "" { | ||||||
| return errors.New("argument must be metadata.yaml file") | ||||||
| } | ||||||
|
|
@@ -104,7 +106,7 @@ func run(ymlPath string) error { | |||||
| if !slices.Contains(nonComponents, md.Status.Class) { | ||||||
| toGenerate[filepath.Join(tmplDir, "status.go.tmpl")] = filepath.Join(codeDir, "generated_status.go") | ||||||
| err = generateFile(filepath.Join(tmplDir, "component_test.go.tmpl"), | ||||||
| filepath.Join(ymlDir, "generated_component_test.go"), md, packageName) | ||||||
| filepath.Join(ymlDir, "generated_component_test.go"), md, packageName, noformat) | ||||||
| if err != nil { | ||||||
| return err | ||||||
| } | ||||||
|
|
@@ -124,7 +126,7 @@ func run(ymlPath string) error { | |||||
| } | ||||||
|
|
||||||
| err = generateFile(filepath.Join(tmplDir, "package_test.go.tmpl"), | ||||||
| filepath.Join(ymlDir, "generated_package_test.go"), md, packageName) | ||||||
| filepath.Join(ymlDir, "generated_package_test.go"), md, packageName, noformat) | ||||||
| if err != nil { | ||||||
| return err | ||||||
| } | ||||||
|
|
@@ -220,12 +222,12 @@ func run(ymlPath string) error { | |||||
| } | ||||||
|
|
||||||
| for tmpl, dst := range toGenerate { | ||||||
| if err := generateFile(tmpl, dst, md, md.GeneratedPackageName); err != nil { | ||||||
| if err := generateFile(tmpl, dst, md, md.GeneratedPackageName, noformat); err != nil { | ||||||
| return err | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if err := generateConfigFiles(md, ymlDir); err != nil { | ||||||
| if err := generateConfigFiles(md, ymlDir, noformat); err != nil { | ||||||
| return fmt.Errorf("failed to generate config files: %w", err) | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -414,8 +416,8 @@ func executeTemplate(tmplFile string, md Metadata, goPackage string, fns templat | |||||
| return buf.Bytes(), nil | ||||||
| } | ||||||
|
|
||||||
| func generateFile(tmplFile, outputFile string, md Metadata, goPackage string) error { | ||||||
| return generateFileWithFns(tmplFile, outputFile, md, goPackage, getTemplateFuncMap(md)) | ||||||
| func generateFile(tmplFile, outputFile string, md Metadata, goPackage string, noformat bool) error { | ||||||
| return generateFileWithFns(tmplFile, outputFile, md, goPackage, getTemplateFuncMap(md), noformat) | ||||||
| } | ||||||
|
|
||||||
| func inlineReplace(tmplFile, outputFile string, md Metadata, start, end, goPackage string) error { | ||||||
|
|
@@ -447,7 +449,7 @@ func inlineReplace(tmplFile, outputFile string, md Metadata, start, end, goPacka | |||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| func generateFileWithFns(tmplFile, outputFile string, md Metadata, goPackage string, fns template.FuncMap) error { | ||||||
| func generateFileWithFns(tmplFile, outputFile string, md Metadata, goPackage string, fns template.FuncMap, noformat bool) error { | ||||||
| if err := os.Remove(outputFile); err != nil && !errors.Is(err, fs.ErrNotExist) { | ||||||
| return fmt.Errorf("unable to remove generated file %q: %w", outputFile, err) | ||||||
| } | ||||||
|
|
@@ -457,7 +459,7 @@ func generateFileWithFns(tmplFile, outputFile string, md Metadata, goPackage str | |||||
| return err | ||||||
| } | ||||||
| var formatErr error | ||||||
| if strings.HasSuffix(outputFile, ".go") { | ||||||
| if strings.HasSuffix(outputFile, ".go") && !noformat { | ||||||
| if formatted, err := format.Source(result); err == nil { | ||||||
| result = formatted | ||||||
| } else { | ||||||
|
|
@@ -532,7 +534,7 @@ func validateYAMLKeyOrder(raw []byte) error { | |||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| func generateConfigFiles(md Metadata, mdDir string) error { | ||||||
| func generateConfigFiles(md Metadata, mdDir string, noformat bool) error { | ||||||
| if md.Config != nil { | ||||||
| resolver := cfggen.NewResolver(md.PackageName, md.Status.Class, md.Type, mdDir) | ||||||
| resolvedSchema, err := resolver.ResolveSchema(md.Config) | ||||||
|
|
@@ -545,14 +547,14 @@ func generateConfigFiles(md Metadata, mdDir string) error { | |||||
| return fmt.Errorf("failed to write config schema: %w", err) | ||||||
| } | ||||||
|
|
||||||
| if err := generateConfigGoStruct(md, mdDir); err != nil { | ||||||
| if err := generateConfigGoStruct(md, mdDir, noformat); err != nil { | ||||||
| return fmt.Errorf("failed to generate config Go struct: %w", err) | ||||||
| } | ||||||
| } | ||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| func generateConfigGoStruct(md Metadata, outputDir string) error { | ||||||
| func generateConfigGoStruct(md Metadata, outputDir string, noformat bool) error { | ||||||
| rootPkg, err := helpers.RootPackage(outputDir) | ||||||
| if err != nil { | ||||||
| return fmt.Errorf("unable to determine root package: %w", err) | ||||||
|
|
@@ -563,5 +565,5 @@ func generateConfigGoStruct(md Metadata, outputDir string) error { | |||||
| dstFile := filepath.Join(outputDir, "generated_config.go") | ||||||
|
|
||||||
| fns := cfggen.WithCfgFns(getTemplateFuncMap(md), rootPkg, md.PackageName) | ||||||
| return generateFileWithFns(tmplFile, dstFile, md, packageName, fns) | ||||||
| return generateFileWithFns(tmplFile, dstFile, md, packageName, fns, noformat) | ||||||
| } | ||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will need to run this on CI.