123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package utils
- import (
- "bufio"
- "encoding/json"
- "errors"
- "fmt"
- "os"
- )
- func GetVersion() (string, error) {
- data, err := os.ReadFile(PackConfPath)
- if err != nil {
- fmt.Println(err)
- return "", errors.New("读取文件失败")
- }
- packConf := make(map[string]interface{})
- err = json.Unmarshal(data, &packConf)
- if err != nil {
- fmt.Println(err)
- return "", errors.New("解析文件失败")
- }
- if _, ok := packConf["version"]; !ok {
- return "", errors.New("没有版本信息")
- }
- return packConf["version"].(string), nil
- }
- func ReadUpConf() (map[string]*UpConfItem, error) {
- conf, err := os.Open(ConfigPath)
- if err != nil {
- fmt.Println(err)
- return nil, errors.New("打开文件失败")
- }
- reader := bufio.NewReader(conf)
- decoder := json.NewDecoder(reader)
- config := make(map[string]*UpConfItem)
- err = decoder.Decode(&config)
- if err != nil {
- return nil, errors.New("解析失败")
- }
- return config, nil
- }
- func ReadAppConfig() (*App, error) {
- conf, err := os.Open(AppConfPath)
- if err != nil {
- fmt.Println(err)
- return nil, errors.New("打开文件失败")
- }
- reader := bufio.NewReader(conf)
- decoder := json.NewDecoder(reader)
- appConf := &App{}
- err = decoder.Decode(appConf)
- if err != nil {
- return nil, errors.New("解析失败")
- }
- return appConf, nil
- }
- func WriteAppConfig(config *App, dest string) error {
- data, err := json.MarshalIndent(config, "", " ")
- if err != nil {
- fmt.Println(err)
- return errors.New("json marshal err")
- }
- err = os.WriteFile(dest, data, 0777)
- if err != nil {
- fmt.Println(err)
- return errors.New("写入配置文件出错")
- }
- return nil
- }
- func WriteVersionDb(version *Version, dest string) error {
- data, err := json.MarshalIndent(version, "", " ")
- if err != nil {
- fmt.Println(err)
- return errors.New("json marshal err")
- }
- err = os.WriteFile(dest, data, 0777)
- if err != nil {
- fmt.Println(err)
- return errors.New("写入配置文件出错")
- }
- return nil
- }
|