Skip to content

Commit 3547781

Browse files
committed
feat: --from-file, ${devspace.name} & ${devspace.tempFolder}
1 parent ed7c67f commit 3547781

File tree

28 files changed

+457
-180
lines changed

28 files changed

+457
-180
lines changed

cmd/build.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/loft-sh/devspace/pkg/devspace/build"
77
"github.com/loft-sh/devspace/pkg/devspace/config/loader"
88
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
9+
"github.com/loft-sh/devspace/pkg/devspace/context/values"
910
pipelinetypes "github.com/loft-sh/devspace/pkg/devspace/pipeline/types"
1011
"github.com/loft-sh/devspace/pkg/devspace/plugin"
1112
"github.com/loft-sh/devspace/pkg/util/factory"
@@ -74,6 +75,9 @@ func (cmd *BuildCmd) Run(f factory.Factory) error {
7475
defer cancelFn()
7576
}
7677

78+
// set command in context
79+
cmd.Ctx = values.WithCommand(cmd.Ctx, "build")
80+
7781
configOptions := cmd.ToConfigOptions()
7882
ctx, err := prepare(cmd.Ctx, f, configOptions, cmd.GlobalFlags, true)
7983
if err != nil {

cmd/deploy.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/loft-sh/devspace/pkg/devspace/config/loader"
99
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
1010
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
11+
"github.com/loft-sh/devspace/pkg/devspace/context/values"
1112
"github.com/loft-sh/devspace/pkg/devspace/dependency"
1213
"github.com/loft-sh/devspace/pkg/devspace/dependency/registry"
1314
"github.com/loft-sh/devspace/pkg/devspace/deploy"
@@ -25,6 +26,7 @@ import (
2526
"github.com/sirupsen/logrus"
2627
"github.com/spf13/cobra"
2728
"gopkg.in/yaml.v3"
29+
"io/ioutil"
2830
"k8s.io/client-go/kubernetes/fake"
2931
"os"
3032
"strings"
@@ -113,6 +115,9 @@ func (cmd *DeployCmd) Run(f factory.Factory) error {
113115
defer cancelFn()
114116
}
115117

118+
// set command in context
119+
cmd.Ctx = values.WithCommand(cmd.Ctx, "deploy")
120+
116121
configOptions := cmd.ToConfigOptions()
117122
ctx, err := prepare(cmd.Ctx, f, configOptions, cmd.GlobalFlags, false)
118123
if err != nil {
@@ -158,6 +163,15 @@ func prepare(ctx context.Context, f factory.Factory, configOptions *loader.Confi
158163
// start file logging
159164
logpkg.StartFileLogging()
160165

166+
// create a temporary folder for us to use
167+
tempFolder, err := ioutil.TempDir("", "devspace-")
168+
if err != nil {
169+
return nil, errors.Wrap(err, "create temporary folder")
170+
}
171+
172+
// add temp folder to context
173+
ctx = values.WithTempFolder(ctx, tempFolder)
174+
161175
// get the main logger after file logging is started
162176
log := f.GetLog()
163177

cmd/dev.go

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

33
import (
44
"context"
5+
"github.com/loft-sh/devspace/pkg/devspace/context/values"
56
"io"
67
"os"
78

@@ -133,6 +134,9 @@ func (cmd *DevCmd) Run(f factory.Factory) error {
133134
defer cancelFn()
134135
}
135136

137+
// set command in context
138+
cmd.Ctx = values.WithCommand(cmd.Ctx, "dev")
139+
136140
configOptions := cmd.ToConfigOptions()
137141
ctx, err := prepare(cmd.Ctx, f, configOptions, cmd.GlobalFlags, false)
138142
if err != nil {
@@ -199,6 +203,10 @@ func runWithHooks(ctx *devspacecontext.Context, command string, fn func() error)
199203
}
200204

201205
defer func() {
206+
// delete temp folder
207+
deleteTempFolder(ctx.Context, ctx.Log)
208+
209+
// execute hooks
202210
if err != nil {
203211
hook.LogExecuteHooks(ctx, map[string]interface{}{"error": err}, command+":after:execute", command+":error")
204212
} else {
@@ -207,10 +215,25 @@ func runWithHooks(ctx *devspacecontext.Context, command string, fn func() error)
207215
}()
208216

209217
return interrupt.Global.Run(fn, func() {
218+
// delete temp folder
219+
deleteTempFolder(ctx.Context, ctx.Log)
220+
221+
// execute hooks
210222
hook.LogExecuteHooks(ctx, nil, command+":interrupt")
211223
})
212224
}
213225

226+
func deleteTempFolder(ctx context.Context, log log.Logger) {
227+
// delete temp folder
228+
tempFolder, ok := values.TempFolderFrom(ctx)
229+
if ok && tempFolder != os.TempDir() {
230+
err := os.RemoveAll(tempFolder)
231+
if err != nil {
232+
log.Debugf("error removing temp folder: %v", err)
233+
}
234+
}
235+
}
236+
214237
func (cmd *DevCmd) adjustConfig(conf config.Config) error {
215238
// check if terminal is enabled
216239
c := conf.Config()

cmd/flags/flags.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type GlobalFlags struct {
1111
NoWarn bool
1212
Debug bool
1313

14+
OverrideName string
1415
Namespace string
1516
KubeContext string
1617
Profiles []string
@@ -29,6 +30,7 @@ func (gf *GlobalFlags) ToConfigOptions() *loader.ConfigOptions {
2930
profiles := []string{}
3031
profiles = append(profiles, gf.Profiles...)
3132
return &loader.ConfigOptions{
33+
OverrideName: gf.OverrideName,
3234
Profiles: profiles,
3335
ProfileRefresh: gf.ProfileRefresh,
3436
DisableProfileActivation: gf.DisableProfileActivation,
@@ -43,6 +45,7 @@ func SetGlobalFlags(flags *flag.FlagSet) *GlobalFlags {
4345
Flags: flags,
4446
}
4547

48+
flags.StringVar(&globalFlags.OverrideName, "override-name", "", "If specified will override the devspace.yaml name")
4649
flags.BoolVar(&globalFlags.NoWarn, "no-warn", false, "If true does not show any warning when deploying into a different namespace or kube-context than before")
4750
flags.BoolVar(&globalFlags.Debug, "debug", false, "Prints the stack trace if an error occurs")
4851
flags.BoolVar(&globalFlags.Silent, "silent", false, "Run in silent mode and prevents any devspace log output except panics & fatals")

cmd/purge.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"github.com/loft-sh/devspace/pkg/devspace/config/loader"
66
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
7+
"github.com/loft-sh/devspace/pkg/devspace/context/values"
78
"github.com/loft-sh/devspace/pkg/devspace/pipeline/types"
89
"github.com/loft-sh/devspace/pkg/devspace/plugin"
910

@@ -74,6 +75,9 @@ func (cmd *PurgeCmd) Run(f factory.Factory) error {
7475
defer cancelFn()
7576
}
7677

78+
// set command in context
79+
cmd.Ctx = values.WithCommand(cmd.Ctx, "purge")
80+
7781
configOptions := cmd.ToConfigOptions()
7882
ctx, err := prepare(cmd.Ctx, f, configOptions, cmd.GlobalFlags, false)
7983
if err != nil {

e2e/framework/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func ExpectLocalFileContents(filePath string, contents string) {
138138
func ExpectLocalFileContentsWithoutSpaces(filePath string, contents string) {
139139
out, err := ioutil.ReadFile(filePath)
140140
ExpectNoError(err)
141-
gomega.ExpectWithOffset(2, strings.TrimSpace(string(out))).To(gomega.Equal(contents))
141+
gomega.ExpectWithOffset(1, strings.TrimSpace(string(out))).To(gomega.Equal(contents))
142142
}
143143

144144
func ExpectLocalFileNotFound(filePath string) {

e2e/tests/imports/imports.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"github.com/loft-sh/devspace/e2e/kube"
88
"github.com/loft-sh/devspace/pkg/util/factory"
99
"github.com/onsi/ginkgo"
10+
"io/ioutil"
1011
"os"
12+
"strings"
1113
)
1214

1315
var _ = DevSpaceDescribe("imports", func() {
@@ -52,10 +54,22 @@ var _ = DevSpaceDescribe("imports", func() {
5254
// run the command
5355
err = deployCmd.Run(f)
5456
framework.ExpectNoError(err)
57+
58+
// read temp folder
59+
out, err := ioutil.ReadFile("temp.txt")
60+
framework.ExpectNoError(err)
61+
framework.ExpectLocalFileContentsWithoutSpaces("name.txt", "base")
5562
framework.ExpectLocalFileContentsWithoutSpaces("dependency.txt", "import3")
63+
framework.ExpectLocalFileContentsWithoutSpaces("dependency-name.txt", "import1")
64+
framework.ExpectLocalFileContentsWithoutSpaces("dependency-temp.txt", strings.TrimSpace(string(out)))
5665
framework.ExpectLocalFileContentsWithoutSpaces("import1.txt", "import1")
5766
framework.ExpectLocalFileContentsWithoutSpaces("import2.txt", "import2")
67+
framework.ExpectLocalFileContentsWithoutSpaces("import2-name.txt", "base")
5868
framework.ExpectLocalFileContentsWithoutSpaces("import3.txt", "import3")
5969
framework.ExpectLocalFileContentsWithoutSpaces("vars.txt", ns+"-"+ns+"-base-import1-import2-import3")
70+
71+
// make sure temp folder is erased
72+
_, err = os.Stat(strings.TrimSpace(string(out)))
73+
framework.ExpectError(err)
6074
})
6175
})

e2e/tests/imports/testdata/local/devspace.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@ vars:
1111
dependencies:
1212
import1:
1313
path: import1.yaml
14-
pipeline: import3
14+
pipeline: import3-dep
1515

1616
pipelines:
1717
deploy:
1818
steps:
1919
- run: |-
20+
echo ${devspace.name} > name.txt
21+
echo ${devspace.tempFolder} > temp.txt
22+
2023
run_dependency_pipelines --all > dependency.txt
2124
2225
run_pipelines import1 > import1.txt
2326
run_pipelines import2 > import2.txt
27+
run_pipelines import2-name > import2-name.txt
2428
run_pipelines import3 > import3.txt
2529
2630
echo ${DEVSPACE_NAMESPACE}-${devspace.namespace}-${BASE}-${IMPORT1}-${IMPORT2}-${IMPORT3} > vars.txt

e2e/tests/imports/testdata/local/import2.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ vars:
55
IMPORT2: import2
66

77
pipelines:
8+
import2-name:
9+
steps:
10+
- run: |-
11+
echo ${devspace.name}
812
import2:
913
steps:
1014
- run: |-

e2e/tests/imports/testdata/local/import3.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ pipelines:
88
import3:
99
steps:
1010
- run: |-
11-
echo ${IMPORT3}
11+
echo ${IMPORT3}
12+
import3-dep:
13+
steps:
14+
- run: |-
15+
echo ${IMPORT3}
16+
echo ${devspace.name} > dependency-name.txt
17+
echo ${devspace.tempFolder} > dependency-temp.txt

0 commit comments

Comments
 (0)