From e0daee4766ec47366071b13fbae41f16aee5f1b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Mon, 4 Nov 2024 03:04:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=A7=BB=E5=8A=A8=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=88=B0utils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 3 ++- go.sum | 23 +++++++++-------------- internal/apps/mysql/service.go | 4 ++-- internal/apps/nginx/service.go | 4 ++-- internal/apps/toolbox/service.go | 10 +++++----- internal/data/app.go | 1 + internal/data/backup.go | 12 ++++++------ internal/data/container_image.go | 4 ++-- internal/data/container_volume.go | 4 ++-- internal/service/file.go | 8 ++++---- internal/service/file_windows.go | 8 ++++---- pkg/api/api.go | 3 +-- pkg/copier/copier.go | 18 ------------------ pkg/slice/slice.go | 30 ------------------------------ pkg/str/string.go | 17 ----------------- pkg/str/string_test.go | 27 --------------------------- pkg/tools/tools.go | 13 +++++++++++++ pkg/tools/tools_test.go | 28 ++++++++++++++++++++-------- 18 files changed, 73 insertions(+), 144 deletions(-) delete mode 100644 pkg/copier/copier.go delete mode 100644 pkg/slice/slice.go delete mode 100644 pkg/str/string.go delete mode 100644 pkg/str/string_test.go diff --git a/go.mod b/go.mod index 78701e9d..86486cb5 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/go-rat/chix v1.1.4 github.com/go-rat/gormstore v1.0.6 github.com/go-rat/sessions v1.0.11 - github.com/go-rat/utils v1.0.6 + github.com/go-rat/utils v1.0.7 github.com/go-resty/resty/v2 v2.15.3 github.com/go-sql-driver/mysql v1.8.1 github.com/golang-cz/httplog v0.0.0-20241002114323-98e09d6f537a @@ -80,6 +80,7 @@ require ( github.com/tklauser/numcpus v0.8.0 // indirect github.com/yusufpapurcu/wmi v1.2.4 // indirect go.uber.org/multierr v1.11.0 // indirect + golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect golang.org/x/sys v0.26.0 // indirect golang.org/x/text v0.19.0 // indirect gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect diff --git a/go.sum b/go.sum index 43ba884d..9278e52c 100644 --- a/go.sum +++ b/go.sum @@ -21,8 +21,7 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= -github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= -github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4= +github.com/gabriel-vasile/mimetype v1.4.6 h1:3+PzJTKLkvgjeTbts6msPJt4DixhT4YtFNf1gtGe3zc= github.com/gabriel-vasile/mimetype v1.4.6/go.mod h1:JX1qVKqZd40hUPpAfiNTe0Sne7hdfKSbOqqmkq8GCXc= github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec+ruQ= github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc= @@ -50,14 +49,8 @@ github.com/go-rat/securecookie v1.0.1 h1:HW0fpKmB+FjJzXTw8ABOwBJ+XrPmRBSZqHhmrv8 github.com/go-rat/securecookie v1.0.1/go.mod h1:tP/ObWYyjmcpabQ7WTon/i2lBSip/Aolliw2llXuPDU= github.com/go-rat/sessions v1.0.11 h1:2eTUvhEBtymrAtwv71LlGHNylz+Ad2SgY3jg/sSUboA= github.com/go-rat/sessions v1.0.11/go.mod h1:Ray/GCbuhm4U9xpjFFSCfOTCEn91puEhAXX5creHE9g= -github.com/go-rat/utils v1.0.3 h1:SqH/O0KYq4SBv8naSAjJA4hC45/d8NLNrTCONfqjbFM= -github.com/go-rat/utils v1.0.3/go.mod h1:4WNPlrF57KmeGZN3HOeBgdBVLJL3xgba4QRP/t6pKrE= -github.com/go-rat/utils v1.0.4 h1:k9jkR4ddL0Fgex3P4qURvNCDx61S9u5LF0EZna+Reic= -github.com/go-rat/utils v1.0.4/go.mod h1:yokaqppXr7xSqhhxtioHG+txMXI9a9AGriPJnjki2ts= -github.com/go-rat/utils v1.0.5 h1:XHluZY4Cd+hrb0/9Jm1Uz38pFWrKEdh1fmW5vkPyw14= -github.com/go-rat/utils v1.0.5/go.mod h1:n22OGZrg9tp3L/T0tWo2OOMl6+jAUoBujmN0fEbDUtk= -github.com/go-rat/utils v1.0.6 h1:P70t4FWDDOT3JYVucELQFSdZR4ATGwsXPJM4wqJhBhU= -github.com/go-rat/utils v1.0.6/go.mod h1:n22OGZrg9tp3L/T0tWo2OOMl6+jAUoBujmN0fEbDUtk= +github.com/go-rat/utils v1.0.7 h1:hZUe+n1W/tvF3j+oP/TeAHX3AF98SjVB1FuF11zkCAI= +github.com/go-rat/utils v1.0.7/go.mod h1:b0S/vd2bXWvPUQpw8z6RmJrUG7G7BkMuNSrsz6JRBHs= github.com/go-resty/resty/v2 v2.15.3 h1:bqff+hcqAflpiF591hhJzNdkRsFhlB96CYfBwSFvql8= github.com/go-resty/resty/v2 v2.15.3/go.mod h1:0fHAoK7JoBy/Ch36N8VFeMsK7xQOHhvWaC3iOktwmIU= github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= @@ -178,10 +171,12 @@ golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDf golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= +golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 h1:e66Fs6Z+fZTbFBAxKfP3PALWBtpfqks2bwGcexMxgtk= +golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs1QcRcFwyucIwBGpK1p2f1YFFY= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8= -golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0= +golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= @@ -231,8 +226,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg= -golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI= +golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE= +golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= diff --git a/internal/apps/mysql/service.go b/internal/apps/mysql/service.go index 239ab527..9d8ba8bb 100644 --- a/internal/apps/mysql/service.go +++ b/internal/apps/mysql/service.go @@ -15,8 +15,8 @@ import ( "github.com/TheTNB/panel/pkg/db" "github.com/TheTNB/panel/pkg/io" "github.com/TheTNB/panel/pkg/shell" - "github.com/TheTNB/panel/pkg/str" "github.com/TheTNB/panel/pkg/systemctl" + "github.com/TheTNB/panel/pkg/tools" "github.com/TheTNB/panel/pkg/types" ) @@ -123,7 +123,7 @@ func (s *Service) Load(w http.ResponseWriter, r *http.Request) { if len(matches) > 1 { d := map[string]string{"name": expression.name, "value": matches[1]} if expression.name == "发送" || expression.name == "接收" { - d["value"] = str.FormatBytes(cast.ToFloat64(matches[1])) + d["value"] = tools.FormatBytes(cast.ToFloat64(matches[1])) } load = append(load, d) diff --git a/internal/apps/nginx/service.go b/internal/apps/nginx/service.go index 66ec44d2..eb64c790 100644 --- a/internal/apps/nginx/service.go +++ b/internal/apps/nginx/service.go @@ -13,8 +13,8 @@ import ( "github.com/TheTNB/panel/internal/service" "github.com/TheTNB/panel/pkg/io" "github.com/TheTNB/panel/pkg/shell" - "github.com/TheTNB/panel/pkg/str" "github.com/TheTNB/panel/pkg/systemctl" + "github.com/TheTNB/panel/pkg/tools" "github.com/TheTNB/panel/pkg/types" ) @@ -95,7 +95,7 @@ func (s *Service) Load(w http.ResponseWriter, r *http.Request) { service.Error(w, http.StatusInternalServerError, "获取负载失败") return } - mem := str.FormatBytes(cast.ToFloat64(out)) + mem := tools.FormatBytes(cast.ToFloat64(out)) data = append(data, types.NV{ Name: "内存占用", Value: mem, diff --git a/internal/apps/toolbox/service.go b/internal/apps/toolbox/service.go index eff77e8c..6e1bc79c 100644 --- a/internal/apps/toolbox/service.go +++ b/internal/apps/toolbox/service.go @@ -14,7 +14,7 @@ import ( "github.com/TheTNB/panel/pkg/io" "github.com/TheTNB/panel/pkg/ntp" "github.com/TheTNB/panel/pkg/shell" - "github.com/TheTNB/panel/pkg/str" + "github.com/TheTNB/panel/pkg/tools" "github.com/TheTNB/panel/pkg/types" ) @@ -77,7 +77,7 @@ func (s *Service) GetSWAP(w http.ResponseWriter, r *http.Request) { } size = file.Size() / 1024 / 1024 - total = str.FormatBytes(float64(file.Size())) + total = tools.FormatBytes(float64(file.Size())) } else { size = 0 total = "0.00 B" @@ -91,8 +91,8 @@ func (s *Service) GetSWAP(w http.ResponseWriter, r *http.Request) { match := regexp.MustCompile(`Swap:\s+(\d+)\s+(\d+)\s+(\d+)`).FindStringSubmatch(raw) if len(match) >= 4 { - used = str.FormatBytes(cast.ToFloat64(match[2]) * 1024) - free = str.FormatBytes(cast.ToFloat64(match[3]) * 1024) + used = tools.FormatBytes(cast.ToFloat64(match[2]) * 1024) + free = tools.FormatBytes(cast.ToFloat64(match[3]) * 1024) } service.Success(w, chix.M{ @@ -134,7 +134,7 @@ func (s *Service) UpdateSWAP(w http.ResponseWriter, r *http.Request) { return } if cast.ToInt64(free)*1024 < req.Size*1024*1024 { - service.Error(w, http.StatusInternalServerError, "硬盘空间不足,当前剩余%s", str.FormatBytes(cast.ToFloat64(free))) + service.Error(w, http.StatusInternalServerError, "硬盘空间不足,当前剩余%s", tools.FormatBytes(cast.ToFloat64(free))) return } diff --git a/internal/data/app.go b/internal/data/app.go index 1b1e3e46..3ce430ee 100644 --- a/internal/data/app.go +++ b/internal/data/app.go @@ -7,6 +7,7 @@ import ( "slices" "github.com/expr-lang/expr" + "github.com/go-rat/utils/collect" "github.com/hashicorp/go-version" "github.com/spf13/cast" diff --git a/internal/data/backup.go b/internal/data/backup.go index 724fe2a2..664863fd 100644 --- a/internal/data/backup.go +++ b/internal/data/backup.go @@ -16,7 +16,7 @@ import ( "github.com/TheTNB/panel/pkg/db" "github.com/TheTNB/panel/pkg/io" "github.com/TheTNB/panel/pkg/shell" - "github.com/TheTNB/panel/pkg/str" + "github.com/TheTNB/panel/pkg/tools" "github.com/TheTNB/panel/pkg/types" ) @@ -53,7 +53,7 @@ func (r *backupRepo) List(typ biz.BackupType) ([]*types.BackupFile, error) { list = append(list, &types.BackupFile{ Name: file.Name(), Path: filepath.Join(path, file.Name()), - Size: str.FormatBytes(float64(info.Size())), + Size: tools.FormatBytes(float64(info.Size())), Time: info.ModTime(), }) } @@ -478,9 +478,9 @@ func (r *backupRepo) preCheckPath(to, path string) error { } if app.IsCli { - fmt.Printf("|-目标大小:%s\n", str.FormatBytes(float64(size))) + fmt.Printf("|-目标大小:%s\n", tools.FormatBytes(float64(size))) fmt.Printf("|-目标文件数:%d\n", files) - fmt.Printf("|-备份目录可用空间:%s\n", str.FormatBytes(float64(usage.Free))) + fmt.Printf("|-备份目录可用空间:%s\n", tools.FormatBytes(float64(usage.Free))) fmt.Printf("|-备份目录可用Inode:%d\n", usage.InodesFree) } @@ -504,8 +504,8 @@ func (r *backupRepo) preCheckDB(to string, size int64) error { } if app.IsCli { - fmt.Printf("|-目标大小:%s\n", str.FormatBytes(float64(size))) - fmt.Printf("|-备份目录可用空间:%s\n", str.FormatBytes(float64(usage.Free))) + fmt.Printf("|-目标大小:%s\n", tools.FormatBytes(float64(size))) + fmt.Printf("|-备份目录可用空间:%s\n", tools.FormatBytes(float64(usage.Free))) fmt.Printf("|-备份目录可用Inode:%d\n", usage.InodesFree) } diff --git a/internal/data/container_image.go b/internal/data/container_image.go index 9254ffa3..db9bafbc 100644 --- a/internal/data/container_image.go +++ b/internal/data/container_image.go @@ -14,7 +14,7 @@ import ( "github.com/TheTNB/panel/internal/biz" "github.com/TheTNB/panel/internal/http/request" "github.com/TheTNB/panel/pkg/shell" - "github.com/TheTNB/panel/pkg/str" + "github.com/TheTNB/panel/pkg/tools" "github.com/TheTNB/panel/pkg/types" "github.com/TheTNB/panel/pkg/types/docker/image" ) @@ -57,7 +57,7 @@ func (r *containerImageRepo) List() ([]types.ContainerImage, error) { Containers: item.Containers, RepoTags: item.RepoTags, RepoDigests: item.RepoDigests, - Size: str.FormatBytes(float64(item.Size)), + Size: tools.FormatBytes(float64(item.Size)), Labels: types.MapToKV(item.Labels), CreatedAt: time.Unix(item.Created, 0), }) diff --git a/internal/data/container_volume.go b/internal/data/container_volume.go index 7a82e1b0..7c0deffb 100644 --- a/internal/data/container_volume.go +++ b/internal/data/container_volume.go @@ -14,7 +14,7 @@ import ( "github.com/TheTNB/panel/internal/biz" "github.com/TheTNB/panel/internal/http/request" "github.com/TheTNB/panel/pkg/shell" - "github.com/TheTNB/panel/pkg/str" + "github.com/TheTNB/panel/pkg/tools" "github.com/TheTNB/panel/pkg/types" "github.com/TheTNB/panel/pkg/types/docker/volume" ) @@ -61,7 +61,7 @@ func (r *containerVolumeRepo) List() ([]types.ContainerVolume, error) { Labels: types.MapToKV(item.Labels), Options: types.MapToKV(item.Options), RefCount: item.UsageData.RefCount, - Size: str.FormatBytes(float64(item.UsageData.Size)), + Size: tools.FormatBytes(float64(item.UsageData.Size)), }) } diff --git a/internal/service/file.go b/internal/service/file.go index 38e82559..b3809d9e 100644 --- a/internal/service/file.go +++ b/internal/service/file.go @@ -23,7 +23,7 @@ import ( "github.com/TheTNB/panel/pkg/io" "github.com/TheTNB/panel/pkg/os" "github.com/TheTNB/panel/pkg/shell" - "github.com/TheTNB/panel/pkg/str" + "github.com/TheTNB/panel/pkg/tools" ) type FileService struct { @@ -301,7 +301,7 @@ func (s *FileService) Info(w http.ResponseWriter, r *http.Request) { Success(w, chix.M{ "name": info.Name(), - "size": str.FormatBytes(float64(info.Size())), + "size": tools.FormatBytes(float64(info.Size())), "mode_str": info.Mode().String(), "mode": fmt.Sprintf("%04o", info.Mode().Perm()), "dir": info.IsDir(), @@ -451,7 +451,7 @@ func (s *FileService) formatDir(base string, entries []stdos.DirEntry) []any { paths = append(paths, map[string]any{ "name": info.Name(), "full": filepath.Join(base, info.Name()), - "size": str.FormatBytes(float64(info.Size())), + "size": tools.FormatBytes(float64(info.Size())), "mode_str": info.Mode().String(), "mode": fmt.Sprintf("%04o", info.Mode().Perm()), "owner": os.GetUser(stat.Uid), @@ -477,7 +477,7 @@ func (s *FileService) formatInfo(infos map[string]stdos.FileInfo) []map[string]a paths = append(paths, map[string]any{ "name": info.Name(), "full": path, - "size": str.FormatBytes(float64(info.Size())), + "size": tools.FormatBytes(float64(info.Size())), "mode_str": info.Mode().String(), "mode": fmt.Sprintf("%04o", info.Mode().Perm()), "owner": os.GetUser(stat.Uid), diff --git a/internal/service/file_windows.go b/internal/service/file_windows.go index 02ad5126..835e2111 100644 --- a/internal/service/file_windows.go +++ b/internal/service/file_windows.go @@ -21,7 +21,7 @@ import ( "github.com/TheTNB/panel/internal/http/request" "github.com/TheTNB/panel/pkg/io" "github.com/TheTNB/panel/pkg/shell" - "github.com/TheTNB/panel/pkg/str" + "github.com/TheTNB/panel/pkg/tools" ) type FileService struct { @@ -260,7 +260,7 @@ func (s *FileService) Info(w http.ResponseWriter, r *http.Request) { Success(w, chix.M{ "name": info.Name(), - "size": str.FormatBytes(float64(info.Size())), + "size": tools.FormatBytes(float64(info.Size())), "mode_str": info.Mode().String(), "mode": fmt.Sprintf("%04o", info.Mode().Perm()), "dir": info.IsDir(), @@ -397,7 +397,7 @@ func (s *FileService) formatDir(base string, entries []stdos.DirEntry) []any { paths = append(paths, map[string]any{ "name": info.Name(), "full": filepath.Join(base, info.Name()), - "size": str.FormatBytes(float64(info.Size())), + "size": tools.FormatBytes(float64(info.Size())), "mode_str": info.Mode().String(), "mode": fmt.Sprintf("%04o", info.Mode().Perm()), "owner": "", @@ -422,7 +422,7 @@ func (s *FileService) formatInfo(infos map[string]stdos.FileInfo) []map[string]a paths = append(paths, map[string]any{ "name": info.Name(), "full": path, - "size": str.FormatBytes(float64(info.Size())), + "size": tools.FormatBytes(float64(info.Size())), "mode_str": info.Mode().String(), "mode": fmt.Sprintf("%04o", info.Mode().Perm()), "owner": "", diff --git a/pkg/api/api.go b/pkg/api/api.go index 7b21a756..739b8d2a 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -5,10 +5,9 @@ import ( "log" "time" + "github.com/go-rat/utils/copier" "github.com/go-resty/resty/v2" "github.com/shirou/gopsutil/host" - - "github.com/TheTNB/panel/pkg/copier" ) type API struct { diff --git a/pkg/copier/copier.go b/pkg/copier/copier.go deleted file mode 100644 index 005723b9..00000000 --- a/pkg/copier/copier.go +++ /dev/null @@ -1,18 +0,0 @@ -package copier - -import ( - "encoding/json" - "fmt" -) - -func Copy[T any](from any) (*T, error) { - to := new(T) - b, err := json.Marshal(from) - if err != nil { - return nil, fmt.Errorf("copier: marshal data err: %w", err) - } - if err = json.Unmarshal(b, to); err != nil { - return nil, fmt.Errorf("copier: unmarshal data err: %w", err) - } - return to, nil -} diff --git a/pkg/slice/slice.go b/pkg/slice/slice.go deleted file mode 100644 index cd4d4a11..00000000 --- a/pkg/slice/slice.go +++ /dev/null @@ -1,30 +0,0 @@ -package slice - -import "github.com/spf13/cast" - -// ToAny 将任意类型切片转换为 []any -func ToAny[T any](s []T) []any { - result := make([]any, len(s)) - for i, v := range s { - result[i] = v - } - return result -} - -// ToString 将任意类型切片转换为 []string -func ToString[T any](s []T) []string { - result := make([]string, len(s)) - for i, v := range s { - result[i] = cast.ToString(v) - } - return result -} - -// ToInt 将任意类型切片转换为 []int -func ToInt[T any](s []T) []int { - result := make([]int, len(s)) - for i, v := range s { - result[i] = cast.ToInt(v) - } - return result -} diff --git a/pkg/str/string.go b/pkg/str/string.go deleted file mode 100644 index 286dd25c..00000000 --- a/pkg/str/string.go +++ /dev/null @@ -1,17 +0,0 @@ -package str - -import ( - "fmt" -) - -// FormatBytes 格式化bytes -func FormatBytes(size float64) string { - units := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"} - - i := 0 - for ; size >= 1024 && i < len(units); i++ { - size /= 1024 - } - - return fmt.Sprintf("%.2f %s", size, units[i]) -} diff --git a/pkg/str/string_test.go b/pkg/str/string_test.go deleted file mode 100644 index 26b32cf2..00000000 --- a/pkg/str/string_test.go +++ /dev/null @@ -1,27 +0,0 @@ -package str - -import ( - "testing" - - "github.com/stretchr/testify/suite" -) - -type StringHelperTestSuite struct { - suite.Suite -} - -func TestStringHelperTestSuite(t *testing.T) { - suite.Run(t, &StringHelperTestSuite{}) -} - -func (s *StringHelperTestSuite) TestFormatBytes() { - s.Equal("1.00 B", FormatBytes(1)) - s.Equal("1.00 KB", FormatBytes(1024)) - s.Equal("1.00 MB", FormatBytes(1024*1024)) - s.Equal("1.00 GB", FormatBytes(1024*1024*1024)) - s.Equal("1.00 TB", FormatBytes(1024*1024*1024*1024)) - s.Equal("1.00 PB", FormatBytes(1024*1024*1024*1024*1024)) - s.Equal("1.00 EB", FormatBytes(1024*1024*1024*1024*1024*1024)) - s.Equal("1.00 ZB", FormatBytes(1024*1024*1024*1024*1024*1024*1024)) - s.Equal("1.00 YB", FormatBytes(1024*1024*1024*1024*1024*1024*1024*1024)) -} diff --git a/pkg/tools/tools.go b/pkg/tools/tools.go index 989d5d8f..b8cd9cb7 100644 --- a/pkg/tools/tools.go +++ b/pkg/tools/tools.go @@ -4,6 +4,7 @@ package tools import ( "context" "errors" + "fmt" stdnet "net" "net/http" "slices" @@ -175,3 +176,15 @@ func GetLocalIPv6() (string, error) { local := conn.LocalAddr().(*stdnet.UDPAddr) return local.IP.String(), nil } + +// FormatBytes 格式化bytes +func FormatBytes(size float64) string { + units := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"} + + i := 0 + for ; size >= 1024 && i < len(units); i++ { + size /= 1024 + } + + return fmt.Sprintf("%.2f %s", size, units[i]) +} diff --git a/pkg/tools/tools_test.go b/pkg/tools/tools_test.go index ee9b0b05..dc6e40ec 100644 --- a/pkg/tools/tools_test.go +++ b/pkg/tools/tools_test.go @@ -6,38 +6,50 @@ import ( "github.com/stretchr/testify/suite" ) -type HelperTestSuite struct { +type ToolsTestSuite struct { suite.Suite } -func TestHelperTestSuite(t *testing.T) { - suite.Run(t, &HelperTestSuite{}) +func TestToolsTestSuite(t *testing.T) { + suite.Run(t, &ToolsTestSuite{}) } -func (s *HelperTestSuite) TestGetMonitoringInfo() { +func (s *ToolsTestSuite) TestGetMonitoringInfo() { s.NotNil(CurrentInfo(nil, nil)) } -func (s *HelperTestSuite) TestGetPublicIPv4() { +func (s *ToolsTestSuite) TestGetPublicIPv4() { ip, err := GetPublicIPv4() s.NoError(err) s.NotEmpty(ip) } -func (s *HelperTestSuite) TestGetPublicIPv6() { +func (s *ToolsTestSuite) TestGetPublicIPv6() { ip, err := GetPublicIPv6() s.Error(err) s.Empty(ip) } -func (s *HelperTestSuite) TestGetLocalIPv4() { +func (s *ToolsTestSuite) TestGetLocalIPv4() { ip, err := GetLocalIPv4() s.NoError(err) s.NotEmpty(ip) } -func (s *HelperTestSuite) TestGetLocalIPv6() { +func (s *ToolsTestSuite) TestGetLocalIPv6() { ip, err := GetLocalIPv6() s.Error(err) s.Empty(ip) } + +func (s *ToolsTestSuite) TestFormatBytes() { + s.Equal("1.00 B", FormatBytes(1)) + s.Equal("1.00 KB", FormatBytes(1024)) + s.Equal("1.00 MB", FormatBytes(1024*1024)) + s.Equal("1.00 GB", FormatBytes(1024*1024*1024)) + s.Equal("1.00 TB", FormatBytes(1024*1024*1024*1024)) + s.Equal("1.00 PB", FormatBytes(1024*1024*1024*1024*1024)) + s.Equal("1.00 EB", FormatBytes(1024*1024*1024*1024*1024*1024)) + s.Equal("1.00 ZB", FormatBytes(1024*1024*1024*1024*1024*1024*1024)) + s.Equal("1.00 YB", FormatBytes(1024*1024*1024*1024*1024*1024*1024*1024)) +}