-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconfiguration.go
266 lines (240 loc) · 7.65 KB
/
configuration.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"embed"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"time"
"gopkg.in/yaml.v3"
)
var (
BasePathFlag = flag.String("basepath", "/ipfs-sync/", "relative MFS directory path")
BasePath string
EndPointFlag = flag.String("endpoint", "http://127.0.0.1:5001", "node to connect to over HTTP")
EndPoint string
DirKeysFlag = new(SyncDirs)
DirKeys []*DirKey
SyncTimeFlag = flag.Duration("sync", time.Second*10, "time to sleep between IPNS syncs (ex: 120s)")
SyncTime time.Duration
TimeoutTimeFlag = flag.Duration("timeout", time.Second*30, "longest time to wait for API calls like 'version' and 'files/mkdir' (ex: 60s)")
TimeoutTime time.Duration
ConfigFileFlag = flag.String("config", getHomeDir()+".ipfs-sync.yaml", "path to config file to use")
ConfigFile string
IgnoreFlag = new(IgnoreStruct)
Ignore []string
LicenseFlag = flag.Bool("copyright", false, "display copyright and exit")
DBPathFlag = flag.String("db", getHomeDir()+".ipfs-sync.db", `path to file where db should be stored`)
DBPath string
IgnoreHiddenFlag = flag.Bool("ignorehidden", false, `ignore anything prefixed with "."`)
IgnoreHidden bool
VersionFlag = flag.Bool("version", false, "display version and exit")
VerboseFlag = flag.Bool("v", false, "display verbose output")
Verbose bool
EstuaryAPIKey string // don't make this a flag
VerifyFilestoreFlag = flag.Bool("verify", false, "verify filestore on startup (not recommended unless you're having issues)")
VerifyFilestore bool
version string // passed by -ldflags
)
func init() {
flag.Var(DirKeysFlag, "dirs", `set the dirs to monitor in json format like: [{"ID":"Example1", "Dir":"/home/user/Documents/", "Nocopy": false},{"ID":"Example2", "Dir":"/home/user/Pictures/", "Nocopy": false}]`)
flag.Var(IgnoreFlag, "ignore", `set the suffixes to ignore (default: ["kate-swp", "swp", "part", "crdownload"])`)
}
func getHomeDir() string {
homeDir, _ := os.UserHomeDir()
return homeDir + string(os.PathSeparator)
}
//go:embed config.yaml.sample
var content embed.FS
// DirKey used for keeping track of directories, and it's used in the `dirs` config paramerter.
type DirKey struct {
// config values
ID string `json:"ID" yaml:"ID"`
Dir string `yaml:"Dir"`
Nocopy bool `yaml:"Nocopy"`
DontHash bool `yaml:"DontHash"`
Pin bool `yaml:"Pin"`
Estuary bool `yaml:"Estuary"`
// probably best to let this be managed automatically
CID string
MFSPath string
}
// SyncDirs is used for reading what the user specifies for which directories they'd like to sync.
type SyncDirs struct {
DirKeys []*DirKey
json string
}
// Set takes a JSON string and marshals it into `sd`.
func (sd *SyncDirs) Set(str string) error {
sd.DirKeys = make([]*DirKey, 0, 1)
sd.json = str
return json.Unmarshal([]byte(str), &sd.DirKeys)
}
// String returns the raw JSON used to build `sd`.
func (sd *SyncDirs) String() string {
return sd.json
}
// IgnoreStruct is used for reading what the user specifies for which extensions they'd like to ignore.
type IgnoreStruct struct {
Ignores []string
json string
}
// Set takes a JSON string and marshals it into `ig`.
func (ig *IgnoreStruct) Set(str string) error {
ig.Ignores = make([]string, 0, 1)
ig.json = str
return json.Unmarshal([]byte(str), &ig.Ignores)
}
// String returns the raw JSON used to build `ig`.
func (ig *IgnoreStruct) String() string {
return ig.json
}
// ConfigFileStruct is used for loading information from the config file.
type ConfigFileStruct struct {
BasePath string `yaml:"BasePath"`
EndPoint string `yaml:"EndPoint"`
Dirs []*DirKey `yaml:"Dirs"`
Sync string `yaml:"Sync"`
Ignore []string `yaml:"Ignore"`
DB string `yaml:"DB"`
IgnoreHidden bool `yaml:"IgnoreHidden"`
Timeout string `yaml:"Timeout"`
EstuaryAPIKey string `yaml:"EstuaryAPIKey"`
VerifyFilestore bool `yaml:"VerifyFilestore"`
}
func loadConfig(path string) {
log.Println("Loading config file", path)
cfgFile, err := os.Open(path)
if err != nil {
log.Println("Config file not found, generating...")
defaultconfig, _ := content.ReadFile("config.yaml.sample")
err = ioutil.WriteFile(path, defaultconfig, 0644)
if err != nil {
log.Println("[ERROR] Error loading config file:", err)
log.Println("[ERROR] Skipping config file...")
return
}
cfgFile, err = os.Open(path)
if err != nil {
log.Println("[ERROR] Error loading config file:", err)
log.Println("[ERROR] Skipping config file...")
return
}
}
defer cfgFile.Close()
cfgTxt, _ := ioutil.ReadAll(cfgFile)
cfg := new(ConfigFileStruct)
err = yaml.Unmarshal(cfgTxt, cfg)
if err != nil {
log.Println("[ERROR] Error decoding config file:", err)
log.Println("[ERROR] Skipping config file...")
return
}
if cfg.BasePath != "" {
BasePath = cfg.BasePath
}
if cfg.EndPoint != "" {
EndPoint = cfg.EndPoint
}
if len(cfg.Dirs) > 0 {
DirKeys = cfg.Dirs
}
if cfg.Sync != "" {
tsTime, err := time.ParseDuration(cfg.Sync)
if err != nil {
log.Println("[ERROR] Error processing sync in config file:", err)
} else {
SyncTime = tsTime
}
}
if cfg.Timeout != "" {
tsTime, err := time.ParseDuration(cfg.Timeout)
if err != nil {
log.Println("[ERROR] Error processing timeout in config file:", err)
} else {
TimeoutTime = tsTime
}
}
if cfg.DB != "" {
DBPath = cfg.DB
}
IgnoreHidden = cfg.IgnoreHidden
EstuaryAPIKey = cfg.EstuaryAPIKey
VerifyFilestore = cfg.VerifyFilestore
}
// Process flags, and load config.
func ProcessFlags() {
flag.Parse()
if *LicenseFlag {
fmt.Println("Copyright © 2020, The ipfs-sync Contributors. All rights reserved.")
fmt.Println("BSD 3-Clause “New” or “Revised” License.")
fmt.Println("License available at: https://github.com/TheDiscordian/ipfs-sync/blob/master/LICENSE")
os.Exit(0)
}
if *VersionFlag {
if version == "" {
version = "devel"
}
fmt.Printf("ipfs-sync %s\n", version)
os.Exit(0)
}
log.Println("ipfs-sync starting up...")
ConfigFile = *ConfigFileFlag
if ConfigFile != "" {
loadConfig(ConfigFile)
}
if len(DirKeysFlag.DirKeys) > 0 {
DirKeys = DirKeysFlag.DirKeys
}
// Process Dir
if len(DirKeys) == 0 {
log.Fatalln(`dirs field is required as flag, or in config.`)
} else { // Check if Dir entries are at least somewhat valid.
for _, dk := range DirKeys {
if len(dk.Dir) == 0 {
log.Fatalln("Dir entry path cannot be empty. (ID:", dk.ID, ")")
}
// Check if trailing "/" exists, if not, append it.
if dk.Dir[len(dk.Dir)-1] != os.PathSeparator {
dk.Dir = dk.Dir + string(os.PathSeparator)
}
}
}
if *BasePathFlag != "/ipfs-sync/" || BasePath == "" {
BasePath = *BasePathFlag
}
if *EndPointFlag != "http://127.0.0.1:5001" || EndPoint == "" {
EndPoint = *EndPointFlag
}
// Ignore has no defaults so we need to set them here (if nothing else set it)
if len(IgnoreFlag.Ignores) > 0 {
Ignore = IgnoreFlag.Ignores
} else if len(Ignore) == 0 {
Ignore = []string{"kate-swp", "swp", "part", "crdownload"}
}
if *DBPathFlag != "" {
DBPath = *DBPathFlag
}
if DBPath != "" {
InitDB(DBPath)
}
if *SyncTimeFlag != time.Second*10 || SyncTime == 0 {
SyncTime = *SyncTimeFlag
}
if *TimeoutTimeFlag != time.Second*30 || TimeoutTime == 0 {
TimeoutTime = *TimeoutTimeFlag
}
if *IgnoreHiddenFlag {
IgnoreHidden = true
}
if *VerifyFilestoreFlag {
VerifyFilestore = true
}
Verbose = *VerboseFlag
_, err := doRequest(TimeoutTime, "version")
if err != nil {
log.Fatalln("Failed to connect to end point:", err)
}
}