Skip to content

Commit d8978ac

Browse files
siredmarlizardruss
authored andcommitted
fix: KUBECONFIG gets set when using --kubeconfig via var DEVSPACE_FLAGS
Signed-off-by: Armin Schlegel <armin.schlegel@gmx.de>
1 parent b773b9b commit d8978ac

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

pkg/util/flags/flags.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package flags
22

33
import (
44
"fmt"
5+
"os"
56
"strings"
67

78
"github.com/loft-sh/devspace/pkg/devspace/env"
@@ -18,6 +19,18 @@ func ApplyExtraFlags(cobraCmd *cobra.Command, osArgs []string, forceParsing bool
1819
return nil, err
1920
}
2021

22+
for i := 0; i < len(flags); i++ {
23+
if flags[i] == "--kubeconfig" {
24+
if i+1 < len(flags) {
25+
err = os.Setenv("KUBECONFIG", flags[i+1])
26+
if err != nil {
27+
return nil, err
28+
}
29+
break
30+
}
31+
}
32+
}
33+
2134
commandFlags, err := ParseCommandLine(env.GlobalGetEnv(envName))
2235
if err != nil {
2336
return nil, err

pkg/util/flags/flags_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package flags
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/loft-sh/devspace/cmd/flags"
8+
"github.com/loft-sh/devspace/pkg/devspace/env"
9+
"github.com/spf13/cobra"
10+
"gotest.tools/assert"
11+
)
12+
13+
func Test_ApplyExtraFlagsWithEmpty(t *testing.T) {
14+
mycmd := cobra.Command{}
15+
flags, err := ApplyExtraFlags(&mycmd, []string{}, false)
16+
assert.NilError(t, err, "Error applying extra flags")
17+
assert.Equal(t, 0, len(flags), "Flags should be empty")
18+
}
19+
20+
func Test_ApplyExtraFlagsWithDevspaceFlags(t *testing.T) {
21+
devspaceFlags := env.GlobalGetEnv("DEVSPACE_FLAGS")
22+
assert.Equal(t, "", devspaceFlags, "DEVSPACE_FLAGS should be empty")
23+
24+
devspaceFlags = "-s --kubeconfig /path/to/kubeconfig --debug"
25+
err := os.Setenv("DEVSPACE_FLAGS", devspaceFlags)
26+
assert.NilError(t, err, "Error setting DEVSPACE_FLAGS")
27+
mycmd := &cobra.Command{}
28+
persistentFlags := mycmd.PersistentFlags()
29+
_ = flags.SetGlobalFlags(persistentFlags)
30+
31+
flags, err := ApplyExtraFlags(mycmd, []string{}, false)
32+
assert.NilError(t, err, "Error applying extra flags")
33+
assert.Equal(t, 4, len(flags), "Flags should have 4 elements")
34+
35+
kubeconfig := env.GlobalGetEnv("KUBECONFIG")
36+
assert.Equal(t, "/path/to/kubeconfig", kubeconfig, "Path should be /path/to/kubeconfig")
37+
}

0 commit comments

Comments
 (0)