Skip to content

Commit 917f232

Browse files
committed
config-patch: backup config
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent 2514c74 commit 917f232

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

core/commands/config.go

+5
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,11 @@ func transformConfig(configRoot string, transformer config.Transformer) error {
372372
return err
373373
}
374374

375+
_, err = r.BackupConfig("profile-")
376+
if err != nil {
377+
return err
378+
}
379+
375380
return r.SetConfig(cfg)
376381
}
377382

repo/fsrepo/fsrepo.go

+26
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,32 @@ func (r *FSRepo) FileManager() *filestore.FileManager {
480480
return r.filemgr
481481
}
482482

483+
func (m *FSRepo) BackupConfig(prefix string) (string, error) {
484+
temp, err := ioutil.TempFile(m.path, "config-"+prefix)
485+
if err != nil {
486+
return "", err
487+
}
488+
defer temp.Close()
489+
490+
configFilename, err := config.Filename(m.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+
483509
// setConfigUnsynced is for private use.
484510
func (r *FSRepo) setConfigUnsynced(updated *config.Config) error {
485511
configFilename, err := config.Filename(r.path)

repo/mock.go

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ func (m *Mock) SetConfig(updated *config.Config) error {
2828
return nil
2929
}
3030

31+
func (m *Mock) BackupConfig(prefix string) (string, error) {
32+
return "config-" + prefix + "-backup", nil
33+
}
34+
3135
func (m *Mock) SetConfigKey(key string, value interface{}) error {
3236
return errTODO
3337
}

repo/repo.go

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var (
1818

1919
type Repo interface {
2020
Config() (*config.Config, error)
21+
BackupConfig(prefix string) (string, error)
2122
SetConfig(*config.Config) error
2223

2324
SetConfigKey(key string, value interface{}) error

test/sharness/t0021-config.sh

+12
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,18 @@ test_config_cmd() {
183183
test $(cat actual_config | wc -l) = 1
184184
'
185185

186+
test_expect_success "copy ipfs config" '
187+
cp "$IPFS_PATH/config" before_patch
188+
'
189+
186190
test_expect_success "'ipfs config profile apply server' works" '
187191
ipfs config profile apply server
188192
'
189193

194+
test_expect_success "backup was created and looks good" '
195+
test_cmp "$(find "$IPFS_PATH" -name "config-profile*")" before_patch
196+
'
197+
190198
test_expect_success "'ipfs config Swarm.AddrFilters' looks good with server profile" '
191199
ipfs config Swarm.AddrFilters > actual_config &&
192200
test $(cat actual_config | wc -l) = 17
@@ -209,6 +217,10 @@ test_config_cmd() {
209217
# won't work as it changes datastore definition, which makes ipfs not launch
210218
# without converting first
211219
# test_profile_apply_revert badgerds
220+
221+
test_expect_success "cleanup config backups" '
222+
find "$IPFS_PATH" -name "config-profile*" -exec rm {} \;
223+
'
212224
}
213225

214226
test_init_ipfs

0 commit comments

Comments
 (0)