feat: update atom framework

This commit is contained in:
Rogee
2025-02-11 15:12:35 +08:00
parent fd8f2ebe03
commit d7acdbb34b
26 changed files with 890 additions and 685 deletions

View File

@@ -1,11 +0,0 @@
package db
import (
"database/sql"
"fmt"
)
func TruncateTable(db *sql.DB, table string) {
_, err := db.Exec(fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY", table))
_ = err
}

View File

@@ -1,13 +0,0 @@
package err
import (
"net/http"
"github.com/rogeecn/gen"
)
var (
BindBodyFailed = gen.NewBusError(http.StatusBadRequest, http.StatusBadRequest, "Body参数错误")
BindQueryFailed = gen.NewBusError(http.StatusBadRequest, http.StatusBadRequest, "Query参数错误")
BindPathFailed = gen.NewBusError(http.StatusBadRequest, http.StatusBadRequest, "Path参数错误: %s")
)

View File

@@ -1,37 +0,0 @@
package fs
import (
"errors"
"os"
)
func PathExists(path string) (bool, error) {
fi, err := os.Stat(path)
if err == nil {
if fi.IsDir() {
return true, nil
}
return false, errors.New("存在同名文件")
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func CreateDir(dirs ...string) (err error) {
var exist bool
for _, v := range dirs {
exist, err = PathExists(v)
if err != nil {
return err
}
if !exist {
err = os.MkdirAll(v, os.ModePerm)
if err != nil {
return err
}
}
}
return err
}

View File

@@ -1,73 +0,0 @@
package fs
import (
"os"
"path/filepath"
"reflect"
"strings"
)
func Move(src, dst string) (err error) {
if dst == "" {
return nil
}
src, err = filepath.Abs(src)
if err != nil {
return err
}
dst, err = filepath.Abs(dst)
if err != nil {
return err
}
revoke := false
dir := filepath.Dir(dst)
Redirect:
_, err = os.Stat(dir)
if err != nil {
err = os.MkdirAll(dir, 0o755)
if err != nil {
return err
}
if !revoke {
revoke = true
goto Redirect
}
}
return os.Rename(src, dst)
}
func Delete(filePath string) error {
return os.RemoveAll(filePath)
}
func TrimSpace(target interface{}) {
t := reflect.TypeOf(target)
if t.Kind() != reflect.Ptr {
return
}
t = t.Elem()
v := reflect.ValueOf(target).Elem()
for i := 0; i < t.NumField(); i++ {
if v.Field(i).Kind() == reflect.String {
v.Field(i).SetString(strings.TrimSpace(v.Field(i).String()))
}
}
}
// FileExist 判断文件是否存在
func FileExist(path string) bool {
fi, err := os.Lstat(path)
if err == nil {
return !fi.IsDir()
}
return !os.IsNotExist(err)
}
func FilePathInfo(file string) (path, name, ext string) {
filename := filepath.Base(file)
path = filepath.Dir(file)
ext = filepath.Ext(filename)
name = strings.TrimSuffix(filename, ext)
return
}

View File

@@ -1,49 +0,0 @@
package fs
import "testing"
func TestFilePathInfo(t *testing.T) {
type args struct {
file string
}
tests := []struct {
name string
args args
wantPath string
wantName string
wantExt string
}{
{
"1.",
args{
file: "/a/bc.ext",
},
"/a",
"bc",
".ext",
},
{
"1.",
args{
file: "/a/c/c/c/bc.ext",
},
"/a/c/c/c",
"bc",
".ext",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotPath, gotName, gotExt := FilePathInfo(tt.args.file)
if gotPath != tt.wantPath {
t.Errorf("FilePathInfo() gotPath = %v, want %v", gotPath, tt.wantPath)
}
if gotName != tt.wantName {
t.Errorf("FilePathInfo() gotName = %v, want %v", gotName, tt.wantName)
}
if gotExt != tt.wantExt {
t.Errorf("FilePathInfo() gotExt = %v, want %v", gotExt, tt.wantExt)
}
})
}
}

View File

@@ -1,17 +0,0 @@
package hash
import (
"golang.org/x/crypto/bcrypt"
)
// BcryptHash 使用 bcrypt 对密码进行加密
func BcryptHash(password string) string {
bytes, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(bytes)
}
// BcryptCheck 对比明文密码和数据库的哈希值
func BcryptCheck(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}

View File

@@ -1,12 +0,0 @@
package hash
import (
"crypto/md5"
"encoding/hex"
)
func MD5(str []byte, b ...byte) string {
h := md5.New()
h.Write(str)
return hex.EncodeToString(h.Sum(b))
}

View File

@@ -1,7 +0,0 @@
package utils
var (
Version = "1.0.0"
BuildAt = "2014-02-02 00:00:00"
GitHash = ""
)

View File

@@ -1,62 +0,0 @@
package opt
import (
"github.com/spf13/viper"
"go.uber.org/dig"
)
type Options struct {
Config *viper.Viper
Prefix string
Name string
Group string
}
type Option func(o *Options)
func New(opts ...Option) *Options {
o := &Options{}
for _, opt := range opts {
opt(o)
}
return o
}
func (o *Options) UnmarshalConfig(config interface{}) error {
return o.Config.UnmarshalKey(o.Prefix, &config)
}
func (o *Options) DiOptions() []dig.ProvideOption {
options := []dig.ProvideOption{}
if o.Name != "" {
options = append(options, dig.Name(o.Name))
}
if o.Group != "" {
options = append(options, dig.Group(o.Group))
}
return options
}
func Config(config *viper.Viper) Option {
return func(o *Options) {
o.Config = config
}
}
func Name(name string) Option {
return func(o *Options) {
o.Name = name
}
}
func Group(group string) Option {
return func(o *Options) {
o.Group = group
}
}
func Prefix(prefix string) Option {
return func(o *Options) {
o.Prefix = prefix
}
}

View File

@@ -1,8 +0,0 @@
package tap
func T[T any](v T, ds ...func(T)) T {
for _, d := range ds {
d(v)
}
return v
}

View File

@@ -1,15 +0,0 @@
package utils
import (
"os"
"strings"
)
func IsInTesting() bool {
for _, arg := range os.Args {
if strings.HasPrefix(arg, "-test.v=") {
return true
}
}
return false
}

View File

@@ -1,131 +0,0 @@
package utils
import (
"archive/zip"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
)
// Unzip 解压
func Unzip(zipFile, destDir string) ([]string, error) {
zipReader, err := zip.OpenReader(zipFile)
paths := []string{}
if err != nil {
return []string{}, err
}
defer zipReader.Close()
for _, f := range zipReader.File {
if strings.Contains(f.Name, "..") {
return []string{}, fmt.Errorf("%s 文件名不合法", f.Name)
}
// nolint
fpath := filepath.Join(destDir, f.Name)
paths = append(paths, fpath)
if f.FileInfo().IsDir() {
err = os.MkdirAll(fpath, os.ModePerm)
if err != nil {
return nil, err
}
} else {
err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm)
if err != nil {
return []string{}, err
}
zipFunc := func(fpath string) error {
var inFile io.ReadCloser
var outFile *os.File
inFile, err = f.Open()
if err != nil {
return err
}
defer inFile.Close()
outFile, err = os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer outFile.Close()
// nolint
_, err = io.Copy(outFile, inFile)
if err != nil {
return err
}
return nil
}
err = zipFunc(fpath)
if err != nil {
return []string{}, err
}
}
}
return paths, nil
}
func ZipFiles(filename string, files []string, oldForm, newForm string) error {
newZipFile, err := os.Create(filename)
if err != nil {
return err
}
defer func() {
_ = newZipFile.Close()
}()
zipWriter := zip.NewWriter(newZipFile)
defer func() {
_ = zipWriter.Close()
}()
// 把files添加到zip中
for _, file := range files {
err = func(file string) error {
var zipFile *os.File
zipFile, err = os.Open(file)
if err != nil {
return err
}
defer zipFile.Close()
// 获取file的基础信息
var info fs.FileInfo
info, err = zipFile.Stat()
if err != nil {
return err
}
var header *zip.FileHeader
header, err = zip.FileInfoHeader(info)
if err != nil {
return err
}
// 使用上面的FileInfoHeader() 就可以把文件保存的路径替换成我们自己想要的了,如下面
header.Name = strings.Replace(file, oldForm, newForm, -1)
// 优化压缩
// 更多参考see http://golang.org/pkg/archive/zip/#pkg-constants
header.Method = zip.Deflate
var writer io.Writer
writer, err = zipWriter.CreateHeader(header)
if err != nil {
return err
}
if _, err = io.Copy(writer, zipFile); err != nil {
return err
}
return nil
}(file)
if err != nil {
return err
}
}
return nil
}