From 201d43464805ffe601bed027005488be3a0af8b6 Mon Sep 17 00:00:00 2001 From: sahil-lakhwani Date: Sun, 28 Nov 2021 12:48:34 +0530 Subject: [PATCH] condtionally evaluate template while creating config Signed-off-by: sahil-lakhwani --- cmd/cli/main.go | 10 ++++------ pkg/apps/apps.go | 2 +- pkg/config/config.go | 17 ++++++++++++++++- pkg/registry/registry.go | 4 ++-- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 4e95795..f0188a4 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -24,7 +24,6 @@ import ( "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/viper" - "gopkg.in/yaml.v2" "github.com/kbrew-dev/kbrew/pkg/apps" "github.com/kbrew-dev/kbrew/pkg/config" @@ -187,11 +186,10 @@ var ( return err } - bytes, err := yaml.Marshal(appArgs) - if err != nil { - return err + for k, v := range appArgs { + fmt.Println(k, ":", v) } - fmt.Println(string(bytes)) + return nil }, } @@ -250,7 +248,7 @@ func manageApp(m apps.Method, args []string) error { } logger := log.NewLogger(debug) runner := apps.NewAppRunner(m, logger, log.NewStatus(logger)) - c, err := config.NewApp(strings.ToLower(a), configFile) + c, err := config.NewApp(strings.ToLower(a), configFile, true) if err != nil { return err } diff --git a/pkg/apps/apps.go b/pkg/apps/apps.go index 96e3cc3..8bf1f10 100644 --- a/pkg/apps/apps.go +++ b/pkg/apps/apps.go @@ -65,7 +65,7 @@ func NewAppRunner(op Method, log *log.Logger, status *log.Status) *AppRunner { // Run fetches recipe from registry for the app and performs given operation func (r *AppRunner) Run(ctx context.Context, appName, namespace, appConfigPath string) error { - c, err := config.NewApp(appName, appConfigPath) + c, err := config.NewApp(appName, appConfigPath, true) if err != nil { return err } diff --git a/pkg/config/config.go b/pkg/config/config.go index 056884c..6762460 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -105,7 +105,7 @@ type AppCleanup struct { } // NewApp parses kbrew recipe configuration and returns AppConfig instance -func NewApp(name, path string) (*AppConfig, error) { +func NewApp(name, path string, evaluateTemplate bool) (*AppConfig, error) { c := &AppConfig{} configFile, err := os.Open(path) defer func() { @@ -124,6 +124,20 @@ func NewApp(name, path string) (*AppConfig, error) { return nil, err } + // return the yaml without evaluating templates + if !evaluateTemplate { + if len(b) != 0 { + err = yaml.Unmarshal(b, c) + if err != nil { + return nil, err + } + } + + c.App.Name = name + return c, nil + } + + // client needed to evaluate templates k8sconfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{}, @@ -144,6 +158,7 @@ func NewApp(name, path string) (*AppConfig, error) { return nil, err } } + c.App.Name = name return c, nil } diff --git a/pkg/registry/registry.go b/pkg/registry/registry.go index c996815..721a8cc 100644 --- a/pkg/registry/registry.go +++ b/pkg/registry/registry.go @@ -202,7 +202,7 @@ func (kr *KbrewRegistry) Info(appName string) (string, error) { if err != nil { return "", err } - a, err := config.NewApp(appName, c) + a, err := config.NewApp(appName, c, false) if err != nil { return "", err } @@ -219,7 +219,7 @@ func (kr *KbrewRegistry) Args(appName string) (map[string]interface{}, error) { if err != nil { return nil, err } - a, err := config.NewApp(appName, c) + a, err := config.NewApp(appName, c, false) if err != nil { return nil, err }