merge: 合并代码20240531
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package parse
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"image/color"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@@ -10,6 +13,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/robfig/cron/v3"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// Number 解析数值型
|
||||
@@ -51,6 +55,8 @@ func Boolean(str any) bool {
|
||||
case float32, float64:
|
||||
num := reflect.ValueOf(str).Float()
|
||||
return num != 0
|
||||
case bool:
|
||||
return str
|
||||
default:
|
||||
return false
|
||||
}
|
||||
@@ -165,3 +171,83 @@ func Color(colorStr string) *color.RGBA {
|
||||
A: 255, // 不透明
|
||||
}
|
||||
}
|
||||
|
||||
// ConvertIPMask 转换IP网络地址掩码 24 -> 255.255.255.0
|
||||
func ConvertIPMask(bits int64) string {
|
||||
if bits < 0 || bits > 32 {
|
||||
return "Invalid Mask Bits"
|
||||
}
|
||||
|
||||
// 构建一个32位的uint32类型掩码,指定前bits位为1,其余为0
|
||||
mask := uint32((1<<bits - 1) << (32 - bits))
|
||||
|
||||
// 将掩码转换为四个八位分组
|
||||
groups := []string{
|
||||
fmt.Sprintf("%d", mask>>24),
|
||||
fmt.Sprintf("%d", (mask>>16)&255),
|
||||
fmt.Sprintf("%d", (mask>>8)&255),
|
||||
fmt.Sprintf("%d", mask&255),
|
||||
}
|
||||
|
||||
// 将分组用点号连接起来形成掩码字符串
|
||||
return strings.Join(groups, ".")
|
||||
}
|
||||
|
||||
// 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 == "" || 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
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(filePath), 0775); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(filePath, dataByte, 0644)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user