From ab21055ca3151719057e06c56239bfa567336f31 Mon Sep 17 00:00:00 2001 From: Ilya Dmitrichenko Date: Wed, 24 Apr 2024 13:32:15 +0100 Subject: [PATCH] Revert "Factor out `errors.Decribe` and `runtimebuild.BuildFiles` from `cmd/timoni`" This reverts commit 08c01a865497005dccccf943be11a009f56be0ca. Signed-off-by: Ilya Dmitrichenko --- cmd/timoni/apply.go | 3 +- cmd/timoni/build.go | 3 +- cmd/timoni/bundle_apply.go | 13 ++----- cmd/timoni/bundle_build.go | 13 ++----- cmd/timoni/bundle_delete.go | 6 +-- cmd/timoni/bundle_status.go | 6 +-- cmd/timoni/bundle_vet.go | 11 ++---- cmd/timoni/errors.go | 13 +++++++ cmd/timoni/mod_show_config.go | 17 ++++----- cmd/timoni/mod_vet.go | 3 +- cmd/timoni/runtime_build.go | 44 +++++++++++++++++++--- internal/errors/errors.go | 29 --------------- internal/runtime/build/build.go | 66 --------------------------------- 13 files changed, 76 insertions(+), 151 deletions(-) create mode 100644 cmd/timoni/errors.go delete mode 100644 internal/errors/errors.go delete mode 100644 internal/runtime/build/build.go diff --git a/cmd/timoni/apply.go b/cmd/timoni/apply.go index 6fddb0c7..28a0274d 100644 --- a/cmd/timoni/apply.go +++ b/cmd/timoni/apply.go @@ -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" @@ -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) diff --git a/cmd/timoni/build.go b/cmd/timoni/build.go index 37caad50..bf41f6af 100644 --- a/cmd/timoni/build.go +++ b/cmd/timoni/build.go @@ -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" ) @@ -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) diff --git a/cmd/timoni/bundle_apply.go b/cmd/timoni/bundle_apply.go index 11d42088..ed8af239 100644 --- a/cmd/timoni/bundle_apply.go +++ b/cmd/timoni/bundle_apply.go @@ -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{ @@ -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 } @@ -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) @@ -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, diff --git a/cmd/timoni/bundle_build.go b/cmd/timoni/bundle_build.go index 68f13d40..7c8de4f8 100644 --- a/cmd/timoni/bundle_build.go +++ b/cmd/timoni/bundle_build.go @@ -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{ @@ -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 } @@ -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) @@ -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) diff --git a/cmd/timoni/bundle_delete.go b/cmd/timoni/bundle_delete.go index 58aef17e..58986d49 100644 --- a/cmd/timoni/bundle_delete.go +++ b/cmd/timoni/bundle_delete.go @@ -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{ @@ -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 } diff --git a/cmd/timoni/bundle_status.go b/cmd/timoni/bundle_status.go index 2332b75c..6d6eb8db 100644 --- a/cmd/timoni/bundle_status.go +++ b/cmd/timoni/bundle_status.go @@ -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" ) @@ -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 } diff --git a/cmd/timoni/bundle_vet.go b/cmd/timoni/bundle_vet.go index e8108ddd..15acb445 100644 --- a/cmd/timoni/bundle_vet.go +++ b/cmd/timoni/bundle_vet.go @@ -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{ @@ -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 } @@ -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) diff --git a/cmd/timoni/errors.go b/cmd/timoni/errors.go new file mode 100644 index 00000000..7b2dd3fc --- /dev/null +++ b/cmd/timoni/errors.go @@ -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, + })) +} diff --git a/cmd/timoni/mod_show_config.go b/cmd/timoni/mod_show_config.go index 3cc1116e..b32534e5 100644 --- a/cmd/timoni/mod_show_config.go +++ b/cmd/timoni/mod_show_config.go @@ -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" ) @@ -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"} @@ -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) } } @@ -169,10 +168,10 @@ 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() @@ -180,7 +179,7 @@ func writeFile(readFile string, header []string, rows [][]string, f fetcher.Fetc // 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() @@ -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 { @@ -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 diff --git a/cmd/timoni/mod_vet.go b/cmd/timoni/mod_vet.go index 13092b37..04fb240e 100644 --- a/cmd/timoni/mod_vet.go +++ b/cmd/timoni/mod_vet.go @@ -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" ) @@ -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) diff --git a/cmd/timoni/runtime_build.go b/cmd/timoni/runtime_build.go index 1a6a57a5..86810c7e 100644 --- a/cmd/timoni/runtime_build.go +++ b/cmd/timoni/runtime_build.go @@ -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{ @@ -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 } @@ -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 +} diff --git a/internal/errors/errors.go b/internal/errors/errors.go deleted file mode 100644 index d795f5e3..00000000 --- a/internal/errors/errors.go +++ /dev/null @@ -1,29 +0,0 @@ -/* -Copyright 2024 Stefan Prodan - -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 errors - -import ( - "fmt" - - "cuelang.org/go/cue/errors" -) - -func Describe(moduleRoot, description string, err error) error { - return fmt.Errorf("%s:\n%s", description, errors.Details(err, &errors.Config{ - Cwd: moduleRoot, - })) -} diff --git a/internal/runtime/build/build.go b/internal/runtime/build/build.go deleted file mode 100644 index fc84eb64..00000000 --- a/internal/runtime/build/build.go +++ /dev/null @@ -1,66 +0,0 @@ -/* -Copyright 2024 Stefan Prodan - -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 runtimebuild - -import ( - "os" - - "cuelang.org/go/cue/cuecontext" - "k8s.io/cli-runtime/pkg/genericclioptions" - - apiv1 "github.com/stefanprodan/timoni/api/v1alpha1" - "github.com/stefanprodan/timoni/internal/engine" - "github.com/stefanprodan/timoni/internal/errors" -) - -type Options struct { - KubeConfigFlags *genericclioptions.ConfigFlags -} - -func BuildFiles(opts Options, paths ...string) (*apiv1.Runtime, error) { - defaultRuntime := apiv1.DefaultRuntime(*opts.KubeConfigFlags.Context) - if len(paths) == 0 { - return defaultRuntime, nil - } - - tmpDir, err := os.MkdirTemp("", apiv1.FieldManager) - if err != nil { - return nil, err - } - defer os.RemoveAll(tmpDir) - - rb := engine.NewRuntimeBuilder(cuecontext.New(), paths) - - if err := rb.InitWorkspace(tmpDir); err != nil { - return nil, errors.Describe(tmpDir, "failed to init runtime", err) - } - - v, err := rb.Build() - if err != nil { - return nil, errors.Describe(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 -}