Skip to content

Commit

Permalink
✨ feat(fs): update and add some fs util func
Browse files Browse the repository at this point in the history
- add SlashPath, UnixPath
- add OpenAppendFile, OpenTruncFile
  • Loading branch information
inhere committed Mar 3, 2023
1 parent 7460c0f commit ededf5b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 36 deletions.
14 changes: 14 additions & 0 deletions fsutil/fsutil.go
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
)

const (
Expand Down Expand Up @@ -83,3 +84,16 @@ func ReaderMimeType(r io.Reader) (mime string) {
func JoinPaths(elem ...string) string {
return filepath.Join(elem...)
}

// SlashPath alias of filepath.ToSlash
func SlashPath(path string) string {
return filepath.ToSlash(path)
}

// UnixPath like of filepath.ToSlash, but always replace
func UnixPath(path string) string {
if !strings.ContainsRune(path, '\\') {
return path
}
return strings.ReplaceAll(path, "\\", "/")
}
19 changes: 19 additions & 0 deletions fsutil/fsutil_nonwin_test.go
@@ -0,0 +1,19 @@
//go:build !windows

package fsutil_test

import (
"testing"

"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/testutil/assert"
)

func TestSlashPath_nw(t *testing.T) {
assert.Eq(t, "path/to/dir", fsutil.JoinPaths("path", "to", "dir"))
}

func TestRealpath_nw(t *testing.T) {
inPath := "/path/to/some/../dir"
assert.Eq(t, "/path/to/dir", fsutil.Realpath(inPath))
}
16 changes: 7 additions & 9 deletions fsutil/fsutil_test.go
Expand Up @@ -2,7 +2,6 @@ package fsutil_test

import (
"bytes"
"runtime"
"testing"

"github.com/gookit/goutil/fsutil"
Expand Down Expand Up @@ -37,15 +36,14 @@ func TestTempDir(t *testing.T) {
assert.NoErr(t, fsutil.Remove(dir))
}

func TestRealpath(t *testing.T) {
inPath := "/path/to/some/../dir"
if runtime.GOOS == "windows" {
assert.Eq(t, "\\path\\to\\dir", fsutil.Realpath(inPath))
} else {
assert.Eq(t, "/path/to/dir", fsutil.Realpath(inPath))
}

func TestSplitPath(t *testing.T) {
dir, file := fsutil.SplitPath("/path/to/dir/some.txt")
assert.Eq(t, "/path/to/dir/", dir)
assert.Eq(t, "some.txt", file)
}

func TestSlashPath(t *testing.T) {
assert.Eq(t, "/path/to/dir", fsutil.SlashPath("/path/to/dir"))
assert.Eq(t, "/path/to/dir", fsutil.UnixPath("/path/to/dir"))
assert.Eq(t, "/path/to/dir", fsutil.UnixPath("\\path\\to\\dir"))
}
19 changes: 19 additions & 0 deletions fsutil/fsutil_windows_test.go
@@ -0,0 +1,19 @@
//go:build windows

package fsutil_test

import (
"testing"

"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/testutil/assert"
)

func TestSlashPath_win(t *testing.T) {
assert.Eq(t, "path\\to\\dir", fsutil.JoinPaths("path", "to", "dir"))
}

func TestRealpath_win(t *testing.T) {
inPath := "/path/to/some/../dir"
assert.Eq(t, "\\path\\to\\dir", fsutil.Realpath(inPath))
}
32 changes: 15 additions & 17 deletions fsutil/operate.go
Expand Up @@ -8,6 +8,8 @@ import (
"path"
"path/filepath"
"strings"

"github.com/gookit/goutil/basefn"
)

// Mkdir alias of os.MkdirAll()
Expand Down Expand Up @@ -57,13 +59,6 @@ const (
FsRFlags = os.O_RDONLY // read-only
)

func fsFlagsOr(fileFlags []int, fileFlag int) int {
if len(fileFlags) > 0 {
return fileFlags[0]
}
return fileFlag
}

// OpenFile like os.OpenFile, but will auto create dir.
func OpenFile(filepath string, flag int, perm os.FileMode) (*os.File, error) {
fileDir := path.Dir(filepath)
Expand All @@ -80,14 +75,22 @@ func OpenFile(filepath string, flag int, perm os.FileMode) (*os.File, error) {

/* TODO MustOpenFile() */

// QuickOpenFile like os.OpenFile, open for write, if not exists, will create it.
//
// Tip: file flag default is FsCWAFlags
// QuickOpenFile like os.OpenFile, open for append write. if not exists, will create it.
func QuickOpenFile(filepath string, fileFlag ...int) (*os.File, error) {
flag := fsFlagsOr(fileFlag, FsCWAFlags)
flag := basefn.FirstOr(fileFlag, FsCWAFlags)
return OpenFile(filepath, flag, DefaultFilePerm)
}

// OpenAppendFile like os.OpenFile, open for append write. if not exists, will create it.
func OpenAppendFile(filepath string) (*os.File, error) {
return OpenFile(filepath, FsCWAFlags, DefaultFilePerm)
}

// OpenTruncFile like os.OpenFile, open for override write. if not exists, will create it.
func OpenTruncFile(filepath string) (*os.File, error) {
return OpenFile(filepath, FsCWTFlags, DefaultFilePerm)
}

// OpenReadFile like os.OpenFile, open file for read contents
func OpenReadFile(filepath string) (*os.File, error) {
return os.OpenFile(filepath, FsRFlags, OnlyReadFilePerm)
Expand All @@ -107,11 +110,7 @@ func CreateFile(fpath string, filePerm, dirPerm os.FileMode, fileFlag ...int) (*
}
}

flag := FsCWTFlags
if len(fileFlag) > 0 {
flag = fileFlag[0]
}

flag := basefn.FirstOr(fileFlag, FsCWAFlags)
return os.OpenFile(fpath, flag, filePerm)
}

Expand Down Expand Up @@ -193,7 +192,6 @@ func Unzip(archive, targetDir string) (err error) {
}

for _, file := range reader.File {

if strings.Contains(file.Name, "..") {
return fmt.Errorf("illegal file path in zip: %v", file.Name)
}
Expand Down
17 changes: 7 additions & 10 deletions fsutil/opwrite.go
Expand Up @@ -3,6 +3,8 @@ package fsutil
import (
"io"
"os"

"github.com/gookit/goutil/basefn"
)

// ************************************************************
Expand All @@ -13,14 +15,13 @@ import (
//
// data type allow: string, []byte, io.Reader
//
// Tip: file flag default is FsCWAFlags
// Tip: file flag default is FsCWTFlags (override write)
//
// Usage:
//
// fsutil.PutContents(filePath, contents, fsutil.FsCWTFlags)
// fsutil.PutContents(filePath, contents, fsutil.FsCWAFlags) // append write
func PutContents(filePath string, data any, fileFlag ...int) (int, error) {
// create and open file
f, err := QuickOpenFile(filePath, fileFlag...)
f, err := QuickOpenFile(filePath, basefn.FirstOr(fileFlag, FsCWTFlags))
if err != nil {
return 0, err
}
Expand All @@ -32,17 +33,13 @@ func PutContents(filePath string, data any, fileFlag ...int) (int, error) {
//
// data type allow: string, []byte, io.Reader
//
// Tip: file flag default is FsCWTFlags
// Tip: file flag default is FsCWTFlags (override write)
//
// Usage:
//
// fsutil.WriteFile(filePath, contents, fsutil.DefaultFilePerm, fsutil.FsCWAFlags)
func WriteFile(filePath string, data any, perm os.FileMode, fileFlag ...int) error {
flag := FsCWTFlags
if len(fileFlag) > 0 {
flag = fileFlag[0]
}

flag := basefn.FirstOr(fileFlag, FsCWTFlags)
f, err := OpenFile(filePath, flag, perm)
if err != nil {
return err
Expand Down

0 comments on commit ededf5b

Please sign in to comment.