grafana-cli: Diff generated ts directly instead of relying on git (#45815)

* Add diffing support to grafana-cli cue gen-ts

* Rely on diff comparison in cuetsify pipeline step

* Ignore *.gen.ts files with eslint

* Chore: Fix lint `sdboyer/cuetsify-compare` (#45818)

* Sync drone

(cherry picked from commit 40645ab19e39ff9b0a12b7ebb13a4dc4c5e1d472)

* Fix lint

(cherry picked from commit c95ece983984432fea029335b2b729b09d76c7eb)

* Sign drone

Co-authored-by: Dimitris Sotirakis <sotirakis.dim@gmail.com>
This commit is contained in:
sam boyer
2022-02-24 06:03:07 -05:00
committed by GitHub
parent 8d57318941
commit 60db643983
6 changed files with 97 additions and 101 deletions

View File

@@ -257,6 +257,11 @@ so must be recompiled to validate newly-added CUE files.`,
Name: "grafana-root",
Usage: "path to the root of a Grafana repository in which to generate TypeScript from CUE files",
},
&cli.BoolFlag{
Name: "diff",
Usage: "diff results of codegen against files already on disk. Exits 1 if diff is non-empty",
Value: false,
},
},
},
}

View File

@@ -17,6 +17,7 @@ import (
"cuelang.org/go/cue/errors"
cload "cuelang.org/go/cue/load"
"cuelang.org/go/cue/parser"
"github.com/google/go-cmp/cmp"
"github.com/grafana/cuetsy"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
"github.com/grafana/grafana/pkg/schema/load"
@@ -63,6 +64,7 @@ var skipPaths = []string{
const prefix = "/"
//nolint: gocyclo
func (cmd Command) generateTypescript(c utils.CommandLine) error {
root := c.String("grafana-root")
if root == "" {
@@ -237,13 +239,46 @@ func (cmd Command) generateTypescript(c utils.CommandLine) error {
return gerrors.New(errors.Details(err, nil))
}
diff := c.Bool("diff")
var derr bool
for of, b := range outfiles {
err := os.WriteFile(filepath.Join(root, of), b, 0644)
if err != nil {
return err
p := filepath.Join(root, of)
if diff {
if _, err := os.Stat(p); err != nil {
if errors.Is(err, os.ErrNotExist) {
fmt.Printf("%s: no generated code file to compare against\n", p)
derr = true
continue
}
return fmt.Errorf("%s: %w", p, err)
}
f, err := os.Open(filepath.Clean(p))
if err != nil {
return fmt.Errorf("%s: %w", p, err)
}
ob, err := io.ReadAll(f)
if err != nil {
return err
}
dstr := cmp.Diff(string(ob), string(b))
if dstr != "" {
derr = true
fmt.Printf("%s would have changed:\n%s\n", p, dstr)
}
} else {
err := os.WriteFile(p, b, 0644)
if err != nil {
return err
}
}
}
if derr {
return errors.New("some files changed")
}
return nil
}
@@ -283,7 +318,7 @@ func toOverlay(prefix string, vfs fs.FS, overlay map[string]cload.Source) error
if !filepath.IsAbs(prefix) {
return fmt.Errorf("must provide absolute path prefix when generating cue overlay, got %q", prefix)
}
err := fs.WalkDir(vfs, ".", (func(path string, d fs.DirEntry, err error) error {
err := fs.WalkDir(vfs, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
@@ -296,6 +331,12 @@ func toOverlay(prefix string, vfs fs.FS, overlay map[string]cload.Source) error
if err != nil {
return err
}
defer func(f fs.File) {
err := f.Close()
if err != nil {
return
}
}(f)
b, err := io.ReadAll(f)
if err != nil {
@@ -304,7 +345,7 @@ func toOverlay(prefix string, vfs fs.FS, overlay map[string]cload.Source) error
overlay[filepath.Join(prefix, path)] = cload.FromBytes(b)
return nil
}))
})
if err != nil {
return err