[Chore] update to cue v0.4.0 (#39641)

* change global flag to flagset

* update pr with comments

* replace flag.args by flagset

* fix build

* migrate the schema package to use cue 4.0

* fix the load package
This commit is contained in:
ying-jeanne
2021-09-28 17:09:09 +02:00
committed by GitHub
parent 05bb451a1d
commit 580cdc46fc
10 changed files with 85 additions and 74 deletions

View File

@@ -39,27 +39,32 @@ type exitWithCode struct {
code int
}
var serverFs = flag.NewFlagSet("server", flag.ContinueOnError)
func (e exitWithCode) Error() string {
return e.reason
}
func RunServer(opt ServerOptions) int {
var (
configFile = flag.String("config", "", "path to config file")
homePath = flag.String("homepath", "", "path to grafana install/home path, defaults to working directory")
pidFile = flag.String("pidfile", "", "path to pid file")
packaging = flag.String("packaging", "unknown", "describes the way Grafana was installed")
configFile = serverFs.String("config", "", "path to config file")
homePath = serverFs.String("homepath", "", "path to grafana install/home path, defaults to working directory")
pidFile = serverFs.String("pidfile", "", "path to pid file")
packaging = serverFs.String("packaging", "unknown", "describes the way Grafana was installed")
v = flag.Bool("v", false, "prints current version and exits")
vv = flag.Bool("vv", false, "prints current version, all dependencies and exits")
profile = flag.Bool("profile", false, "Turn on pprof profiling")
profileAddr = flag.String("profile-addr", "localhost", "Define custom address for profiling")
profilePort = flag.Uint64("profile-port", 6060, "Define custom port for profiling")
tracing = flag.Bool("tracing", false, "Turn on tracing")
tracingFile = flag.String("tracing-file", "trace.out", "Define tracing output file")
v = serverFs.Bool("v", false, "prints current version and exits")
vv = serverFs.Bool("vv", false, "prints current version, all dependencies and exits")
profile = serverFs.Bool("profile", false, "Turn on pprof profiling")
profileAddr = serverFs.String("profile-addr", "localhost", "Define custom address for profiling")
profilePort = serverFs.Uint64("profile-port", 6060, "Define custom port for profiling")
tracing = serverFs.Bool("tracing", false, "Turn on tracing")
tracingFile = serverFs.String("tracing-file", "trace.out", "Define tracing output file")
)
flag.Parse()
if err := serverFs.Parse(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return 1
}
if *v || *vv {
fmt.Printf("Version %s (commit: %s, branch: %s)\n", opt.Version, opt.Commit, opt.BuildBranch)
@@ -161,7 +166,7 @@ func executeServer(configFile, homePath, pidFile, packaging string, traceDiagnos
}
s, err := server.Initialize(setting.CommandLineArgs{
Config: configFile, HomePath: homePath, Args: flag.Args(),
Config: configFile, HomePath: homePath, Args: serverFs.Args(),
}, server.Options{
PidFile: pidFile, Version: opt.Version, Commit: opt.Commit, BuildBranch: opt.BuildBranch,
}, api.ServerOptions{})

View File

@@ -6,12 +6,12 @@ import (
"io/fs"
"path/filepath"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/load"
"github.com/grafana/grafana"
)
var rt = &cue.Runtime{}
var ctx = cuecontext.New()
// Families can have variants, where more typing information narrows the
// possible values for certain keys in schemas. These are a meta-property

View File

@@ -56,15 +56,15 @@ func baseDashboardFamily(p BaseLoadPaths) (cue.Value, error) {
Module: "github.com/grafana/grafana",
Dir: filepath.Join(prefix, dashboardDir),
}
inst, err := rt.Build(load.Instances(nil, cfg)[0])
if err != nil {
cueError := schema.WrapCUEError(err)
if err != nil {
inst := ctx.BuildInstance(load.Instances(nil, cfg)[0])
if inst.Err() != nil {
cueError := schema.WrapCUEError(inst.Err())
if inst.Err() != nil {
return cue.Value{}, cueError
}
}
famval := inst.Value().LookupPath(cue.MakePath(cue.Str("Family")))
famval := inst.LookupPath(cue.MakePath(cue.Str("Family")))
if !famval.Exists() {
return cue.Value{}, errors.New("dashboard schema family did not exist at expected path in expected file")
}
@@ -148,11 +148,11 @@ func (cds *compositeDashboardSchema) Validate(r schema.Resource) error {
if name == "" {
name = "resource"
}
rv, err := rt.Compile(name, r.Value)
if err != nil {
return err
rv := ctx.CompileString(r.Value.(string), cue.Filename(name))
if rv.Err() != nil {
return rv.Err()
}
return cds.actual.Unify(rv.Value()).Validate(cue.Concrete(true))
return cds.actual.Unify(rv).Validate(cue.Concrete(true))
}
// CUE returns the cue.Value representing the actual schema.

View File

@@ -12,11 +12,11 @@ import (
// definitions on which all Grafana scuemata rely.
//
// TODO probably cache this or something
func getBaseScuemata(p BaseLoadPaths) (*cue.Instance, error) {
func getBaseScuemata(p BaseLoadPaths) (cue.Value, error) {
overlay := make(map[string]load.Source)
if err := toOverlay(filepath.Join(prefix, "grafana"), p.BaseCueFS, overlay); err != nil {
return nil, err
return cue.Value{}, err
}
cfg := &load.Config{
@@ -37,10 +37,12 @@ func getBaseScuemata(p BaseLoadPaths) (*cue.Instance, error) {
// this "/".
Dir: prefix,
}
return rt.Build(load.Instances([]string{
res := ctx.BuildInstance(load.Instances([]string{
filepath.Join(prefix, "grafana", "cue", "scuemata", "scuemata.cue"),
filepath.Join(prefix, "grafana", "cue", "scuemata", "panel-plugin.cue"),
}, cfg)[0])
return res, res.Err()
}
func buildGenericScuemata(famval cue.Value) (schema.VersionedCueSchema, error) {
@@ -106,11 +108,11 @@ func (gvs *genericVersionedSchema) Validate(r schema.Resource) error {
if name == "" {
name = "resource"
}
rv, err := rt.Compile(name, r.Value)
if err != nil {
return err
rv := ctx.CompileString(r.Value.(string), cue.Filename(name))
if rv.Err() != nil {
return rv.Err()
}
return gvs.actual.Unify(rv.Value()).Validate(cue.Concrete(true))
return gvs.actual.Unify(rv).Validate(cue.Concrete(true))
}
// CUE returns the cue.Value representing the actual schema.

View File

@@ -91,7 +91,7 @@ func TestDevenvDashboardValidity(t *testing.T) {
}
t.Run(filepath.Base(path), func(t *testing.T) {
err := sch.Validate(schema.Resource{Value: byt, Name: path})
err := sch.Validate(schema.Resource{Value: string(byt), Name: path})
if err != nil {
// Testify trims errors to short length. We want the full text
errstr := errors.Details(err, nil)
@@ -191,16 +191,16 @@ func TestAllPluginsInDist(t *testing.T) {
Dir: filepath.Join(prefix, dashboardDir, "dist"),
Package: "dist",
}
inst, err := rt.Build(load.Instances(nil, cfg)[0])
require.NoError(t, err)
inst := ctx.BuildInstance(load.Instances(nil, cfg)[0])
require.NoError(t, inst.Err())
dinst, err := rt.Compile("str", `
dinst := ctx.CompileString(`
Family: compose: Panel: {}
typs: [for typ, _ in Family.compose.Panel {typ}]
`)
require.NoError(t, err)
`, cue.Filename("str"))
require.NoError(t, dinst.Err())
typs := dinst.Value().Unify(inst.Value()).LookupPath(cue.MakePath(cue.Str("typs")))
typs := dinst.Unify(inst).LookupPath(cue.MakePath(cue.Str("typs")))
j, err := cuejson.Marshal(typs)
require.NoError(t, err)

View File

@@ -20,7 +20,7 @@ import (
func mapPanelModel(id string, vcs schema.VersionedCueSchema) cue.Value {
maj, min := vcs.Version()
// Ignore err return, this can't fail to compile
inter, _ := rt.Compile(fmt.Sprintf("%s-glue-panelComposition", id), fmt.Sprintf(`
inter := ctx.CompileString(fmt.Sprintf(`
in: {
type: %q
v: {
@@ -39,10 +39,10 @@ func mapPanelModel(id string, vcs schema.VersionedCueSchema) cue.Value {
fieldConfig: defaults: custom: in.model.PanelFieldConfig
}
}
`, id, maj, min))
`, id, maj, min), cue.Filename(fmt.Sprintf("%s-glue-panelComposition", id)))
// TODO validate, especially with #PanelModel
return inter.Value().FillPath(cue.MakePath(cue.Str("in"), cue.Str("model")), vcs.CUE()).LookupPath(cue.MakePath(cue.Str(("result"))))
return inter.FillPath(cue.MakePath(cue.Str("in"), cue.Str("model")), vcs.CUE()).LookupPath(cue.MakePath(cue.Str(("result"))))
}
func loadPanelScuemata(p BaseLoadPaths) (map[string]cue.Value, error) {
@@ -60,7 +60,7 @@ func loadPanelScuemata(p BaseLoadPaths) (map[string]cue.Value, error) {
return nil, err
}
pmf := base.Value().LookupPath(cue.MakePath(cue.Def("#PanelFamily")))
pmf := base.LookupPath(cue.MakePath(cue.Def("#PanelFamily")))
if !pmf.Exists() {
return nil, errors.New("could not locate #PanelFamily definition")
}
@@ -108,13 +108,13 @@ func loadPanelScuemata(p BaseLoadPaths) (map[string]cue.Value, error) {
}
li := load.Instances([]string{filepath.Join("/", dpath, "models.cue")}, cfg)
imod, err := rt.Build(li[0])
if err != nil {
return err
imod := ctx.BuildInstance(li[0])
if imod.Err() != nil {
return imod.Err()
}
// Get the Family declaration in the models.cue file...
pmod := imod.Value().LookupPath(cue.MakePath(cue.Str("Panel")))
pmod := imod.LookupPath(cue.MakePath(cue.Str("Panel")))
if !pmod.Exists() {
return fmt.Errorf("%s does not contain a declaration of its models at path 'Family'", path)
}

View File

@@ -8,11 +8,12 @@ import (
"strings"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
errs "cuelang.org/go/cue/errors"
cuejson "cuelang.org/go/pkg/encoding/json"
)
var rt = &cue.Runtime{}
var ctx = cuecontext.New()
// CueError wraps Errors caused by malformed cue files.
type CueError struct {
@@ -281,11 +282,11 @@ func ApplyDefaults(r Resource, scue cue.Value) (Resource, error) {
if name == "" {
name = "resource"
}
rv, err := rt.Compile(name, r.Value)
if err != nil {
return r, err
rv := ctx.CompileString(r.Value.(string), cue.Filename(name))
if rv.Err() != nil {
return r, rv.Err()
}
rvUnified := rv.Value().Unify(scue)
rvUnified := rv.Unify(scue)
re, err := convertCUEValueToString(rvUnified)
if err != nil {
return r, err
@@ -314,11 +315,11 @@ func TrimDefaults(r Resource, scue cue.Value) (Resource, error) {
if name == "" {
name = "resource"
}
rvInstance, err := rt.Compile(name, r.Value)
if err != nil {
return r, err
rvInstance := ctx.CompileString(r.Value.(string), cue.Filename(name))
if rvInstance.Err() != nil {
return r, rvInstance.Err()
}
rv, _, err := removeDefaultHelper(scue, rvInstance.Value())
rv, _, err := removeDefaultHelper(scue, rvInstance)
if err != nil {
return r, err
}
@@ -337,9 +338,9 @@ func isCueValueEqual(inputdef cue.Value, input cue.Value) bool {
func removeDefaultHelper(inputdef cue.Value, input cue.Value) (cue.Value, bool, error) {
// To include all optional fields, we need to use inputdef for iteration,
// since the lookuppath with optional field doesn't work very well
rvInstance, err := rt.Compile("helper", []byte{})
if err != nil {
return input, false, err
rvInstance := ctx.CompileString("", cue.Filename("helper"))
if rvInstance.Err() != nil {
return input, false, rvInstance.Err()
}
rv := rvInstance.Value()
@@ -404,11 +405,11 @@ func removeDefaultHelper(inputdef cue.Value, input cue.Value) (cue.Value, bool,
}
}
iterlistContent := fmt.Sprintf("[%s]", strings.Join(iterlist, ","))
liInstance, err := rt.Compile("resource", []byte(iterlistContent))
if err != nil {
liInstance := ctx.CompileString(iterlistContent, cue.Filename("resource"))
if liInstance.Err() != nil {
return rv, false, err
}
return liInstance.Value(), false, nil
return liInstance, false, nil
default:
if isCueValueEqual(inputdef, input) {
return input, true, nil

View File

@@ -30,13 +30,12 @@ func TestGenerate(t *testing.T) {
for _, c := range cases {
t.Run(c.Name+" apply default value", func(t *testing.T) {
var r cue.Runtime
scmInstance, err := r.Compile(c.Name+".cue", c.CUE)
if err != nil {
t.Fatal(err)
scmInstance := ctx.CompileString(c.CUE, cue.Filename(c.Name+".cue"))
if scmInstance.Err() != nil {
t.Fatal(scmInstance.Err())
}
inputResource := Resource{Value: c.Trimmed}
out, err := ApplyDefaults(inputResource, scmInstance.Value())
out, err := ApplyDefaults(inputResource, scmInstance)
if err != nil {
t.Fatal(err)
}
@@ -50,13 +49,12 @@ func TestGenerate(t *testing.T) {
for _, c := range cases {
t.Run(c.Name+" trim default value", func(t *testing.T) {
var r cue.Runtime
scmInstance, err := r.Compile(c.Name+".cue", c.CUE)
if err != nil {
t.Fatal(err)
scmInstance := ctx.CompileString(c.CUE, cue.Filename(c.Name+".cue"))
if scmInstance.Err() != nil {
t.Fatal(scmInstance.Err())
}
inputResource := Resource{Value: c.Full}
out, err := TrimDefaults(inputResource, scmInstance.Value())
out, err := TrimDefaults(inputResource, scmInstance)
if err != nil {
t.Fatal(err)
}