Skip to content
This repository was archived by the owner on Jan 4, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cmd/cuebe/cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ cuebe apply --dry-run .
}

func runApply(cmd *cobra.Command, args []string) {
mfs, build, err := manifetsFrom(cmd)
mfs, build, err := manifestFrom(cmd)
cobra.CheckErr(err)

// group by Instances
Expand Down Expand Up @@ -111,7 +111,7 @@ func getK8sConfig(context string) (*utils.K8sConfig, error) {
}

// TODO move that in its own package
func manifetsFrom(cmd *cobra.Command) ([]manifest.Manifest, cue.Value, error) {
func manifestFrom(cmd *cobra.Command) ([]manifest.Manifest, cue.Value, error) {
opts := factory.GetBuildOpt(cmd)

// build
Expand All @@ -123,6 +123,11 @@ func manifetsFrom(cmd *cobra.Command) ([]manifest.Manifest, cue.Value, error) {
return nil, cue.Value{}, fmt.Errorf("could not build context: %w", err)
}

// Before trying to extract the manifests. We must ensure all values are concrete
if err = v.Validate(cue.Concrete(true)); err != nil {
return nil, v, fmt.Errorf("checking for concrete value and field definitions: %w", err)
}

// parse paths
paths := make([]cue.Path, 0, len(opts.Expressions))
for _, e := range opts.Expressions {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cuebe/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ cuebe apply -c colima .
}

func runDelete(cmd *cobra.Command, args []string) {
mfs, build, err := manifetsFrom(cmd)
mfs, build, err := manifestFrom(cmd)
cobra.CheckErr(err)

// group by Instances
Expand Down
89 changes: 89 additions & 0 deletions cmd/cuebe/cmd/eval.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright © 2021 Loft Orbital

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/load"
"fmt"
"github.com/loft-orbital/cuebe/cmd/cuebe/factory"
"github.com/loft-orbital/cuebe/pkg/build"
"github.com/spf13/cobra"
)

func newEvalCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "eval",
SuggestFor: []string{"render", "template"},
Short: "Equivalent of cue eval command.",
Long: `
Will evaluate the cue definitions without requiring concrete values
the same way 'cue eval' would do.
`,
Example: `
# Export current directory
cuebe eval .
`,
Run: runEval,
}

factory.BuildAware(cmd)
factory.BuildContextAware(cmd)

return cmd
}

// TODO: Rely on Cobra err catching system
func runEval(cmd *cobra.Command, args []string) {
// Build opts, shall return a BuildOpt{} struct
opts := factory.GetBuildOpt(cmd)

// build the cuebe context, and return a cue.Value with the unified cue structure
v, err := build.Build(factory.GetBuildContext(cmd), &load.Config{
Tags: opts.Tags,
TagVars: load.DefaultTagVars(),
})
if err != nil {
cobra.CheckErr(fmt.Errorf("could not build context: %w", err))
}

// // test validate
// // Add concrete flag here
// if err = v.Validate(cue.Concrete(true)); err != nil {
// cobra.CheckErr(fmt.Errorf("validation error: %v", err))
// }

// Parse paths expressions (-e argument)
paths := make([]cue.Path, 0, len(opts.Expressions))
for _, e := range opts.Expressions {
p := cue.ParsePath(e)
if p.Err() != nil {
cobra.CheckErr(fmt.Errorf("failed to parse expression %s: %w", e, p.Err()))
}
paths = append(paths, p)
}

// Output the cue evaluation WITHOUT concrete values
// If we have no paths, we dump the whole unified values
if len(paths) == 0 {
fmt.Printf("%v", v)
} else {
for _, p := range paths {
node := v.LookupPath(p)
fmt.Printf("%v", node)
}
}
}
2 changes: 1 addition & 1 deletion cmd/cuebe/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ cuebe export -i main.enc.yaml
}

func runExport(cmd *cobra.Command, args []string) {
mfs, _, err := manifetsFrom(cmd)
mfs, _, err := manifestFrom(cmd)
cobra.CheckErr(err)

// render
Expand Down
1 change: 1 addition & 0 deletions cmd/cuebe/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func init() {
newApplyCmd(),
newDeleteCmd(),
newExportCmd(),
newEvalCmd(),
newInstallCmd(),
newPackCmd(),
newVersionCmd(),
Expand Down