From f53d33ba34ae93383f037605d583d18cc3c58ff6 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Thu, 2 Mar 2017 15:45:35 +0100 Subject: [PATCH] Make ArchiveReader a struct --- src/cmds/restic/cmd_backup.go | 8 +++++- src/restic/archiver/archive_reader.go | 16 ++++++++--- src/restic/archiver/archive_reader_test.go | 32 +++++++++++++++++++--- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/cmds/restic/cmd_backup.go b/src/cmds/restic/cmd_backup.go index 3d2514ed3..6c259ffa0 100644 --- a/src/cmds/restic/cmd_backup.go +++ b/src/cmds/restic/cmd_backup.go @@ -272,7 +272,13 @@ func readBackupFromStdin(opts BackupOptions, gopts GlobalOptions, args []string) return err } - _, id, err := archiver.ArchiveReader(repo, newArchiveStdinProgress(gopts), os.Stdin, opts.StdinFilename, opts.Tags, opts.Hostname) + r := &archiver.Reader{ + Repository: repo, + Tags: opts.Tags, + Hostname: opts.Hostname, + } + + _, id, err := r.Archive(opts.StdinFilename, os.Stdin, newArchiveStdinProgress(gopts)) if err != nil { return err } diff --git a/src/restic/archiver/archive_reader.go b/src/restic/archiver/archive_reader.go index 43beee69a..6ed72ab96 100644 --- a/src/restic/archiver/archive_reader.go +++ b/src/restic/archiver/archive_reader.go @@ -11,15 +11,22 @@ import ( "github.com/restic/chunker" ) -// ArchiveReader reads from the reader and archives the data. Returned is the -// resulting snapshot and its ID. -func ArchiveReader(repo restic.Repository, p *restic.Progress, rd io.Reader, name string, tags []string, hostname string) (*restic.Snapshot, restic.ID, error) { +// Reader allows saving a stream of data to the repository. +type Reader struct { + restic.Repository + + Tags []string + Hostname string +} + +// Archive reads data from the reader and saves it to the repo. +func (r *Reader) Archive(name string, rd io.Reader, p *restic.Progress) (*restic.Snapshot, restic.ID, error) { if name == "" { return nil, restic.ID{}, errors.New("no filename given") } debug.Log("start archiving %s", name) - sn, err := restic.NewSnapshot([]string{name}, tags, hostname) + sn, err := restic.NewSnapshot([]string{name}, r.Tags, r.Hostname) if err != nil { return nil, restic.ID{}, err } @@ -27,6 +34,7 @@ func ArchiveReader(repo restic.Repository, p *restic.Progress, rd io.Reader, nam p.Start() defer p.Done() + repo := r.Repository chnker := chunker.New(rd, repo.Config().ChunkerPolynomial) ids := restic.IDs{} diff --git a/src/restic/archiver/archive_reader_test.go b/src/restic/archiver/archive_reader_test.go index bdcc2a1e8..a8ab18668 100644 --- a/src/restic/archiver/archive_reader_test.go +++ b/src/restic/archiver/archive_reader_test.go @@ -79,7 +79,13 @@ func TestArchiveReader(t *testing.T) { f := fakeFile(t, seed, size) - sn, id, err := ArchiveReader(repo, nil, f, "fakefile", []string{"test"}, "localhost") + r := &Reader{ + Repository: repo, + Hostname: "localhost", + Tags: []string{"test"}, + } + + sn, id, err := r.Archive("fakefile", f, nil) if err != nil { t.Fatalf("ArchiveReader() returned error %v", err) } @@ -99,7 +105,13 @@ func TestArchiveReaderNull(t *testing.T) { repo, cleanup := repository.TestRepository(t) defer cleanup() - sn, id, err := ArchiveReader(repo, nil, bytes.NewReader(nil), "fakefile", nil, "localhost") + r := &Reader{ + Repository: repo, + Hostname: "localhost", + Tags: []string{"test"}, + } + + sn, id, err := r.Archive("fakefile", bytes.NewReader(nil), nil) if err != nil { t.Fatalf("ArchiveReader() returned error %v", err) } @@ -134,7 +146,13 @@ func TestArchiveReaderError(t *testing.T) { repo, cleanup := repository.TestRepository(t) defer cleanup() - sn, id, err := ArchiveReader(repo, nil, errReader("error returned by reading stdin"), "fakefile", nil, "localhost") + r := &Reader{ + Repository: repo, + Hostname: "localhost", + Tags: []string{"test"}, + } + + sn, id, err := r.Archive("fakefile", errReader("error returned by reading stdin"), nil) if err == nil { t.Errorf("expected error not returned") } @@ -167,11 +185,17 @@ func BenchmarkArchiveReader(t *testing.B) { t.Fatal(err) } + r := &Reader{ + Repository: repo, + Hostname: "localhost", + Tags: []string{"test"}, + } + t.SetBytes(size) t.ResetTimer() for i := 0; i < t.N; i++ { - _, _, err := ArchiveReader(repo, nil, bytes.NewReader(buf), "fakefile", []string{"test"}, "localhost") + _, _, err := r.Archive("fakefile", bytes.NewReader(buf), nil) if err != nil { t.Fatal(err) }