Delete utils directory

Fraud
This commit is contained in:
Tay754 2024-08-15 06:42:11 -04:00 committed by GitHub
parent 6a495a1449
commit facc7bf730
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 0 additions and 301 deletions

View File

@ -1,8 +0,0 @@
package byteutil
var OnSplitUTF8Func = func(r rune) rune {
if r == 0x00 || r == 0x01 {
return -1
}
return r
}

View File

@ -1,159 +0,0 @@
package fileutil
import (
"archive/zip"
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
cp "github.com/otiai10/copy"
)
// IsFileExists checks if the file exists in the provided path
func IsFileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
if err != nil {
return false
}
return !info.IsDir()
}
// IsDirExists checks if the folder exists
func IsDirExists(folder string) bool {
info, err := os.Stat(folder)
if os.IsNotExist(err) {
return false
}
if err != nil {
return false
}
return info.IsDir()
}
// 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)
}
// 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
}
// Filename returns the filename from the provided path
func Filename(browser, dataType, ext string) string {
replace := strings.NewReplacer(" ", "_", ".", "_", "-", "_")
return strings.ToLower(fmt.Sprintf("%s_%s.%s", replace.Replace(browser), dataType, 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 fmt.Errorf("read dir error: %w", err)
}
if len(files) == 0 {
// Return an error if no files are found in the directory
return fmt.Errorf("no files to compress in: %s", dir)
}
buffer := new(bytes.Buffer)
zipWriter := zip.NewWriter(buffer)
defer func() {
_ = zipWriter.Close()
}()
for _, file := range files {
if err := addFileToZip(zipWriter, filepath.Join(dir, file.Name())); err != nil {
return fmt.Errorf("failed to add file to zip: %w", err)
}
}
if err := zipWriter.Close(); err != nil {
return fmt.Errorf("error closing zip writer: %w", err)
}
zipFilename := filepath.Join(dir, filepath.Base(dir)+".zip")
return writeFile(buffer, zipFilename)
}
func addFileToZip(zw *zip.Writer, filename string) error {
content, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("error reading file %s: %w", filename, err)
}
fw, err := zw.Create(filepath.Base(filename))
if err != nil {
return fmt.Errorf("error creating zip entry for %s: %w", filename, err)
}
if _, err = fw.Write(content); err != nil {
return fmt.Errorf("error writing content to zip for %s: %w", filename, err)
}
if err = os.Remove(filename); err != nil {
return fmt.Errorf("error removing original file %s: %w", filename, err)
}
return nil
}
func writeFile(buffer *bytes.Buffer, filename string) error {
outFile, err := os.Create(filename)
if err != nil {
return fmt.Errorf("error creating output file %s: %w", filename, err)
}
defer func() {
_ = outFile.Close()
}()
if _, err = buffer.WriteTo(outFile); err != nil {
return fmt.Errorf("error writing data to file %s: %w", filename, err)
}
return nil
}

View File

@ -1,52 +0,0 @@
package fileutil
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func setupTestDir(t *testing.T, files []string) string {
t.Helper() // Marks the function as a helper function.
tempDir, err := os.MkdirTemp("", "testCompressDir")
require.NoError(t, err, "failed to create a temporary directory")
for _, file := range files {
filePath := filepath.Join(tempDir, file)
err := os.WriteFile(filePath, []byte("test content"), 0o644)
require.NoError(t, err, "failed to create a test file")
}
return tempDir
}
func TestCompressDir(t *testing.T) {
t.Run("Normal Operation", func(t *testing.T) {
tempDir := setupTestDir(t, []string{"file1.txt", "file2.txt", "file3.txt"})
defer os.RemoveAll(tempDir)
err := CompressDir(tempDir)
assert.NoError(t, err, "compressDir should not return an error")
// Check if the zip file exists
zipFile := filepath.Join(tempDir, filepath.Base(tempDir)+".zip")
assert.FileExists(t, zipFile, "zip file should be created")
})
t.Run("Directory Does Not Exist", func(t *testing.T) {
err := CompressDir("/path/to/nonexistent/directory")
assert.Error(t, err, "should return an error for non-existent directory")
})
t.Run("Empty Directory", func(t *testing.T) {
tempDir, err := os.MkdirTemp("", "testEmptyDir")
require.NoError(t, err, "failed to create empty test directory")
defer os.RemoveAll(tempDir)
err = CompressDir(tempDir)
assert.Error(t, err, "should return an error for an empty directory")
})
}

View File

@ -1,58 +0,0 @@
package typeutil
import (
"time"
)
// 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
}
// Signed is a constraint that permits any signed integer type.
// If future releases of Go add new predeclared signed integer types,
// this constraint will be modified to include them.
type Signed interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}
func IntToBool[T 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
}

View File

@ -1,24 +0,0 @@
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)
}
}
}
}