2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 10:17:17 +08:00

feat: 移动部分方法到utils

This commit is contained in:
耗子
2024-11-04 03:04:34 +08:00
parent bf8c832b32
commit e0daee4766
18 changed files with 73 additions and 144 deletions

View File

@@ -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 {

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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])
}

View File

@@ -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))
}

View File

@@ -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])
}

View File

@@ -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))
}