Files
EveryPublish/client/core/internal/config/config.go
T

46 lines
1.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package config
import (
"encoding/json"
"os"
"path/filepath"
)
// Config 客户端配置(data/config.json)
type Config struct {
Server string `json:"server"`
DeviceID uint64 `json:"deviceId"`
Name string `json:"name"`
Proxy string `json:"proxy"`
SimMS int `json:"simMs"`
FakeAll bool `json:"fakeAll"` // 全平台模拟执行(无真实账号联调用)
}
// Default 默认配置
func Default() *Config {
return &Config{Server: "http://127.0.0.1:8090", SimMS: 50}
}
// Load 读取配置(不存在则返回默认)
func Load(dir string) (*Config, error) {
cfg := Default()
raw, err := os.ReadFile(filepath.Join(dir, "config.json"))
if err != nil {
if os.IsNotExist(err) {
return cfg, nil
}
return nil, err
}
_ = json.Unmarshal(raw, cfg)
return cfg, nil
}
// Save 持久化配置
func (c *Config) Save(dir string) error {
if err := os.MkdirAll(dir, 0o700); err != nil {
return err
}
raw, _ := json.MarshalIndent(c, "", " ")
return os.WriteFile(filepath.Join(dir, "config.json"), raw, 0o600)
}