-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
113 lines (102 loc) · 2.76 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/adrg/xdg"
)
type Config struct {
Port string `json:"port"` // http服务端口
CsrfDomains []string `json:"csrf_domain"` // CSRF 域名列表
DraftRootPath string `json:"draft_root_path"` // 草稿根目录
}
func DefaultConfig() Config {
defaultConfig := Config{
Port: ":9507",
CsrfDomains: []string{"https://keyframeai.top"},
DraftRootPath: "",
}
return defaultConfig
}
type ConfigStore struct {
configPath string
currentVersion string
}
func NewConfigStore() (*ConfigStore, error) {
configFilePath, err := xdg.ConfigFile("jianyingpro-batch-keyframe-copilot/config.json")
if err != nil {
return nil, fmt.Errorf("could not resolve path for config file: %w", err)
}
return &ConfigStore{
configPath: configFilePath,
currentVersion: "0.1.2",
}, nil
}
func (s *ConfigStore) Config() (Config, error) {
// Reload the default configuration file if the version is upgraded
versionLockFilePath := filepath.Join(filepath.Dir(s.configPath), s.currentVersion)
_, err := os.Stat(versionLockFilePath)
if os.IsNotExist(err) {
// create a file
os.Create(versionLockFilePath)
s.SaveConfig(DefaultConfig())
return DefaultConfig(), nil
}
// Create a default configuration file if the file does not exist
_, err = os.Stat(s.configPath)
if os.IsNotExist(err) {
s.SaveConfig(DefaultConfig())
return DefaultConfig(), nil
}
dir, fileName := filepath.Split(s.configPath)
if len(dir) == 0 {
dir = "."
}
buf, err := fs.ReadFile(os.DirFS(dir), fileName)
if err != nil {
return Config{}, fmt.Errorf("could not read the configuration file: %w", err)
}
if len(buf) == 0 {
return DefaultConfig(), nil
}
cfg := Config{}
if err := json.Unmarshal(buf, &cfg); err != nil {
return Config{}, fmt.Errorf("configuration file does not have a valid format: %w", err)
}
return cfg, nil
}
func (s *ConfigStore) SaveConfig(cfg Config) error {
data, err := json.Marshal(cfg)
if err != nil {
return fmt.Errorf("could not marshal config data: %w", err)
}
dir, _ := filepath.Split(s.configPath)
if len(dir) == 0 {
dir = "."
}
err = os.MkdirAll(dir, 0755)
if err != nil {
return fmt.Errorf("could not create config directory: %w", err)
}
err = os.WriteFile(s.configPath, data, 0644)
if err != nil {
return fmt.Errorf("could not write config file: %w", err)
}
return nil
}
// func main() {
// store, err := NewConfigStore()
// if err != nil {
// fmt.Printf("could not initialize the config store: %v\n", err)
// return
// }
// fmt.Println(store.configPath)
// cfg, err := store.Config()
// if err != nil {
// fmt.Printf("could not retrieve the configuration: %v\n", err)
// return
// }
// fmt.Printf("config: %v\n", cfg)
// }