From d5323223f4acdf551b70635fcba16801357b4361 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 31 Jul 2016 16:27:36 +0200 Subject: [PATCH] Change repository Init() function to allow better testing --- src/restic/repository/config.go | 11 +++++------ src/restic/repository/config_test.go | 4 +++- src/restic/repository/repository.go | 14 +++++++++++++- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/restic/repository/config.go b/src/restic/repository/config.go index a41517f3c..abe1277ed 100644 --- a/src/restic/repository/config.go +++ b/src/restic/repository/config.go @@ -7,9 +7,10 @@ import ( "errors" "io" - "github.com/restic/chunker" "restic/backend" "restic/debug" + + "github.com/restic/chunker" ) // Config contains the configuration for a repository. @@ -37,8 +38,8 @@ type JSONUnpackedLoader interface { } // CreateConfig creates a config file with a randomly selected polynomial and -// ID and saves the config in the repository. -func CreateConfig(r JSONUnpackedSaver) (Config, error) { +// ID. +func CreateConfig() (Config, error) { var ( err error cfg Config @@ -59,9 +60,7 @@ func CreateConfig(r JSONUnpackedSaver) (Config, error) { cfg.Version = RepoVersion debug.Log("Repo.CreateConfig", "New config: %#v", cfg) - - _, err = r.SaveJSONUnpacked(backend.Config, cfg) - return cfg, err + return cfg, nil } // LoadConfig returns loads, checks and returns the config for a repository. diff --git a/src/restic/repository/config_test.go b/src/restic/repository/config_test.go index 10fc61d94..71f2fd810 100644 --- a/src/restic/repository/config_test.go +++ b/src/restic/repository/config_test.go @@ -32,9 +32,11 @@ func TestConfig(t *testing.T) { return backend.ID{}, nil } - cfg1, err := repository.CreateConfig(saver(save)) + cfg1, err := repository.CreateConfig() OK(t, err) + _, err = saver(save).SaveJSONUnpacked(backend.Config, cfg1) + load := func(tpe backend.Type, id backend.ID, arg interface{}) error { Assert(t, tpe == backend.Config, "wrong backend type: got %v, wanted %v", diff --git a/src/restic/repository/repository.go b/src/restic/repository/repository.go index aa75d322e..bdda7fe48 100644 --- a/src/restic/repository/repository.go +++ b/src/restic/repository/repository.go @@ -416,6 +416,17 @@ func (r *Repository) Init(password string) error { return errors.New("repository master key and config already initialized") } + cfg, err := CreateConfig() + if err != nil { + return err + } + + return r.init(password, cfg) +} + +// init creates a new master key with the supplied password and uses it to save +// the config into the repo. +func (r *Repository) init(password string, cfg Config) error { key, err := createMasterKey(r, password) if err != nil { return err @@ -424,7 +435,8 @@ func (r *Repository) Init(password string) error { r.key = key.master r.packerManager.key = key.master r.keyName = key.Name() - r.Config, err = CreateConfig(r) + r.Config = cfg + _, err = r.SaveJSONUnpacked(backend.Config, cfg) return err }