Skip to content

Commit

Permalink
Merge pull request #2728 from 89luca89/fix/run_watch_globs
Browse files Browse the repository at this point in the history
fix: run_watch glob pattern handling
  • Loading branch information
FabianKramm committed Oct 10, 2023
2 parents 5f97e99 + 2d4121d commit 6bafc07
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
11 changes: 10 additions & 1 deletion docs/pages/_partials/command_run_watch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ run_watch --path [MY_PATH] -- [my_command]

### `--path`, `-p`

The paths to watch. Can be patterns in the form of `./**/my-file.txt`.
The paths to watch. Can be patterns in the form of `"./**/my-file.txt"`.

Please note that globgs need to be quoted in order to avoid them being expanded
by your shell, for example:


```
run_watch --path "**/*.go" -- devspace dev
```


```
```
Expand Down
28 changes: 28 additions & 0 deletions e2e/tests/pipelines/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,34 @@ var _ = DevSpaceDescribe("pipelines", func() {
framework.ExpectNoError(err)
}
})
ginkgo.It("should fail to watch files with unquoted globbing", func(ctx context.Context) {
tempDir, err := framework.CopyToTempDir("tests/pipelines/testdata/run_watch")
framework.ExpectNoError(err)
ginkgo.DeferCleanup(framework.CleanupTempDir, initialDir, tempDir)

ns, err := kubeClient.CreateNamespace("pipelines")
framework.ExpectNoError(err)
ginkgo.DeferCleanup(framework.ExpectDeleteNamespace, kubeClient, ns)

cancelCtx, cancel := context.WithCancel(ctx)
ginkgo.DeferCleanup(cancel)

output := &bytes.Buffer{}
multiWriter := io.MultiWriter(output, os.Stdout)
log := logpkg.NewStreamLogger(multiWriter, multiWriter, logrus.DebugLevel)

devCmd := &cmd.RunPipelineCmd{
GlobalFlags: &flags.GlobalFlags{
NoWarn: true,
Namespace: ns,
},
Pipeline: "unquoted-glob",
Ctx: cancelCtx,
Log: log,
}
err = devCmd.RunDefault(f)
framework.ExpectError(err)
})

ginkgo.It("should use --set and --set-string values from run_pipelines command", func() {
tempDir, err := framework.CopyToTempDir("tests/pipelines/testdata/run_pipelines")
Expand Down
8 changes: 6 additions & 2 deletions e2e/tests/pipelines/testdata/run_watch/devspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ pipelines:
--path './foo*/**/*' \
--exclude './foo2/*' \
-- date
unquoted-glob: |-
# Still triggers run_watch for foo2/bar2.txt changes
run_watch \
--path **/* \
-- date
23 changes: 23 additions & 0 deletions pkg/devspace/pipeline/engine/basichandler/commands/run_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ type RunWatchOptions struct {
}

func RunWatch(ctx context.Context, args []string, handler types2.ExecHandler, log log.Logger) error {
command := []string{}

// Separately handle the `--` catchall flag
// in order to have clean arguments for the parsing below
commandINdex := indexOf("--", args)
if commandINdex > 0 {
command = args[commandINdex+1:]
}

options := &RunWatchOptions{}
args, err := flags.ParseArgs(options, args)
if err != nil {
Expand All @@ -41,10 +50,15 @@ func RunWatch(ctx context.Context, args []string, handler types2.ExecHandler, lo
if len(args) == 0 {
return fmt.Errorf("usage: run_watch --path MY_PATH -- my_command")
}
if len(args) > len(command) {
// if we have more args left thant the one after "--" then we have some invalid flags inside
return fmt.Errorf("invalid flags: %v, usage: run_watch --path MY_PATH --path 'MY/**/GLOB*/PATH' -- my_command", command)
}

w := &watcher{
options: *options,
}

return w.Watch(ctx, func(ctx context.Context) error {
return handler.ExecHandler(ctx, args)
}, log)
Expand Down Expand Up @@ -232,3 +246,12 @@ func (w *watcher) startCommand(ctx context.Context, action func(ctx context.Cont
})
return t
}

func indexOf(element string, data []string) int {
for k, v := range data {
if element == v {
return k
}
}
return -1
}

0 comments on commit 6bafc07

Please sign in to comment.