Skip to content

Commit 2c3a4b7

Browse files
committed
fix: disable expressions & command vars on initial loading
1 parent cedda03 commit 2c3a4b7

File tree

6 files changed

+51
-16
lines changed

6 files changed

+51
-16
lines changed

cmd/root.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"flag"
66
"fmt"
77
"github.com/loft-sh/devspace/pkg/devspace/config/loader"
8+
"github.com/loft-sh/devspace/pkg/devspace/config/loader/variable/expression"
89
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
910
"github.com/loft-sh/devspace/pkg/devspace/env"
1011
"github.com/loft-sh/devspace/pkg/util/log"
@@ -185,11 +186,14 @@ func BuildRoot(f factory.Factory, excludePlugins bool) *cobra.Command {
185186
}
186187

187188
// try to parse the raw config
188-
rawConfig, err := parseConfig(f)
189-
if err != nil {
190-
f.GetLog().Debugf("error parsing raw config: %v", err)
191-
} else {
192-
env.GlobalGetEnv = rawConfig.GetEnv
189+
var rawConfig *RawConfig
190+
if os.Getenv(expression.DEVSPACE_SKIP_PRELOAD_ENV) == "" {
191+
rawConfig, err = parseConfig(f)
192+
if err != nil {
193+
f.GetLog().Debugf("error parsing raw config: %v", err)
194+
} else {
195+
env.GlobalGetEnv = rawConfig.GetEnv
196+
}
193197
}
194198

195199
// build the root cmd
@@ -323,7 +327,9 @@ func parseConfig(f factory.Factory) (*RawConfig, error) {
323327
r := &RawConfig{
324328
resolved: map[string]string{},
325329
}
326-
_, err = configLoader.LoadWithParser(timeoutCtx, nil, nil, r, &loader.ConfigOptions{Dry: true}, log.Discard)
330+
_, err = configLoader.LoadWithParser(timeoutCtx, nil, nil, r, &loader.ConfigOptions{
331+
Dry: true,
332+
}, log.Discard)
327333
if r.Resolver != nil {
328334
return r, nil
329335
}

pkg/devspace/config/loader/variable/command_variable.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package variable
33
import (
44
"bytes"
55
"context"
6+
"github.com/loft-sh/devspace/pkg/devspace/config/loader/variable/expression"
67
"github.com/loft-sh/devspace/pkg/devspace/pipeline/engine"
78
"mvdan.cc/sh/v3/expand"
89
"os"
@@ -53,10 +54,13 @@ func execCommand(ctx context.Context, varName string, definition *latest.Variabl
5354
writer := &bytes.Buffer{}
5455
stdErrWriter := &bytes.Buffer{}
5556
var err error
57+
envVars := []string{}
58+
envVars = append(envVars, expression.DEVSPACE_SKIP_PRELOAD_ENV+"=true")
59+
envVars = append(envVars, os.Environ()...)
5660
if args == nil {
57-
err = engine.ExecuteSimpleShellCommand(ctx, dir, expand.ListEnviron(os.Environ()...), writer, stdErrWriter, nil, cmd)
61+
err = engine.ExecuteSimpleShellCommand(ctx, dir, expand.ListEnviron(envVars...), writer, stdErrWriter, nil, cmd)
5862
} else {
59-
err = command.Command(ctx, dir, expand.ListEnviron(os.Environ()...), writer, stdErrWriter, nil, cmd, args...)
63+
err = command.Command(ctx, dir, expand.ListEnviron(envVars...), writer, stdErrWriter, nil, cmd, args...)
6064
}
6165
if err != nil {
6266
errMsg := "fill variable " + varName + " with command '" + cmd + "': " + err.Error()

pkg/devspace/config/loader/variable/default_variable.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package variable
33
import (
44
"context"
55
"github.com/loft-sh/devspace/pkg/devspace/config/localcache"
6+
"github.com/sirupsen/logrus"
67
"os"
78
"strconv"
89

@@ -23,6 +24,7 @@ func NewDefaultVariable(name string, workingDirectory string, localCache localca
2324
type defaultVariable struct {
2425
name string
2526
workingDirectory string
27+
dry bool
2628
localCache localcache.Cache
2729
log log.Logger
2830
}
@@ -47,12 +49,22 @@ func (d *defaultVariable) Load(ctx context.Context, definition *latest.Variable)
4749
}
4850
}
4951

50-
// Now ask the question
51-
value, err := askQuestion(definition, d.log)
52-
if err != nil {
53-
return nil, err
52+
// is logger silent
53+
if d.log == log.Discard || d.log.GetLevel() < logrus.InfoLevel {
54+
if definition.Default != nil {
55+
return definition.Default, nil
56+
}
57+
58+
return valueByType(value, definition.Default)
59+
} else {
60+
var err error
61+
value, err = askQuestion(definition, d.log)
62+
if err != nil {
63+
return nil, err
64+
}
5465
}
5566

67+
// Now ask the question
5668
if !definition.NoCache {
5769
d.localCache.SetVar(d.name, value)
5870
}

pkg/devspace/config/loader/variable/expression/expression.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
// ExpressionMatchRegex is the regex to check if a value matches the devspace var format
2222
var ExpressionMatchRegex = regexp.MustCompile(`(?ms)^\$\#?\!?\((.+)\)$`)
2323

24+
const DEVSPACE_SKIP_PRELOAD_ENV = "DEVSPACE_SKIP_PRELOAD"
25+
2426
func expressionMatchFn(key, value string) bool {
2527
return ExpressionMatchRegex.MatchString(value)
2628
}
@@ -95,7 +97,11 @@ func ResolveExpressions(ctx context.Context, value, dir string, variables map[st
9597

9698
stdout := &bytes.Buffer{}
9799
stderr := &bytes.Buffer{}
98-
err := engine.ExecuteSimpleShellCommand(ctx, dir, env.NewVariableEnvProvider(expand.ListEnviron(os.Environ()...), vars), stdout, stderr, nil, match[1], os.Args[1:]...)
100+
101+
envVars := []string{}
102+
envVars = append(envVars, DEVSPACE_SKIP_PRELOAD_ENV+"=true")
103+
envVars = append(envVars, os.Environ()...)
104+
err := engine.ExecuteSimpleShellCommand(ctx, dir, env.NewVariableEnvProvider(expand.ListEnviron(envVars...), vars), stdout, stderr, nil, match[1], os.Args[1:]...)
99105
if err != nil {
100106
if len(strings.TrimSpace(stdout.String())) == 0 && len(strings.TrimSpace(stderr.String())) == 0 {
101107
if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() == 1 {

pkg/devspace/config/loader/variable/resolver.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ func MergeVarsWithFlags(vars map[string]interface{}, flags []string) error {
5555
type resolver struct {
5656
vars map[string]*latest.Variable
5757
memoryCache map[string]interface{}
58-
localCache localcache.Cache
59-
options *PredefinedVariableOptions
60-
log log.Logger
58+
59+
localCache localcache.Cache
60+
options *PredefinedVariableOptions
61+
log log.Logger
6162
}
6263

6364
func varMatchFn(key, value string) bool {

pkg/devspace/config/loader/variable/undefined_variable.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"github.com/loft-sh/devspace/pkg/devspace/config/localcache"
7+
"github.com/sirupsen/logrus"
78
"os"
89
"strconv"
910

@@ -38,6 +39,11 @@ func (u *undefinedVariable) Load(ctx context.Context, _ *latest.Variable) (inter
3839
return convertStringValue(v), nil
3940
}
4041

42+
// is logger silent
43+
if u.log == log.Discard || u.log.GetLevel() < logrus.InfoLevel {
44+
return "", nil
45+
}
46+
4147
// Ask for variable
4248
val, err := askQuestion(&latest.Variable{
4349
Question: "Please enter a value for " + u.name,

0 commit comments

Comments
 (0)