Skip to content

Commit

Permalink
feat: allow filtering by extension with -e flag
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaschain committed Dec 4, 2023
1 parent b238641 commit 71a5749
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
10 changes: 10 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

var types []string
var extensions []string
var allowFailing bool
var defaultTypes = []string{"WRITE"}

Expand All @@ -20,6 +21,15 @@ func SetFlags(cmd *cobra.Command) {
fmt.Sprintf("Event types to watch, options: %s", event_types.EventTypes),
)

cmd.Flags().StringSliceVarP(
&extensions,
"extension",
"e",
[]string{},
`If provided, only files with the given extensions will trigger the command. E.g.:
$ beholder . -e go -e yml -- go test ./...`,
)

cmd.Flags().BoolVarP(
&allowFailing,
"allow-failing",
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func Run(cmd *cobra.Command, args []string) {
Paths: paths,
Command: command,
AllowedTypes: allowedTypes,
Extensions: extensions,
AllowFailing: allowFailing,
}
use_case.Watch(watchConfig, infrastructure.FileWatcher, infrastructure.Command)
Expand Down
15 changes: 15 additions & 0 deletions core/extension_filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core

func ExtensionFilter(path string, extensions []string) bool {
if len(extensions) == 0 {
return true
}

for _, extension := range extensions {
if extension == path[len(path)-len(extension):] {
return true
}
}

return false
}
22 changes: 22 additions & 0 deletions core/extension_filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package core_test

import (
"testing"

"github.com/lucaschain/beholder/core"
"github.com/stretchr/testify/assert"
)

func TestExtensionFilter(t *testing.T) {
t.Run("should return true if no extensions passed", func(t *testing.T) {
assert.True(t, core.ExtensionFilter("foo.go", []string{}))
})

t.Run("should return true if path has one of the extensions", func(t *testing.T) {
assert.True(t, core.ExtensionFilter("foo.go", []string{".go", ".rb"}))
})

t.Run("should return false if the patch matches none of the extensions", func(t *testing.T) {
assert.False(t, core.ExtensionFilter("foo.go", []string{".rb", ".js"}))
})
}
3 changes: 2 additions & 1 deletion use_case/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type WatchConfig struct {
Paths []string
Command []string
AllowedTypes []event_types.EventType
Extensions []string
AllowFailing bool
}

Expand All @@ -24,7 +25,7 @@ func onFileChange(c WatchConfig, commandRunner CommandRunner) core.ChangeCallbac
return err
}

if event_types.Filter(event.Type, c.AllowedTypes) {
if event_types.Filter(event.Type, c.AllowedTypes) && core.ExtensionFilter(event.FileName, c.Extensions) {
command := core.CommandTokens(c.Command, event)
commandError := commandRunner(command)

Expand Down
1 change: 1 addition & 0 deletions use_case/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func buildWatchConfig() use_case.WatchConfig {
return use_case.WatchConfig{
Paths: []string{"."},
Command: []string{"echo", "hello"},
Extensions: []string{".txt"},
AllowedTypes: []event_types.EventType{event_types.Create},
AllowFailing: false,
}
Expand Down

0 comments on commit 71a5749

Please sign in to comment.