Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

condtionally evaluate template while creating config #128

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
10 changes: 4 additions & 6 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
},
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 16 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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{},
Expand All @@ -144,6 +158,7 @@ func NewApp(name, path string) (*AppConfig, error) {
return nil, err
}
}

c.App.Name = name
return c, nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down