Skip to content

Commit

Permalink
config-patch: backup config
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Dec 15, 2017
1 parent 2514c74 commit bebdd2b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,11 @@ func transformConfig(configRoot string, transformer config.Transformer) error {
return err
}

_, err = r.BackupConfig("profile-")
if err != nil {
return err
}

return r.SetConfig(cfg)
}

Expand Down
26 changes: 26 additions & 0 deletions repo/fsrepo/fsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,32 @@ func (r *FSRepo) FileManager() *filestore.FileManager {
return r.filemgr
}

func (r *FSRepo) BackupConfig(prefix string) (string, error) {
temp, err := ioutil.TempFile(r.path, "config-"+prefix)
if err != nil {
return "", err
}
defer temp.Close()

configFilename, err := config.Filename(r.path)
if err != nil {
return "", err
}

orig, err := os.OpenFile(configFilename, os.O_RDONLY, 0600)
if err != nil {
return "", err
}
defer orig.Close()

_, err = io.Copy(temp, orig)
if err != nil {
return "", err
}

return orig.Name(), nil
}

// setConfigUnsynced is for private use.
func (r *FSRepo) setConfigUnsynced(updated *config.Config) error {
configFilename, err := config.Filename(r.path)
Expand Down
4 changes: 4 additions & 0 deletions repo/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (m *Mock) SetConfig(updated *config.Config) error {
return nil
}

func (m *Mock) BackupConfig(prefix string) (string, error) {
return "config-" + prefix + "-backup", nil
}

func (m *Mock) SetConfigKey(key string, value interface{}) error {
return errTODO
}
Expand Down
1 change: 1 addition & 0 deletions repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (

type Repo interface {
Config() (*config.Config, error)
BackupConfig(prefix string) (string, error)
SetConfig(*config.Config) error

SetConfigKey(key string, value interface{}) error
Expand Down
12 changes: 12 additions & 0 deletions test/sharness/t0021-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,18 @@ test_config_cmd() {
test $(cat actual_config | wc -l) = 1
'

test_expect_success "copy ipfs config" '
cp "$IPFS_PATH/config" before_patch
'

test_expect_success "'ipfs config profile apply server' works" '
ipfs config profile apply server
'

test_expect_success "backup was created and looks good" '
test_cmp "$(find "$IPFS_PATH" -name "config-profile*")" before_patch
'

test_expect_success "'ipfs config Swarm.AddrFilters' looks good with server profile" '
ipfs config Swarm.AddrFilters > actual_config &&
test $(cat actual_config | wc -l) = 17
Expand All @@ -209,6 +217,10 @@ test_config_cmd() {
# won't work as it changes datastore definition, which makes ipfs not launch
# without converting first
# test_profile_apply_revert badgerds

test_expect_success "cleanup config backups" '
find "$IPFS_PATH" -name "config-profile*" -exec rm {} \;
'
}

test_init_ipfs
Expand Down

0 comments on commit bebdd2b

Please sign in to comment.