Skip to content

Commit c22eb99

Browse files
committed
refactoring
1 parent bafcb10 commit c22eb99

File tree

5 files changed

+26
-25
lines changed

5 files changed

+26
-25
lines changed

files.go renamed to fs/files.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
44
*/
55

6-
package ioutils
6+
package fs
77

88
import (
99
"io"
@@ -21,12 +21,18 @@ func CurrentDir() string {
2121
return dir
2222
}
2323

24-
func Exist(filename string) bool {
24+
func ParentFolder(filename string) string {
25+
dir := filepath.Dir(filename)
26+
tree := strings.Split(dir, string(os.PathSeparator))
27+
return tree[len(tree)-1]
28+
}
29+
30+
func FileExist(filename string) bool {
2531
_, err := os.Stat(filename)
2632
return err == nil
2733
}
2834

29-
func Search(dir, filename string) ([]string, error) {
35+
func SearchFiles(dir, filename string) ([]string, error) {
3036
files := make([]string, 0, 2)
3137
err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
3238
if err != nil {
@@ -41,7 +47,7 @@ func Search(dir, filename string) ([]string, error) {
4147
return files, err
4248
}
4349

44-
func SearchByExt(dir, ext string) ([]string, error) {
50+
func SearchFilesByExt(dir, ext string) ([]string, error) {
4551
files := make([]string, 0, 2)
4652
err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
4753
if err != nil {
@@ -56,8 +62,8 @@ func SearchByExt(dir, ext string) ([]string, error) {
5662
return files, err
5763
}
5864

59-
func Rewrite(filename string, call func([]byte) ([]byte, error)) error {
60-
if !Exist(filename) {
65+
func RewriteFile(filename string, call func([]byte) ([]byte, error)) error {
66+
if !FileExist(filename) {
6167
if err := os.WriteFile(filename, []byte(""), 0755); err != nil {
6268
return err
6369
}
@@ -72,7 +78,7 @@ func Rewrite(filename string, call func([]byte) ([]byte, error)) error {
7278
return os.WriteFile(filename, b, 0755)
7379
}
7480

75-
func Copy(dst, src string, mode os.FileMode) error {
81+
func CopyFile(dst, src string, mode os.FileMode) error {
7682
source, err := os.OpenFile(src, os.O_RDONLY, 0)
7783
if err != nil {
7884
return err
@@ -96,9 +102,3 @@ func Copy(dst, src string, mode os.FileMode) error {
96102
_, err = io.Copy(dist, source)
97103
return err
98104
}
99-
100-
func Folder(filename string) string {
101-
dir := filepath.Dir(filename)
102-
tree := strings.Split(dir, string(os.PathSeparator))
103-
return tree[len(tree)-1]
104-
}

hash.go renamed to hash/hash.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
44
*/
55

6-
package ioutils
6+
package hash
77

88
import (
99
"encoding/hex"
@@ -15,7 +15,7 @@ import (
1515
"go.osspkg.com/errors"
1616
)
1717

18-
func IsValidFileHash(filename string, h hash.Hash, valid string) error {
18+
func ValidateFile(filename string, h hash.Hash, valid string) error {
1919
r, err := os.Open(filename)
2020
if err != nil {
2121
return err
@@ -32,7 +32,7 @@ func IsValidFileHash(filename string, h hash.Hash, valid string) error {
3232
return nil
3333
}
3434

35-
func FileHash(filename string, h hash.Hash) (string, error) {
35+
func CreateFromFile(filename string, h hash.Hash) (string, error) {
3636
r, err := os.Open(filename)
3737
if err != nil {
3838
return "", err

pool.go renamed to pool/pool.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
44
*/
55

6-
package ioutils
6+
package pool
77

88
import "sync"
99

@@ -16,7 +16,7 @@ type Pool[T TPool] struct {
1616
pool sync.Pool
1717
}
1818

19-
func NewPool[T TPool](callNew func() T) *Pool[T] {
19+
func New[T TPool](callNew func() T) *Pool[T] {
2020
return &Pool[T]{
2121
pool: sync.Pool{New: func() any { return callNew() }},
2222
}
@@ -42,9 +42,3 @@ type SlicePool[T any] struct {
4242
func (v *SlicePool[T]) Reset() {
4343
v.B = v.B[:0]
4444
}
45-
46-
func NewSlicePool[T any](l, c int) *Pool[*SlicePool[T]] {
47-
return NewPool(func() *SlicePool[T] {
48-
return &SlicePool[T]{B: make([]T, l, c)}
49-
})
50-
}

pool_test.go renamed to pool/pool_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
44
*/
55

6-
package ioutils
6+
package pool
77

88
import (
99
"testing"

pool/slice.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package pool
2+
3+
func NewSlicePool[T any](l, c int) *Pool[*SlicePool[T]] {
4+
return New(func() *SlicePool[T] {
5+
return &SlicePool[T]{B: make([]T, l, c)}
6+
})
7+
}

0 commit comments

Comments
 (0)