Skip to content

Commit

Permalink
Revert "Factor out errors.Decribe and runtimebuild.BuildFiles fro…
Browse files Browse the repository at this point in the history
…m `cmd/timoni`"

This reverts commit 08c01a8.

Signed-off-by: Ilya Dmitrichenko <[email protected]>
  • Loading branch information
errordeveloper committed Jun 10, 2024
1 parent 03f24c5 commit ab21055
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 151 deletions.
3 changes: 1 addition & 2 deletions cmd/timoni/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"
"github.com/stefanprodan/timoni/internal/engine"
"github.com/stefanprodan/timoni/internal/engine/fetcher"
cueerrors "github.com/stefanprodan/timoni/internal/errors"
"github.com/stefanprodan/timoni/internal/flags"
"github.com/stefanprodan/timoni/internal/logger"
"github.com/stefanprodan/timoni/internal/reconciler"
Expand Down Expand Up @@ -220,7 +219,7 @@ func runApplyCmd(cmd *cobra.Command, args []string) error {

buildResult, err := builder.Build()
if err != nil {
return cueerrors.Describe(f.GetModuleRoot(), "build failed", err)
return describeErr(f.GetModuleRoot(), "build failed", err)
}

ctx, cancel := context.WithTimeout(cmd.Context(), rootArgs.timeout)
Expand Down
3 changes: 1 addition & 2 deletions cmd/timoni/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"
"github.com/stefanprodan/timoni/internal/engine"
"github.com/stefanprodan/timoni/internal/engine/fetcher"
cueerrors "github.com/stefanprodan/timoni/internal/errors"
"github.com/stefanprodan/timoni/internal/flags"
)

