chore: update project layout

This commit is contained in:
moonD4rk
2023-03-10 14:52:26 +08:00
parent 9850624d45
commit b65d3186c4
36 changed files with 181 additions and 179 deletions
+176
View File
@@ -0,0 +1,176 @@
package fileutil
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"
cp "github.com/otiai10/copy"
)
// FileExists checks if the file exists in the provided path
func FileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
if err != nil {
return false
}
return !info.IsDir()
}
// FolderExists checks if the folder exists
func FolderExists(foldername string) bool {
info, err := os.Stat(foldername)
if os.IsNotExist(err) {
return false
}
if err != nil {
return false
}
return info.IsDir()
}
// FilesInFolder returns the filepath contains in the provided folder
func FilesInFolder(dir, filename string) ([]string, error) {
if !FolderExists(dir) {
return nil, errors.New(dir + " folder does not exist")
}
var files []string
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() && strings.HasSuffix(path, filename) {
files = append(files, path)
}
return err
})
return files, err
}
// ReadFile reads the file from the provided path
func ReadFile(filename string) (string, error) {
s, err := os.ReadFile(filename)
return string(s), err
}
// CopyDir copies the directory from the source to the destination
// skip the file if you don't want to copy
func CopyDir(src, dst, skip string) error {
s := cp.Options{Skip: func(info os.FileInfo, src, dst string) (bool, error) {
return strings.HasSuffix(strings.ToLower(src), skip), nil
}}
return cp.Copy(src, dst, s)
}
// CopyDirHasSuffix copies the directory from the source to the destination
// contain is the file if you want to copy, and rename copied filename with dir/index_filename
func CopyDirHasSuffix(src, dst, suffix string) error {
var files []string
err := filepath.Walk(src, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() && strings.HasSuffix(strings.ToLower(f.Name()), suffix) {
files = append(files, path)
}
return err
})
if err != nil {
return err
}
if err := os.MkdirAll(dst, 0o700); err != nil {
return err
}
for index, file := range files {
// p = dir/index_file
p := fmt.Sprintf("%s/%d_%s", dst, index, BaseDir(file))
err = CopyFile(file, p)
if err != nil {
return err
}
}
return nil
}
// CopyFile copies the file from the source to the destination
func CopyFile(src, dst string) error {
s, err := os.ReadFile(src)
if err != nil {
return err
}
err = os.WriteFile(dst, s, 0o600)
if err != nil {
return err
}
return nil
}
// ItemName returns the filename from the provided path
func ItemName(browser, item, ext string) string {
replace := strings.NewReplacer(" ", "_", ".", "_", "-", "_")
return strings.ToLower(fmt.Sprintf("%s_%s.%s", replace.Replace(browser), item, ext))
}
func BrowserName(browser, user string) string {
replace := strings.NewReplacer(" ", "_", ".", "_", "-", "_", "Profile", "User")
return strings.ToLower(fmt.Sprintf("%s_%s", replace.Replace(browser), replace.Replace(user)))
}
// ParentDir returns the parent directory of the provided path
func ParentDir(p string) string {
return filepath.Dir(filepath.Clean(p))
}
// BaseDir returns the base directory of the provided path
func BaseDir(p string) string {
return filepath.Base(p)
}
// ParentBaseDir returns the parent base directory of the provided path
func ParentBaseDir(p string) string {
return BaseDir(ParentDir(p))
}
// CompressDir compresses the directory into a zip file
func CompressDir(dir string) error {
files, err := os.ReadDir(dir)
if err != nil {
return err
}
b := new(bytes.Buffer)
zw := zip.NewWriter(b)
for _, f := range files {
fw, err := zw.Create(f.Name())
if err != nil {
return err
}
name := path.Join(dir, f.Name())
content, err := os.ReadFile(name)
if err != nil {
return err
}
_, err = fw.Write(content)
if err != nil {
return err
}
err = os.Remove(name)
if err != nil {
return err
}
}
if err := zw.Close(); err != nil {
return err
}
filename := filepath.Join(dir, fmt.Sprintf("%s.zip", dir))
outFile, err := os.Create(filepath.Clean(filename))
if err != nil {
return err
}
_, err = b.WriteTo(outFile)
if err != nil {
return err
}
return outFile.Close()
}
+53
View File
@@ -0,0 +1,53 @@
package typeutil
import (
"time"
"golang.org/x/exp/constraints"
)
// Keys returns a slice of the keys of the map. based with go 1.18 generics
func Keys[K comparable, V any](m map[K]V) []K {
r := make([]K, 0, len(m))
for k := range m {
r = append(r, k)
}
return r
}
func IntToBool[T constraints.Signed](a T) bool {
switch a {
case 0, -1:
return false
}
return true
}
func Reverse[T any](s []T) []T {
h := make([]T, len(s))
for i := 0; i < len(s); i++ {
h[i] = s[len(s)-i-1]
}
return h
}
func TimeStamp(stamp int64) time.Time {
s := time.Unix(stamp, 0)
if s.Local().Year() > 9999 {
return time.Date(9999, 12, 13, 23, 59, 59, 0, time.Local)
}
return s
}
func TimeEpoch(epoch int64) time.Time {
maxTime := int64(99633311740000000)
if epoch > maxTime {
return time.Date(2049, 1, 1, 1, 1, 1, 1, time.Local)
}
t := time.Date(1601, 1, 1, 0, 0, 0, 0, time.Local)
d := time.Duration(epoch)
for i := 0; i < 1000; i++ {
t = t.Add(d)
}
return t
}
+24
View File
@@ -0,0 +1,24 @@
package typeutil
import (
"testing"
)
func TestReverse(t *testing.T) {
t.Parallel()
reverseTestCases := [][]any{
{1, 2, 3, 4, 5},
{"1", "2", "3", "4", "5"},
{"1", 2, "3", "4", 5},
}
for _, ts := range reverseTestCases {
h := Reverse(ts)
for i := 0; i < len(ts); i++ {
if h[len(h)-i-1] != ts[i] {
t.Errorf("reverse failed %v != %v", h, ts)
}
}
}
}