Skip to content

Commit

Permalink
Document recursive copying (gopasspw#713)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikschulz authored Mar 15, 2018
1 parent 1a239b0 commit fe1de47
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
6 changes: 5 additions & 1 deletion action/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
20 changes: 20 additions & 0 deletions action/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}()
Expand Down Expand Up @@ -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()
}
8 changes: 8 additions & 0 deletions action/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -25,6 +26,7 @@ func TestInsert(t *testing.T) {

buf := &bytes.Buffer{}
out.Stdout = buf
color.NoColor = true
defer func() {
out.Stdout = os.Stdout
}()
Expand All @@ -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")))

}
3 changes: 2 additions & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions tests/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fe1de47

Please sign in to comment.