Expand Down Expand Up @@ -163,7 +162,7 @@ func runBuildCmd(cmd *cobra.Command, args []string) error {

buildResult, err := builder.Build()
if err != nil {
return cueerrors.Describe(f.GetModuleRoot(), "build failed", err)
return describeErr(f.GetModuleRoot(), "build failed", err)
}

apiVer, err := builder.GetAPIVersion(buildResult)
Expand Down
13 changes: 4 additions & 9 deletions cmd/timoni/bundle_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ import (
apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"
"github.com/stefanprodan/timoni/internal/engine"
"github.com/stefanprodan/timoni/internal/engine/fetcher"
cueerrors "github.com/stefanprodan/timoni/internal/errors"
"github.com/stefanprodan/timoni/internal/flags"
"github.com/stefanprodan/timoni/internal/logger"
"github.com/stefanprodan/timoni/internal/reconciler"
"github.com/stefanprodan/timoni/internal/runtime"
runtimebuild "github.com/stefanprodan/timoni/internal/runtime/build"
)

var bundleApplyCmd = &cobra.Command{
Expand Down Expand Up @@ -136,10 +134,7 @@ func runBundleApplyCmd(cmd *cobra.Command, _ []string) error {
maps.Copy(runtimeValues, engine.GetEnv())
}

runtimeBuildOpts := runtimebuild.Options{
KubeConfigFlags: kubeconfigArgs,
}
rt, err := runtimebuild.BuildFiles(runtimeBuildOpts, bundleArgs.runtimeFiles...)
rt, err := buildRuntime(bundleArgs.runtimeFiles)
if err != nil {
return err
}
Expand Down Expand Up @@ -182,12 +177,12 @@ func runBundleApplyCmd(cmd *cobra.Command, _ []string) error {
}

if err := bm.InitWorkspace(workspace, clusterValues); err != nil {
return cueerrors.Describe(workspace, "failed to parse bundle", err)
return describeErr(workspace, "failed to parse bundle", err)
}

v, err := bm.Build()
if err != nil {
return cueerrors.Describe(tmpDir, "failed to build bundle", err)
return describeErr(tmpDir, "failed to build bundle", err)
}

bundle, err := bm.GetBundle(v)
Expand Down Expand Up @@ -317,7 +312,7 @@ func applyBundleInstance(ctx context.Context, cuectx *cue.Context, instance *eng

buildResult, err := builder.Build()
if err != nil {
return cueerrors.Describe(modDir, "build failed for "+instance.Name, err)
return describeErr(modDir, "build failed for "+instance.Name, err)
}

r := reconciler.NewInteractiveReconciler(log,
Expand Down
13 changes: 4 additions & 9 deletions cmd/timoni/bundle_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ import (

apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"
"github.com/stefanprodan/timoni/internal/engine"
cueerrors "github.com/stefanprodan/timoni/internal/errors"
"github.com/stefanprodan/timoni/internal/flags"
"github.com/stefanprodan/timoni/internal/runtime"
runtimebuild "github.com/stefanprodan/timoni/internal/runtime/build"
)

var bundleBuildCmd = &cobra.Command{
Expand Down Expand Up @@ -112,10 +110,7 @@ func runBundleBuildCmd(cmd *cobra.Command, _ []string) error {
kctx, cancel := context.WithTimeout(cmd.Context(), rootArgs.timeout)
defer cancel()

runtimeBuildOpts := runtimebuild.Options{
KubeConfigFlags: kubeconfigArgs,
}
rt, err := runtimebuild.BuildFiles(runtimeBuildOpts, bundleArgs.runtimeFiles...)
rt, err := buildRuntime(bundleArgs.runtimeFiles)
if err != nil {
return err
}
Expand Down Expand Up @@ -147,12 +142,12 @@ func runBundleBuildCmd(cmd *cobra.Command, _ []string) error {
}

if err := bm.InitWorkspace(tmpDir, runtimeValues); err != nil {
return cueerrors.Describe(tmpDir, "failed to parse bundle", err)
return describeErr(tmpDir, "failed to parse bundle", err)
}

v, err := bm.Build()
if err != nil {
return cueerrors.Describe(tmpDir, "failed to build bundle", err)
return describeErr(tmpDir, "failed to build bundle", err)
}

bundle, err := bm.GetBundle(v)
Expand Down Expand Up @@ -222,7 +217,7 @@ func buildBundleInstance(cuectx *cue.Context, instance *engine.BundleInstance, r

buildResult, err := builder.Build()
if err != nil {
return "", cueerrors.Describe(modDir, "build failed for "+instance.Name, err)
return "", describeErr(modDir, "build failed for "+instance.Name, err)
}

bundleBuildSets, err := builder.GetApplySets(buildResult)
Expand Down
6 changes: 1 addition & 5 deletions cmd/timoni/bundle_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/stefanprodan/timoni/internal/engine"
"github.com/stefanprodan/timoni/internal/logger"
"github.com/stefanprodan/timoni/internal/runtime"
runtimebuild "github.com/stefanprodan/timoni/internal/runtime/build"
)

var bundleDelCmd = &cobra.Command{
Expand Down Expand Up @@ -95,10 +94,7 @@ func runBundleDelCmd(cmd *cobra.Command, args []string) error {
bundleDelArgs.name = args[0]
}

runtimeBuildOpts := runtimebuild.Options{
KubeConfigFlags: kubeconfigArgs,
}
rt, err := runtimebuild.BuildFiles(runtimeBuildOpts, bundleArgs.runtimeFiles...)
rt, err := buildRuntime(bundleArgs.runtimeFiles)
if err != nil {
return err
}
Expand Down
6 changes: 1 addition & 5 deletions cmd/timoni/bundle_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/stefanprodan/timoni/internal/engine"
"github.com/stefanprodan/timoni/internal/logger"
"github.com/stefanprodan/timoni/internal/runtime"
runtimebuild "github.com/stefanprodan/timoni/internal/runtime/build"

apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"
)
Expand Down Expand Up @@ -77,10 +76,7 @@ func runBundleStatusCmd(cmd *cobra.Command, args []string) error {
bundleStatusArgs.name = args[0]
}

runtimeBuildOpts := runtimebuild.Options{
KubeConfigFlags: kubeconfigArgs,
}
rt, err := runtimebuild.BuildFiles(runtimeBuildOpts, bundleArgs.runtimeFiles...)
rt, err := buildRuntime(bundleArgs.runtimeFiles)
if err != nil {
return err
}
Expand Down
11 changes: 3 additions & 8 deletions cmd/timoni/bundle_vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ import (

apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"
"github.com/stefanprodan/timoni/internal/engine"
cueerrors "github.com/stefanprodan/timoni/internal/errors"
"github.com/stefanprodan/timoni/internal/flags"
"github.com/stefanprodan/timoni/internal/runtime"
runtimebuild "github.com/stefanprodan/timoni/internal/runtime/build"
)

var bundleVetCmd = &cobra.Command{
Expand Down Expand Up @@ -115,10 +113,7 @@ func runBundleVetCmd(cmd *cobra.Command, args []string) error {
maps.Copy(runtimeValues, engine.GetEnv())
}

runtimeBuildOpts := runtimebuild.Options{
KubeConfigFlags: kubeconfigArgs,
}
rt, err := runtimebuild.BuildFiles(runtimeBuildOpts, bundleArgs.runtimeFiles...)
rt, err := buildRuntime(bundleArgs.runtimeFiles)
if err != nil {
return err
}
Expand Down Expand Up @@ -161,12 +156,12 @@ func runBundleVetCmd(cmd *cobra.Command, args []string) error {
}

if err := bm.InitWorkspace(workspace, clusterValues); err != nil {
return cueerrors.Describe(workspace, "failed to parse bundle", err)
return describeErr(workspace, "failed to parse bundle", err)
}

v, err := bm.Build()
if err != nil {
return cueerrors.Describe(workspace, "failed to build bundle", err)
return describeErr(workspace, "failed to build bundle", err)
}

bundle, err := bm.GetBundle(v)
Expand Down
13 changes: 13 additions & 0 deletions cmd/timoni/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"fmt"

"cuelang.org/go/cue/errors"
)

func describeErr(moduleRoot, description string, err error) error {
return fmt.Errorf("%s:\n%s", description, errors.Details(err, &errors.Config{
Cwd: moduleRoot,
}))
}
17 changes: 8 additions & 9 deletions cmd/timoni/mod_show_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"
"github.com/stefanprodan/timoni/internal/engine"
"github.com/stefanprodan/timoni/internal/engine/fetcher"
cueerrors "github.com/stefanprodan/timoni/internal/errors"
"github.com/stefanprodan/timoni/internal/flags"
)

Expand Down Expand Up @@ -127,12 +126,12 @@ func runConfigShowModCmd(cmd *cobra.Command, args []string) error {

buildResult, err := builder.Build()
if err != nil {
return cueerrors.Describe(f.GetModuleRoot(), "validation failed", err)
return describeErr(f.GetModuleRoot(), "validation failed", err)
}

rows, err := builder.GetConfigDoc(buildResult)
if err != nil {
return cueerrors.Describe(f.GetModuleRoot(), "failed to get config structure", err)
return describeErr(f.GetModuleRoot(), "failed to get config structure", err)
}

header := []string{"Key", "Type", "Default", "Description"}
Expand All @@ -147,7 +146,7 @@ func runConfigShowModCmd(cmd *cobra.Command, args []string) error {

err = os.Rename(tmpFile, configShowModArgs.output)
if err != nil {
return cueerrors.Describe(f.GetModuleRoot(), "Unable to rename file", err)
return describeErr(f.GetModuleRoot(), "Unable to rename file", err)
}
}

Expand All @@ -169,18 +168,18 @@ func writeFile(readFile string, header []string, rows [][]string, f fetcher.Fetc
inputFile, err = os.Create(readFile)

if err != nil {
return "", cueerrors.Describe(f.GetModuleRoot(), "Unable to create the temporary output file", err)
return "", describeErr(f.GetModuleRoot(), "Unable to create the temporary output file", err)
}
} else {
return "", cueerrors.Describe(f.GetModuleRoot(), "Unable to create the temporary output file", err)
return "", describeErr(f.GetModuleRoot(), "Unable to create the temporary output file", err)
}
}
defer inputFile.Close()

// open the output file
outputFile, err := os.Create(tmpFileName)
if err != nil {
return "", cueerrors.Describe(f.GetModuleRoot(), "Unable to create the temporary output file", err)
return "", describeErr(f.GetModuleRoot(), "Unable to create the temporary output file", err)
}
defer outputFile.Close()

Expand All @@ -201,7 +200,7 @@ func writeFile(readFile string, header []string, rows [][]string, f fetcher.Fetc

matched, err := regexp.MatchString(`^\|.*\|$`, line)
if err != nil {
return "", cueerrors.Describe(f.GetModuleRoot(), "Regex Match for table content failed", err)
return "", describeErr(f.GetModuleRoot(), "Regex Match for table content failed", err)
}

if configSection && !foundTable && matched {
Expand All @@ -225,7 +224,7 @@ func writeFile(readFile string, header []string, rows [][]string, f fetcher.Fetc

err = outputWriter.Flush()
if err != nil {
return "", cueerrors.Describe(f.GetModuleRoot(), "Failed to Flush Writer", err)
return "", describeErr(f.GetModuleRoot(), "Failed to Flush Writer", err)
}

return tmpFileName, nil
Expand Down
3 changes: 1 addition & 2 deletions cmd/timoni/mod_vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"
"github.com/stefanprodan/timoni/internal/engine"
"github.com/stefanprodan/timoni/internal/engine/fetcher"
cueerrors "github.com/stefanprodan/timoni/internal/errors"
"github.com/stefanprodan/timoni/internal/flags"
"github.com/stefanprodan/timoni/internal/logger"
)
Expand Down Expand Up @@ -158,7 +157,7 @@ func runVetModCmd(cmd *cobra.Command, args []string) error {

buildResult, err := builder.Build(tags...)
if err != nil {
return cueerrors.Describe(f.GetModuleRoot(), "validation failed", err)
return describeErr(f.GetModuleRoot(), "validation failed", err)
}

applySets, err := builder.GetApplySets(buildResult)
Expand Down
44 changes: 39 additions & 5 deletions cmd/timoni/runtime_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import (
"os"
"sort"

"cuelang.org/go/cue/cuecontext"
"github.com/spf13/cobra"

apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"
"github.com/stefanprodan/timoni/internal/engine"
"github.com/stefanprodan/timoni/internal/logger"
"github.com/stefanprodan/timoni/internal/runtime"
runtimebuild "github.com/stefanprodan/timoni/internal/runtime/build"
)

var runtimeBuildCmd = &cobra.Command{
Expand Down Expand Up @@ -78,10 +80,7 @@ func runRuntimeBuildCmd(cmd *cobra.Command, args []string) error {
defer os.Remove(stdinFile)
}

runtimeBuildOpts := runtimebuild.Options{
KubeConfigFlags: kubeconfigArgs,
}
rt, err := runtimebuild.BuildFiles(runtimeBuildOpts, files...)
rt, err := buildRuntime(files)
if err != nil {
return err
}
Expand Down Expand Up @@ -128,3 +127,38 @@ func runRuntimeBuildCmd(cmd *cobra.Command, args []string) error {

return nil
}

func buildRuntime(files []string) (*apiv1.Runtime, error) {
defaultRuntime := apiv1.DefaultRuntime(*kubeconfigArgs.Context)
if len(files) == 0 {
return defaultRuntime, nil
}

tmpDir, err := os.MkdirTemp("", apiv1.FieldManager)
if err != nil {
return nil, err
}
defer os.RemoveAll(tmpDir)

ctx := cuecontext.New()
rb := engine.NewRuntimeBuilder(ctx, files)

if err := rb.InitWorkspace(tmpDir); err != nil {
return nil, describeErr(tmpDir, "failed to init runtime", err)
}

v, err := rb.Build()
if err != nil {
return nil, describeErr(tmpDir, "failed to parse runtime", err)
}

rt, err := rb.GetRuntime(v)
if err != nil {
return nil, err
}

if len(rt.Clusters) == 0 {
rt.Clusters = defaultRuntime.Clusters
}
return rt, nil
}
Loading

0 comments on commit ab21055

Please sign in to comment.