From 3f70e0a6a8c8e44b2eeffc2ead9901ac724cf0e9 Mon Sep 17 00:00:00 2001 From: Dominik Schulz Date: Thu, 15 Mar 2018 15:17:42 +0100 Subject: [PATCH] Document recursive copying (#713) Fixes #676 --- action/copy.go | 6 +++++- action/copy_test.go | 20 ++++++++++++++++++++ action/insert_test.go | 8 ++++++++ commands.go | 3 ++- tests/copy_test.go | 6 +++--- 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/action/copy.go b/action/copy.go index f3b680a4a3..f6c3ea26c1 100644 --- a/action/copy.go +++ b/action/copy.go @@ -19,7 +19,11 @@ func (s *Action) Copy(ctx context.Context, c *cli.Context) error { from := c.Args()[0] to := c.Args()[1] - if !s.Store.Exists(ctx, from) { + return s.copy(ctx, from, to, force) +} + +func (s *Action) copy(ctx context.Context, from, to string, force bool) error { + if !s.Store.Exists(ctx, from) && !s.Store.IsDir(ctx, from) { return exitError(ctx, ExitNotFound, nil, "%s does not exist", from) } diff --git a/action/copy_test.go b/action/copy_test.go index 3f98a75600..c15d0e9b5b 100644 --- a/action/copy_test.go +++ b/action/copy_test.go @@ -7,7 +7,9 @@ import ( "os" "testing" + "github.com/fatih/color" "github.com/justwatchcom/gopass/tests/gptest" + "github.com/justwatchcom/gopass/utils/ctxutil" "github.com/justwatchcom/gopass/utils/out" "github.com/stretchr/testify/assert" "github.com/urfave/cli" @@ -18,11 +20,13 @@ func TestCopy(t *testing.T) { defer u.Remove() ctx := context.Background() + ctx = ctxutil.WithAlwaysYes(ctx, true) act, err := newMock(ctx, u) assert.NoError(t, err) buf := &bytes.Buffer{} out.Stdout = buf + color.NoColor = true defer func() { out.Stdout = os.Stdout }() @@ -50,4 +54,20 @@ func TestCopy(t *testing.T) { assert.Error(t, act.Copy(ctx, c)) buf.Reset() + + // insert bam/baz + assert.NoError(t, act.insertStdin(ctx, "bam/baz", []byte("foobar"))) + assert.NoError(t, act.insertStdin(ctx, "bam/zab", []byte("barfoo"))) + + // recursive copy: bam -> zab + fs = flag.NewFlagSet("default", flag.ContinueOnError) + assert.NoError(t, fs.Parse([]string{"bam", "zab"})) + c = cli.NewContext(app, fs, nil) + + assert.NoError(t, act.Copy(ctx, c)) + buf.Reset() + + assert.NoError(t, act.show(ctx, c, "zab/zab", "", false)) + assert.Equal(t, "barfoo\n", buf.String()) + buf.Reset() } diff --git a/action/insert_test.go b/action/insert_test.go index 324f1ec39d..6e36597154 100644 --- a/action/insert_test.go +++ b/action/insert_test.go @@ -7,6 +7,7 @@ import ( "os" "testing" + "github.com/fatih/color" "github.com/justwatchcom/gopass/tests/gptest" "github.com/justwatchcom/gopass/utils/ctxutil" "github.com/justwatchcom/gopass/utils/out" @@ -25,6 +26,7 @@ func TestInsert(t *testing.T) { buf := &bytes.Buffer{} out.Stdout = buf + color.NoColor = true defer func() { out.Stdout = os.Stdout }() @@ -47,7 +49,13 @@ func TestInsert(t *testing.T) { // insert baz via stdin assert.NoError(t, act.insertStdin(ctx, "baz", []byte("foobar"))) + buf.Reset() + + assert.NoError(t, act.show(ctx, c, "baz", "", false)) + assert.Equal(t, "foobar\n", buf.String()) + buf.Reset() // insert zab#key assert.NoError(t, act.insertYAML(ctx, "zab", "key", []byte("foobar"))) + } diff --git a/commands.go b/commands.go index f15bc19995..e7724ab3df 100644 --- a/commands.go +++ b/commands.go @@ -260,7 +260,8 @@ func getCommands(ctx context.Context, action *ap.Action, app *cli.App) []cli.Com Usage: "Copy secrets from one location to another", Description: "" + "This command copies an existing secret in the store to another location. " + - "It will also handle copying secrets to different sub stores.", + "It will also handle copying secrets to different sub stores. " + + "If the destination is directory it will automatically copy recursively.", Before: func(c *cli.Context) error { return action.Initialized(withGlobalFlags(ctx, c), c) }, Action: func(c *cli.Context) error { return action.Copy(withGlobalFlags(ctx, c), c) diff --git a/tests/copy_test.go b/tests/copy_test.go index 5954e88c51..fa8efcb7f0 100644 --- a/tests/copy_test.go +++ b/tests/copy_test.go @@ -30,9 +30,9 @@ func TestCopy(t *testing.T) { ts.initSecrets("") - out, err = ts.run("copy foo bar") - assert.Error(t, err) - assert.Equal(t, "\nError: foo does not exist\n", out) + // recursive copy + _, err = ts.run("copy foo bar") + assert.NoError(t, err) out, err = ts.run("copy foo/bar foo/baz") assert.NoError(t, err)