-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
188 lines (167 loc) · 4.03 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
log "github.com/tengfei-xy/go-log"
tools "github.com/tengfei-xy/go-tools"
"gopkg.in/yaml.v3"
)
func initConfig(config string) (Config, error) {
var c Config
f := getAppPath()
file := filepath.Join(f, config)
log.Infof("读取配置文件:%s", file)
if !tools.FileExist(file) {
return Config{}, configGenerate(file)
}
data, err := tools.FileRead(file)
if err != nil {
return Config{}, err
}
if runtime.GOOS == "windows" {
data, err = tools.StringGBKToUTF_8(data)
if err != nil {
return Config{}, err
}
}
if err := yaml.Unmarshal(data, &c); err != nil {
return Config{}, err
}
return c, c.check()
}
func (c *Config) check() error {
if c.Cookie == "" {
return fmt.Errorf("配置文件中的cookie值为空")
}
_, err := os.Stat(c.BackupPath)
if os.IsNotExist(err) {
return fmt.Errorf("路径不存在 %s", c.BackupPath)
} else if err != nil {
return fmt.Errorf("路径:%s 发生错误: %v ", c.BackupPath, err)
}
return nil
}
func (c *Config) getIgnoreWorkspace(ws string) int {
for i, j := range c.Ignore {
if j.Name == ws {
return i
}
}
return -1
}
func (c *Config) getIgnoreSubspace(freePlan bool, ws int, sp string) int {
if ws == -1 {
return -1
}
if freePlan {
return 0
}
for i, j := range c.Ignore[ws].Subspaces {
if j.Name == sp {
return i
}
}
return -1
}
func (c *Config) isIgnoreSubspace(ws int, subspaceName string) bool {
if ws == -1 {
return false
}
for _, subspace := range c.Ignore[ws].Subspaces {
if subspace.Name == "*" {
return true
}
}
for _, subspace := range c.Ignore[ws].Subspaces {
if subspace.Name == subspaceName && len(subspaceName) != 0 {
return true
}
}
return false
}
func (c *Config) isIgnorePage(ws int, sb int, pageName string) bool {
if sb == -1 || ws == -1 {
return false
}
for _, page := range c.Ignore[ws].Subspaces[sb].Pages {
if page.Name == "*" {
return true
}
}
for _, page := range c.Ignore[ws].Subspaces[sb].Pages {
if page.Name == pageName {
return true
}
}
return false
}
func (c *Config) hasHtml() bool {
for _, v := range c.ExportType {
if strings.ToLower(v) == "html" {
return true
}
}
return false
}
func (c *Config) hasMarkdown() bool {
for _, v := range c.ExportType {
v = strings.ToLower(v)
if v == "markdown" || v == "md" {
return true
}
}
return false
}
func (c *Config) addTimeBackupPath() {
config.BackupPath = filepath.Join(config.BackupPath, timeGetChineseString())
}
func configGenerate(file string) error {
var c Config
c.BackupPath = getAppPath()
c.Ignore = make([]Workspace, 2)
c.ExportType = []string{"html", "markdown"}
for i := range c.Ignore {
c.Ignore[i].Name = "工作区名"
c.Ignore[i].Subspaces = make([]Subspace, 2)
for k := range c.Ignore[i].Subspaces {
c.Ignore[i].Subspaces[k].Name = "子空间名"
c.Ignore[i].Subspaces[k].Pages = make([]Page, 2)
for q := range c.Ignore[i].Subspaces[k].Pages {
c.Ignore[i].Subspaces[k].Pages[q].Name = "页面名"
}
}
}
data, err := yaml.Marshal(c)
if err != nil {
return fmt.Errorf("Unmarshal: %v", err)
}
if err := tools.FileWrite(file, data); err != nil {
return fmt.Errorf("%s", err)
}
fmt.Println("对于团队版(家庭版),子空间是必须的,需要填写此参数,subspace项可以有多个")
fmt.Println("对于个人版,子空间是默认第一个的,无需(或任意)填写此参数")
return fmt.Errorf("已创建新配置文件,请修改后重新运行程序 位置:%s", file)
}
type Config struct {
Cookie string `yaml:"cookie"`
// 作为外部文件使用的变量
BackupPath string `yaml:"backupBackupDir"`
ExportType []string `yaml:"exportType"`
Ignore []Workspace `yaml:"ignore"`
// 作为绝对路径的、内部程序使用的变量
backupPath string
}
type Workspace struct {
Name string `yaml:"workspace"`
Subspaces []Subspace `yaml:"subspace"`
}
type Subspace struct {
Name string `yaml:"name"`
Pages []Page `yaml:"page"`
}
type Page struct {
Name string `yaml:"name"`
}