Skip to content

Commit 95a69cb

Browse files
author
Alexis Sellier
committed
Add DEVSPACE_PROFILES variable and is_in command
1 parent ffa1317 commit 95a69cb

5 files changed

Lines changed: 48 additions & 21 deletions

File tree

pkg/devspace/config/loader/loader.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ package loader
33
import (
44
"context"
55
"fmt"
6-
"github.com/loft-sh/devspace/pkg/devspace/context/values"
7-
"github.com/loft-sh/devspace/pkg/util/encoding"
8-
"github.com/loft-sh/devspace/pkg/util/yamlutil"
96
"io/ioutil"
10-
"mvdan.cc/sh/v3/expand"
117
"os"
128
"path/filepath"
139
"regexp"
1410
"strings"
1511

12+
"github.com/loft-sh/devspace/pkg/devspace/context/values"
13+
"github.com/loft-sh/devspace/pkg/util/encoding"
14+
"github.com/loft-sh/devspace/pkg/util/yamlutil"
15+
"mvdan.cc/sh/v3/expand"
16+
1617
"github.com/loft-sh/devspace/pkg/devspace/config/localcache"
1718
"github.com/loft-sh/devspace/pkg/devspace/config/remotecache"
1819
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
@@ -301,7 +302,7 @@ func (l *configLoader) parseConfig(
301302
resolver, err := variable.NewResolver(localCache, &variable.PredefinedVariableOptions{
302303
ConfigPath: l.absConfigPath,
303304
KubeClient: client,
304-
Profile: GetLastProfile(options.Profiles),
305+
Profile: options.Profiles,
305306
}, options.Vars, log)
306307
if err != nil {
307308
return nil, nil, nil, err
@@ -567,13 +568,6 @@ func (l *configLoader) applyProfiles(ctx context.Context, data map[string]interf
567568
return data, nil
568569
}
569570

570-
func GetLastProfile(profiles []string) string {
571-
if len(profiles) == 0 {
572-
return ""
573-
}
574-
return profiles[len(profiles)-1]
575-
}
576-
577571
// configExistsInPath checks whether a devspace configuration exists at a certain path
578572
func configExistsInPath(path string) bool {
579573
_, err := os.Stat(path)

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"os"
10+
"path/filepath"
11+
"strconv"
12+
"strings"
13+
"time"
14+
915
"github.com/loft-sh/devspace/pkg/devspace/context/values"
1016
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
1117
"github.com/loft-sh/devspace/pkg/util/downloader"
1218
"github.com/loft-sh/devspace/pkg/util/downloader/commands"
1319
"github.com/loft-sh/devspace/pkg/util/log"
1420
"github.com/sirupsen/logrus"
15-
"os"
16-
"path/filepath"
17-
"strconv"
18-
"strings"
19-
"time"
2021

2122
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
2223
"github.com/loft-sh/devspace/pkg/devspace/plugin"
@@ -30,7 +31,7 @@ import (
3031
type PredefinedVariableOptions struct {
3132
ConfigPath string
3233
KubeClient kubectl.Client
33-
Profile string
34+
Profile []string
3435
}
3536

3637
// PredefinedVariableFunction is the definition of a predefined variable
@@ -75,6 +76,9 @@ var predefinedVars = map[string]PredefinedVariableFunction{
7576
return randutil.GenerateRandomString(6), nil
7677
},
7778
"DEVSPACE_PROFILE": func(ctx context.Context, options *PredefinedVariableOptions, log log.Logger) (interface{}, error) {
79+
return GetLastProfile(options.Profile), nil
80+
},
81+
"DEVSPACE_PROFILES": func(ctx context.Context, options *PredefinedVariableOptions, log log.Logger) (interface{}, error) {
7882
return options.Profile, nil
7983
},
8084
"DEVSPACE_USER_HOME": func(ctx context.Context, options *PredefinedVariableOptions, log log.Logger) (interface{}, error) {
@@ -194,3 +198,10 @@ func (p *predefinedVariable) Load(ctx context.Context, definition *latest.Variab
194198

195199
return getVar(ctx, p.options, p.log)
196200
}
201+
202+
func GetLastProfile(profiles []string) string {
203+
if len(profiles) == 0 {
204+
return ""
205+
}
206+
return profiles[len(profiles)-1]
207+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/pkg/errors"
2121
)
2222

23-
var AlwaysResolvePredefinedVars = []string{"DEVSPACE_NAME", "DEVSPACE_EXECUTABLE", "DEVSPACE_KUBECTL_EXECUTABLE", "DEVSPACE_TMPDIR", "DEVSPACE_VERSION", "DEVSPACE_RANDOM", "DEVSPACE_PROFILE", "DEVSPACE_USER_HOME", "DEVSPACE_TIMESTAMP", "devspace.context", "DEVSPACE_CONTEXT", "devspace.namespace", "DEVSPACE_NAMESPACE"}
23+
var AlwaysResolvePredefinedVars = []string{"DEVSPACE_NAME", "DEVSPACE_EXECUTABLE", "DEVSPACE_KUBECTL_EXECUTABLE", "DEVSPACE_TMPDIR", "DEVSPACE_VERSION", "DEVSPACE_RANDOM", "DEVSPACE_PROFILE", "DEVSPACE_PROFILES", "DEVSPACE_USER_HOME", "DEVSPACE_TIMESTAMP", "devspace.context", "DEVSPACE_CONTEXT", "devspace.namespace", "DEVSPACE_NAMESPACE"}
2424

2525
// NewResolver creates a new resolver that caches resolved variables in memory and in the provided cache
2626
func NewResolver(localCache localcache.Cache, predefinedVariableOptions *PredefinedVariableOptions, flags []string, log log.Logger) (Resolver, error) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package commands
2+
3+
import (
4+
"mvdan.cc/sh/v3/interp"
5+
)
6+
7+
func IsIn(args []string) error {
8+
if len(args) < 2 {
9+
return interp.NewExitStatus(1)
10+
}
11+
needed := args[0]
12+
for _, value := range args[1:] {
13+
if value == needed {
14+
return interp.NewExitStatus(0)
15+
}
16+
}
17+
return interp.NewExitStatus(1)
18+
}

pkg/devspace/pipeline/engine/basichandler/handler.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ package basichandler
33
import (
44
"context"
55
"fmt"
6+
"os"
7+
"time"
8+
69
enginecommands "github.com/loft-sh/devspace/pkg/devspace/pipeline/engine/basichandler/commands"
710
"github.com/loft-sh/devspace/pkg/devspace/pipeline/engine/types"
811
"github.com/loft-sh/devspace/pkg/util/downloader"
912
"github.com/loft-sh/devspace/pkg/util/downloader/commands"
1013
"github.com/loft-sh/devspace/pkg/util/log"
1114
"github.com/pkg/errors"
1215
"mvdan.cc/sh/v3/interp"
13-
"os"
14-
"time"
1516
)
1617

1718
// BasicCommands are extra commands DevSpace provides within the shell or are common
@@ -32,6 +33,9 @@ var BasicCommands = map[string]func(ctx context.Context, args []string) error{
3233
"is_true": func(ctx context.Context, args []string) error {
3334
return enginecommands.IsTrue(args)
3435
},
36+
"is_in": func(ctx context.Context, args []string) error {
37+
return enginecommands.IsIn(args)
38+
},
3539
"sleep": func(ctx context.Context, args []string) error {
3640
return HandleError(ctx, "sleep", enginecommands.Sleep(ctx, args))
3741
},

0 commit comments

Comments
 (0)