config-patch: backup config
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Dec 15, 2017 at 20:55 UTC
0ff9b24a32fcaa33a4d11f087542704c38daa904
5 files changed
+48
core/commands/config.go
+5
@@ -372,6 +372,11 @@ func transformConfig(configRoot string, transformer config.Transformer) error {
372
return err
373
}
374
375
+ _, err = r.BackupConfig("profile-")
376
+ if err != nil {
377
+ return err
378
+ }
379
+
380
return r.SetConfig(cfg)
381
}
382
repo/fsrepo/fsrepo.go
+26
@@ -480,6 +480,32 @@ func (r *FSRepo) FileManager() *filestore.FileManager {
480
return r.filemgr
481
}
482
483
+func (r *FSRepo) BackupConfig(prefix string) (string, error) {
484
+ temp, err := ioutil.TempFile(r.path, "config-"+prefix)
485
+ if err != nil {
486
+ return "", err
487
+ }
488
+ defer temp.Close()
489
+
490
+ configFilename, err := config.Filename(r.path)
491
+ if err != nil {
492
+ return "", err
493
+ }
494
+
495
+ orig, err := os.OpenFile(configFilename, os.O_RDONLY, 0600)
496
+ if err != nil {
497
+ return "", err
498
+ }
499
+ defer orig.Close()
500
+
501
+ _, err = io.Copy(temp, orig)
502
+ if err != nil {
503
+ return "", err
504
+ }
505
+
506
+ return orig.Name(), nil
507
+}
508
+
509
// setConfigUnsynced is for private use.
510
func (r *FSRepo) setConfigUnsynced(updated *config.Config) error {
511
configFilename, err := config.Filename(r.path)
repo/mock.go
+4
@@ -28,6 +28,10 @@ func (m *Mock) SetConfig(updated *config.Config) error {
28
return nil
29
}
30
31
+func (m *Mock) BackupConfig(prefix string) (string, error) {
32
+ return "", errTODO
33
+}
34
+
35
func (m *Mock) SetConfigKey(key string, value interface{}) error {
36
return errTODO
37
}
repo/repo.go
+1
@@ -18,6 +18,7 @@ var (
18
19
type Repo interface {
20
Config() (*config.Config, error)
21
+ BackupConfig(prefix string) (string, error)
22
SetConfig(*config.Config) error
23
24
SetConfigKey(key string, value interface{}) error
test/sharness/t0021-config.sh
+12
@@ -183,10 +183,18 @@ test_config_cmd() {
183
test $(cat actual_config | wc -l) = 1
184
'
185
186
+ test_expect_success "copy ipfs config" '
187
+ cp "$IPFS_PATH/config" before_patch
188
+ '
189
+
190
test_expect_success "'ipfs config profile apply server' works" '
191
ipfs config profile apply server
192
'
193
194
+ test_expect_success "backup was created and looks good" '
195
+ test_cmp "$(find "$IPFS_PATH" -name "config-profile*")" before_patch
196
+ '
197
+
198
test_expect_success "'ipfs config Swarm.AddrFilters' looks good with server profile" '
199
ipfs config Swarm.AddrFilters > actual_config &&
200
test $(cat actual_config | wc -l) = 17
@@ -209,6 +217,10 @@ test_config_cmd() {
217
# won't work as it changes datastore definition, which makes ipfs not launch
218
# without converting first
219
# test_profile_apply_revert badgerds
220
+
221
+ test_expect_success "cleanup config backups" '
222
+ find "$IPFS_PATH" -name "config-profile*" -exec rm {} \;
223
+ '
224
}
225
226
test_init_ipfs