diff --git a/action/show.go b/action/show.go index 667e826cac..6f42d39f36 100644 --- a/action/show.go +++ b/action/show.go @@ -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" @@ -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() @@ -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) } diff --git a/action/show_test.go b/action/show_test.go index 178634d7c0..fbd1464521 100644 --- a/action/show_test.go +++ b/action/show_test.go @@ -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) { diff --git a/store/err.go b/store/err.go index 7c276288a0..c3f887f455 100644 --- a/store/err.go +++ b/store/err.go @@ -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 diff --git a/tests/show_test.go b/tests/show_test.go index 4665af83fb..54e3f8ed51 100644 --- a/tests/show_test.go +++ b/tests/show_test.go @@ -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) @@ -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)