diff --git a/changelog.md b/changelog.md index 318b184a..a2014755 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,9 @@ ## Changelog +#### v1.6.4 + +* `[path]` Added method IsDotfile for checking dotfile names + #### v1.6.3 * `[strutil]` Added methods PrefixSize and SuffixSize diff --git a/path/path.go b/path/path.go index 1a9b6e56..7451fce2 100644 --- a/path/path.go +++ b/path/path.go @@ -14,6 +14,7 @@ import ( "errors" PATH "path" "path/filepath" + "strings" "pkg.re/essentialkaos/ek.v1/env" ) @@ -112,6 +113,21 @@ func IsSafe(path string) bool { return true } +// IsDotfile return true if file name begins with a full stop +func IsDotfile(path string) bool { + if path == "" { + return false + } + + if !strings.Contains(path, "/") { + return path[0:1] == "." + } + + pathBase := Base(path) + + return pathBase[0:1] == "." +} + // ////////////////////////////////////////////////////////////////////////////////// // func evalHome(path string) string { diff --git a/path/path_test.go b/path/path_test.go index 5bb0e015..a54d1ae9 100644 --- a/path/path_test.go +++ b/path/path_test.go @@ -81,3 +81,16 @@ func (s *PathUtilSuite) TestSafe(c *C) { c.Assert(IsSafe("/var/db/yum"), Equals, false) c.Assert(IsSafe("/var/lib/pgsql"), Equals, false) } + +func (s *PathUtilSuite) TestDotfile(c *C) { + c.Assert(IsDotfile(""), Equals, false) + c.Assert(IsDotfile("/some/dir/abcd"), Equals, false) + c.Assert(IsDotfile("/some/dir/"), Equals, false) + c.Assert(IsDotfile("/"), Equals, false) + c.Assert(IsDotfile("/////"), Equals, false) + c.Assert(IsDotfile(" / "), Equals, false) + + c.Assert(IsDotfile(".dotfile"), Equals, true) + c.Assert(IsDotfile("/.dotfile"), Equals, true) + c.Assert(IsDotfile("/some/dir/.abcd"), Equals, true) +} diff --git a/path/path_windows.go b/path/path_windows.go index f93eb056..aa3e9c7b 100644 --- a/path/path_windows.go +++ b/path/path_windows.go @@ -65,3 +65,8 @@ func Split(path string) (dir, file string) { func IsSafe(path string) bool { return false } + +// IsDotfile return true if file name begins with a full stop +func IsDotfile(path string) bool { + return false +}