Skip to content

Commit

Permalink
cue.Value Syntax(Final()) for showing effective configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonbloom committed Feb 16, 2022
1 parent 7a34e0f commit ea17535
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 44 deletions.
5 changes: 2 additions & 3 deletions internal/cli/stack_show.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package cli

import (
"fmt"

"github.com/deref/exo/internal/util/cmdutil"
"github.com/spf13/cobra"
)

Expand All @@ -23,7 +22,7 @@ var stackShowCmd = &cobra.Command{
} `graphql:"stackByRef(ref: $currentStack)"`
}
mustQueryStack(ctx, &q, nil)
fmt.Println(q.Stack.Configuration)
cmdutil.Show(q.Stack.Configuration)
return nil
},
}
6 changes: 3 additions & 3 deletions internal/manifest/exocue/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func newAnd(xs ...ast.Expr) ast.Expr {
return ast.NewBinExpr(token.AND, xs...)
}

func (b *Builder) Build() *Stack {
func (b *Builder) Build() Stack {
cc := cuecontext.New()
cfg := cc.BuildExpr(declsToStruct(b.decls)).Eval()
return NewStack(cfg.LookupPath(cue.MakePath(cue.Str("$stack"))))
cfg := cc.BuildExpr(declsToStruct(b.decls))
return Stack(cfg.LookupPath(cue.MakePath(cue.Str("$stack"))))
}
33 changes: 19 additions & 14 deletions internal/manifest/exocue/config.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package exocue

import "cuelang.org/go/cue"
import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
)

type Stack struct {
v cue.Value
func Final(v cue.Value) ast.Node {
return StructToFile(v.Syntax(cue.Final()))
}

func NewStack(v cue.Value) *Stack {
dumpValue(v)
return &Stack{
v: v,
}
}
type Stack cue.Value

func (s *Stack) Eval() cue.Value {
return s.v.Eval()
func (s Stack) Final() ast.Node {
return Final(cue.Value(s))
}

func (s *Stack) Component(name string) cue.Value {
func (s Stack) Component(name string) Component {
v := cue.Value(s)
componentPath := cue.MakePath(cue.Str("components"), cue.Str(name))
res := s.v.LookupPath(componentPath)
return res
res := v.LookupPath(componentPath)
return Component(res)
}

type Component cue.Value

func (c Component) Final() ast.Node {
return Final(cue.Value(c))
}
5 changes: 3 additions & 2 deletions internal/manifest/exocue/exocue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package exocue
import (
"testing"

"cuelang.org/go/cue"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -33,7 +34,7 @@ func TestManifestToComponent(t *testing.T) {
`)
cfg := b.Build()
spec := cfg.Component("backend")
assert.NoError(t, spec.Err())
assert.NoError(t, cue.Value(spec).Err())
type Spec struct {
Program string
Arguments []string
Expand All @@ -45,7 +46,7 @@ func TestManifestToComponent(t *testing.T) {
Spec Spec
}
var actual Component
assert.NoError(t, spec.Decode(&actual))
assert.NoError(t, cue.Value(spec).Decode(&actual))
assert.Equal(t, Component{
Name: "backend",
Type: "daemon",
Expand Down
32 changes: 19 additions & 13 deletions internal/manifest/exocue/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,31 @@ func FormatString(v interface{}) (string, error) {
return string(bs), err
}

func StructToFile(v cue.Value) *ast.File {
func MustFormatString(v interface{}) string {
s, err := FormatString(v)
if err != nil {
panic(err)
}
return s
}

func Dump(v interface{}) {
fmt.Println(MustFormatString(v))
}

func StructToFile(v interface{}) *ast.File {
var decls []ast.Decl
switch x := v.Syntax().(type) {
switch v := v.(type) {
case cue.Value:
return StructToFile(v.Syntax())
case *ast.StructLit:
decls = x.Elts
decls = v.Elts
case *ast.BottomLit:
decls = []ast.Decl{x}
decls = []ast.Decl{v}
default:
panic(fmt.Errorf("cannot convert %T to file", x))
panic(fmt.Errorf("cannot convert %T to file", v))
}
return &ast.File{
Decls: decls,
}
}

func dumpValue(v cue.Value) {
bs, err := FormatBytes(v)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", bs)
}
9 changes: 4 additions & 5 deletions internal/resolvers/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"net/http"

"cuelang.org/go/cue"
"github.com/deref/exo/internal/gensym"
"github.com/deref/exo/internal/manifest/exocue"
. "github.com/deref/exo/internal/scalars"
Expand Down Expand Up @@ -284,17 +283,17 @@ func (r *ComponentResolver) Configuration(ctx context.Context) (string, error) {
if err != nil {
return "", err
}
return exocue.FormatString(exocue.StructToFile(cfg))
return exocue.FormatString(cfg.Final())
}

func (r *ComponentResolver) configuration(ctx context.Context) (cue.Value, error) {
func (r *ComponentResolver) configuration(ctx context.Context) (exocue.Component, error) {
stack, err := r.Stack(ctx)
if err != nil {
return cue.Value{}, fmt.Errorf("resolving stack: %w", err)
return exocue.Component{}, fmt.Errorf("resolving stack: %w", err)
}
cfg, err := stack.configuration(ctx)
if err != nil {
return cue.Value{}, err
return exocue.Component{}, err
}
return cfg.Component(r.Name), nil
}
4 changes: 3 additions & 1 deletion internal/resolvers/reconciliation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"

"cuelang.org/go/cue"
)

type ReconciliationResolver struct {
Expand Down Expand Up @@ -32,7 +34,7 @@ func (r *MutationResolver) ReconcileComponent(ctx context.Context, args struct {

// XXX check disposal state!

rendered, err := ctrl.Render(ctx, cfg)
rendered, err := ctrl.Render(ctx, cue.Value(cfg))
if err != nil {
return nil, fmt.Errorf("rendering: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/resolvers/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,15 @@ func (r *StackResolver) Configuration(ctx context.Context) (string, error) {
if err != nil {
return "", err
}
return exocue.FormatString(cfg.Eval())
return exocue.FormatString(cfg.Final())
}

// TODO: It might be valuable to cache this for multiple
// ComponentResolver.evalSpec calls.
func (r *StackResolver) configuration(ctx context.Context) (*exocue.Stack, error) {
func (r *StackResolver) configuration(ctx context.Context) (exocue.Stack, error) {
components, err := r.components(ctx)
if err != nil {
return nil, fmt.Errorf("resolving components: %w", err)
return exocue.Stack{}, fmt.Errorf("resolving components: %w", err)
}
b := exocue.NewBuilder()
for _, component := range components {
Expand Down

0 comments on commit ea17535

Please sign in to comment.