12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- 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 ReadConfig() (*Uploader, 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 := &Uploader{}
- 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
- }
|