Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto copy with safecontent #685

Merged
merged 3 commits into from
Mar 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions action/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"strings"

"github.com/fatih/color"
"github.com/justwatchcom/gopass/store"
"github.com/justwatchcom/gopass/store/secret"
"github.com/justwatchcom/gopass/utils/ctxutil"
Expand Down Expand Up @@ -101,7 +100,8 @@ func (s *Action) showHandleOutput(ctx context.Context, name, key string, sec *se
case ctxutil.IsShowSafeContent(ctx) && !IsForce(ctx):
content = sec.Body()
if content == "" {
return exitError(ctx, ExitNotFound, store.ErrNoBody, "no safe content to display, you can force display with show -f")
out.Yellow(ctx, "No safe content to display, you can force display with show -f.\nCopying password instead.")
return copyToClipboard(ctx, name, []byte(sec.Password()))
}
default:
buf, err := sec.Bytes()
Expand All @@ -121,7 +121,7 @@ func (s *Action) showHandleError(ctx context.Context, c *cli.Context, name strin
if err != store.ErrNotFound || !recurse || !ctxutil.IsTerminal(ctx) {
return exitError(ctx, ExitUnknown, err, "failed to retrieve secret '%s': %s", name, err)
}
color.Yellow("Entry '%s' not found. Starting search...", name)
out.Yellow(ctx, "Entry '%s' not found. Starting search...", name)
if err := s.Find(ctx, c); err != nil {
return exitError(ctx, ExitNotFound, err, "%s", err)
}
Expand Down
48 changes: 48 additions & 0 deletions action/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,54 @@ func TestShow(t *testing.T) {
assert.NoError(t, act.Show(ctx, c))
assert.Equal(t, "bar\n└── baz\n\n", buf.String())
buf.Reset()

// show twoliner with safecontent enabled
ctx = ctxutil.WithShowSafeContent(ctx, true)
fs = flag.NewFlagSet("default", flag.ContinueOnError)
assert.NoError(t, fs.Parse([]string{"bar/baz"}))
c = cli.NewContext(app, fs, nil)

assert.NoError(t, act.Show(ctx, c))
assert.Equal(t, "---\nbar: zab", buf.String())
buf.Reset()

// show foo with safecontent enabled, should warn and copy the stuff
fs = flag.NewFlagSet("default", flag.ContinueOnError)
assert.NoError(t, fs.Parse([]string{"foo"}))
c = cli.NewContext(app, fs, nil)

assert.NoError(t, act.Show(ctx, c))
assert.Contains(t, buf.String(), "No safe content to display, you can force display with show -f.")
buf.Reset()

// show foo with safecontent enabled, with the force flag
fs = flag.NewFlagSet("default", flag.ContinueOnError)
bf = cli.BoolFlag{
Name: "force",
Usage: "force",
}
assert.NoError(t, bf.ApplyWithError(fs))
assert.NoError(t, fs.Parse([]string{"--force", "foo"}))
c = cli.NewContext(app, fs, nil)

assert.NoError(t, act.Show(ctx, c))
assert.Equal(t, "secret", buf.String())
buf.Reset()

// show twoliner with safecontent enabled, but with the clip flag, which should copy just the secret
ctx = ctxutil.WithShowSafeContent(ctx, true)
fs = flag.NewFlagSet("default", flag.ContinueOnError)
bf = cli.BoolFlag{
Name: "clip",
Usage: "clip",
}
assert.NoError(t, bf.ApplyWithError(fs))
assert.NoError(t, fs.Parse([]string{"--clip", "bar/baz"}))
c = cli.NewContext(app, fs, nil)

assert.NoError(t, act.Show(ctx, c))
assert.NotContains(t, buf.String(), "123")
buf.Reset()
}

func TestShowHandleRevision(t *testing.T) {
Expand Down
2 changes: 0 additions & 2 deletions store/err.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ var (
ErrGitNoRemote = errors.Errorf("git has no remote origin")
// ErrGitNothingToCommit is returned if there are no staged changes
ErrGitNothingToCommit = errors.Errorf("git has nothing to commit")
// ErrNoBody is returned if a secret exists but has no content beyond a password
ErrNoBody = errors.Errorf("no safe content to display, you can force display with show -f")
// ErrNoPassword is returned is a secret exists but has no password, only a body
ErrNoPassword = errors.Errorf("no password to display")
// ErrYAMLNoMark is returned if a secret contains no valid YAML document marker
Expand Down
6 changes: 5 additions & 1 deletion tests/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestShow(t *testing.T) {
assert.NoError(t, err)

out, _ = ts.run("show fixed/secret")
assert.Equal(t, "\nError: no safe content to display, you can force display with show -f\n", out)
assert.Contains(t, out, "No safe content to display, you can force display with show -f.\nCopying password instead.")

out, err = ts.run("show -f fixed/secret")
assert.NoError(t, err)
Expand All @@ -48,6 +48,10 @@ func TestShow(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "and\nmore stuff", out)

out, err = ts.run("show fixed/twoliner -c")
assert.NoError(t, err)
assert.NotContains(t, out, "No safe content to display, you can force display with show -f")

_, err = ts.run("config safecontent false")
assert.NoError(t, err)

Expand Down