@@ -8,6 +8,10 @@ import (
88 "fmt"
99 "github.com/loft-sh/devspace/pkg/devspace/context/values"
1010 "github.com/loft-sh/devspace/pkg/devspace/kubectl"
11+ "github.com/loft-sh/devspace/pkg/util/downloader"
12+ "github.com/loft-sh/devspace/pkg/util/downloader/commands"
13+ "github.com/loft-sh/devspace/pkg/util/log"
14+ "github.com/sirupsen/logrus"
1115 "os"
1216 "path/filepath"
1317 "strconv"
@@ -30,44 +34,60 @@ type PredefinedVariableOptions struct {
3034}
3135
3236// PredefinedVariableFunction is the definition of a predefined variable
33- type PredefinedVariableFunction func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error )
37+ type PredefinedVariableFunction func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error )
3438
3539// predefinedVars holds all predefined variables that can be used in the config
3640var predefinedVars = map [string ]PredefinedVariableFunction {
37- "DEVSPACE_NAME" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
41+ "DEVSPACE_NAME" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
3842 name , ok := values .NameFrom (ctx )
3943 if ! ok {
4044 return "" , nil
4145 }
4246 return name , nil
4347 },
44- "DEVSPACE_TMPDIR" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
48+ "DEVSPACE_EXECUTABLE" : func (ctx context.Context , options * PredefinedVariableOptions , log log.Logger ) (interface {}, error ) {
49+ ex , err := os .Executable ()
50+ if err != nil {
51+ return nil , err
52+ }
53+ return ex , nil
54+ },
55+ "DEVSPACE_KUBECTL_EXECUTABLE" : func (ctx context.Context , options * PredefinedVariableOptions , log log.Logger ) (interface {}, error ) {
56+ debugLog := log .WithLevel (logrus .DebugLevel )
57+ path , err := downloader .NewDownloader (commands .NewKubectlCommand (), debugLog ).EnsureCommand (ctx )
58+ if err != nil {
59+ debugLog .Debugf ("Error downloading kubectl: %v" , err )
60+ return "" , nil
61+ }
62+ return path , nil
63+ },
64+ "DEVSPACE_TMPDIR" : func (ctx context.Context , options * PredefinedVariableOptions , log log.Logger ) (interface {}, error ) {
4565 tempFolder , ok := values .TempFolderFrom (ctx )
4666 if ! ok {
4767 return os .TempDir (), nil
4868 }
4969 return tempFolder , nil
5070 },
51- "DEVSPACE_VERSION" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
71+ "DEVSPACE_VERSION" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
5272 return upgrade .GetVersion (), nil
5373 },
54- "DEVSPACE_RANDOM" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
74+ "DEVSPACE_RANDOM" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
5575 return randutil .GenerateRandomString (6 ), nil
5676 },
57- "DEVSPACE_PROFILE" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
77+ "DEVSPACE_PROFILE" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
5878 return options .Profile , nil
5979 },
60- "DEVSPACE_USER_HOME" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
80+ "DEVSPACE_USER_HOME" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
6181 homeDir , err := homedir .Dir ()
6282 if err != nil {
6383 return nil , err
6484 }
6585 return homeDir , nil
6686 },
67- "DEVSPACE_TIMESTAMP" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
87+ "DEVSPACE_TIMESTAMP" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
6888 return strconv .FormatInt (time .Now ().Unix (), 10 ), nil
6989 },
70- "DEVSPACE_GIT_BRANCH" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
90+ "DEVSPACE_GIT_BRANCH" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
7191 configPath := options .ConfigPath
7292 branch , err := git .GetBranch (filepath .Dir (configPath ))
7393 if err != nil {
@@ -76,7 +96,7 @@ var predefinedVars = map[string]PredefinedVariableFunction{
7696
7797 return branch , nil
7898 },
79- "DEVSPACE_GIT_COMMIT" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
99+ "DEVSPACE_GIT_COMMIT" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
80100 configPath := options .ConfigPath
81101 hash , err := git .GetHash (ctx , filepath .Dir (configPath ))
82102 if err != nil {
@@ -85,14 +105,14 @@ var predefinedVars = map[string]PredefinedVariableFunction{
85105
86106 return hash [:8 ], nil
87107 },
88- "DEVSPACE_CONTEXT" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
108+ "DEVSPACE_CONTEXT" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
89109 if options .KubeClient == nil {
90110 return "" , nil
91111 }
92112
93113 return options .KubeClient .CurrentContext (), nil
94114 },
95- "DEVSPACE_NAMESPACE" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
115+ "DEVSPACE_NAMESPACE" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
96116 if options .KubeClient == nil {
97117 return "" , nil
98118 }
@@ -126,7 +146,7 @@ func AddPredefinedVars(plugins []plugin.Metadata) {
126146 pluginFolder := p .PluginFolder
127147 for _ , variable := range p .Vars {
128148 v := variable
129- predefinedVars [variable .Name ] = func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
149+ predefinedVars [variable .Name ] = func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
130150 args , err := json .Marshal (os .Args )
131151 if err != nil {
132152 return "" , err
@@ -148,20 +168,22 @@ func AddPredefinedVars(plugins []plugin.Metadata) {
148168
149169// NewPredefinedVariable creates a new predefined variable for the given name or fails if there
150170// is none with the given name
151- func NewPredefinedVariable (name string , options * PredefinedVariableOptions ) (Variable , error ) {
171+ func NewPredefinedVariable (name string , options * PredefinedVariableOptions , log log. Logger ) (Variable , error ) {
152172 if _ , ok := predefinedVars [name ]; ! ok {
153173 return nil , errors .New ("predefined variable " + name + " not found" )
154174 }
155175
156176 return & predefinedVariable {
157177 name : name ,
158178 options : options ,
179+ log : log ,
159180 }, nil
160181}
161182
162183type predefinedVariable struct {
163184 name string
164185 options * PredefinedVariableOptions
186+ log log.Logger
165187}
166188
167189func (p * predefinedVariable ) Load (ctx context.Context , definition * latest.Variable ) (interface {}, error ) {
@@ -170,5 +192,5 @@ func (p *predefinedVariable) Load(ctx context.Context, definition *latest.Variab
170192 return nil , errors .New ("predefined variable " + p .name + " not found" )
171193 }
172194
173- return getVar (ctx , p .options )
195+ return getVar (ctx , p .options , p . log )
174196}
0 commit comments