feat: 新增fstp文件传输工具包

This commit is contained in:
TsMask
2024-04-11 17:04:23 +08:00
parent 9c8c12e443
commit 513a5bb5fe
4 changed files with 272 additions and 11 deletions

View File

@@ -1,8 +1,10 @@
package parse
import (
"encoding/json"
"fmt"
"image/color"
"os"
"reflect"
"regexp"
"strconv"
@@ -10,6 +12,7 @@ import (
"time"
"github.com/robfig/cron/v3"
"gopkg.in/yaml.v3"
)
// Number 解析数值型
@@ -165,3 +168,58 @@ func Color(colorStr string) *color.RGBA {
A: 255, // 不透明
}
}
// ConvertConfigToMap 将配置内容转换为Map结构数据
//
// configType 类型支持txt json yaml yml
func ConvertConfigToMap(configType, content string) (map[string]any, error) {
// 类型支持viper.SupportedExts
// config := viper.New()
// config.SetConfigType(configType)
// err := config.ReadConfig(bytes.NewBuffer([]byte(content)))
// return config.AllSettings(), err
var configMap map[string]interface{}
var err error
if configType == "txt" {
configMap = map[string]interface{}{
"txt": content,
}
}
if configType == "yaml" || configType == "yml" {
err = yaml.Unmarshal([]byte(content), &configMap)
}
if configType == "json" {
err = json.Unmarshal([]byte(content), &configMap)
}
return configMap, err
}
// ConvertConfigToFile 将数据写入到指定文件内
//
// configType 类型支持txt json yaml yml
func ConvertConfigToFile(configType, filePath string, data any) error {
// viper.SupportedExts
// config := viper.New()
// config.SetConfigType(configType)
// for key, value := range mapData {
// config.Set(key, value)
// }
// return config.WriteConfigAs(filePath)
var dataByte []byte
var err error
if configType == "" || configType == "txt" {
dataByte = []byte(data.(string))
}
if configType == "yaml" || configType == "yml" {
dataByte, err = yaml.Marshal(data)
}
if configType == "json" {
dataByte, err = json.Marshal(data)
}
if err != nil {
return err
}
return os.WriteFile(filePath, dataByte, 0644)
}