-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
4 changed files
with
108 additions
and
33 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-FileCopyrightText: 2024 caixw | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
package validator | ||
|
||
import ( | ||
"errors" | ||
"io/fs" | ||
"os" | ||
) | ||
|
||
// FileExists path 是否存在 | ||
func FileExists(path string) bool { | ||
_, err := os.Stat(path) | ||
return err == nil || !errors.Is(err, fs.ErrNotExist) | ||
} | ||
|
||
// IsDir path 是否为一个目录 | ||
func IsDir(path string) bool { | ||
s, err := os.Stat(path) | ||
return err == nil && s.IsDir() | ||
} | ||
|
||
// FileExistsFS 判断文件是否存在于 fsys | ||
func FileExistsFS(fsys fs.FS) func(string) bool { | ||
return func(path string) bool { | ||
_, err := fs.Stat(fsys, path) | ||
return err == nil || !errors.Is(err, fs.ErrNotExist) | ||
} | ||
} | ||
|
||
// IsDirFS 判断 fsys 中的 path 是否为目录 | ||
func IsDirFS(fsys fs.FS) func(string) bool { | ||
return func(path string) bool { | ||
s, err := os.Stat(path) | ||
return err == nil && s.IsDir() | ||
} | ||
} |
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,36 @@ | ||
// SPDX-FileCopyrightText: 2024 caixw | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
package validator | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/issue9/assert/v4" | ||
) | ||
|
||
func TestFileExists(t *testing.T) { | ||
a := assert.New(t, false) | ||
|
||
a.True(FileExists("./file.go")). | ||
False(FileExists("./not-exists.go")) | ||
|
||
f := FileExistsFS(os.DirFS("./")) | ||
a.True(f("file.go")). | ||
False(f("not-exists.go")) | ||
} | ||
|
||
func TestIsDir(t *testing.T) { | ||
a := assert.New(t, false) | ||
|
||
a.True(IsDir(".")). | ||
False(IsDir("./file.go")). | ||
False(IsDir("not-exists.go")) | ||
|
||
f := IsDirFS(os.DirFS("./")) | ||
a.True(f(".")). | ||
False(f("file.go")). | ||
False(f("not-exists.go")) | ||
} |