-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2cf46f
commit 7c89f75
Showing
8 changed files
with
176 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package common | ||
|
||
import ( | ||
"errors" | ||
"path" | ||
"regexp" | ||
) | ||
|
||
type WatchPathConfig struct { | ||
// Directory to watch for events | ||
Directory string `json:"directory"` | ||
// Path is relative path of object to watch with respect to the directory | ||
Path string `json:"path,omitempty"` | ||
// PathRegexp is regexp of relative path of object to watch with respect to the directory | ||
PathRegexp string `json:"pathRegexp,omitempty"` | ||
} | ||
|
||
// Validate validates WatchPathConfig | ||
func (c *WatchPathConfig) Validate() error { | ||
if c.Directory == "" { | ||
return errors.New("directory is required") | ||
} | ||
if !path.IsAbs(c.Directory) { | ||
return errors.New("directory must be an absolute file path") | ||
} | ||
if c.Path == "" && c.PathRegexp == "" { | ||
return errors.New("either path or pathRegexp must be specified") | ||
} | ||
if c.Path != "" && c.PathRegexp != "" { | ||
return errors.New("path and pathRegexp cannot be specified together") | ||
} | ||
if c.Path != "" && path.IsAbs(c.Path) { | ||
return errors.New("path must be a relative file path") | ||
} | ||
if c.PathRegexp != "" { | ||
_, err := regexp.Compile(c.PathRegexp) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package common | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPathConfigValidate(t *testing.T) { | ||
var validConfigs []WatchPathConfig | ||
validConfigs = append(validConfigs, WatchPathConfig{ | ||
Directory: "/foo", | ||
Path: "bar", | ||
PathRegexp: "", | ||
}) | ||
validConfigs = append(validConfigs, WatchPathConfig{ | ||
Directory: "/foo", | ||
Path: "", | ||
PathRegexp: "bar", | ||
}) | ||
for _, config := range validConfigs { | ||
err := config.Validate() | ||
assert.NoError(t, err) | ||
} | ||
|
||
var invalidConfigs []WatchPathConfig | ||
// empty dir | ||
invalidConfigs = append(invalidConfigs, WatchPathConfig{ | ||
Directory: "", | ||
Path: "", | ||
PathRegexp: "", | ||
}) | ||
// relative dir | ||
invalidConfigs = append(invalidConfigs, WatchPathConfig{ | ||
Directory: "foo", | ||
Path: "bar", | ||
PathRegexp: "", | ||
}) | ||
// both path and path regexp | ||
invalidConfigs = append(invalidConfigs, WatchPathConfig{ | ||
Directory: "/foo", | ||
Path: "bar", | ||
PathRegexp: "bar", | ||
}) | ||
// absolute path | ||
invalidConfigs = append(invalidConfigs, WatchPathConfig{ | ||
Directory: "/foo", | ||
Path: "/bar", | ||
PathRegexp: "", | ||
}) | ||
// invalid regexp | ||
invalidConfigs = append(invalidConfigs, WatchPathConfig{ | ||
Directory: "/foo", | ||
Path: "", | ||
PathRegexp: "][", | ||
}) | ||
for _, config := range invalidConfigs { | ||
err := config.Validate() | ||
assert.Error(t, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters