Skip to content

Commit

Permalink
Fix AutoClip handling on generate
Browse files Browse the repository at this point in the history
Fixes gopasspw#2023

RELEASE_NOTES=[BUGFIX] Fix AutoClip handling on generate

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
  • Loading branch information
dominikschulz committed Nov 8, 2021
1 parent b494532 commit c53a985
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/action/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (s *Action) generateCopyOrPrint(ctx context.Context, c *cli.Context, name,
// copy to clipboard if:
// - explicitly requested with -c
// - autoclip=true, but only if output is not being redirected
if IsClip(ctx) || (s.cfg.AutoClip && !ctxutil.IsTerminal(ctx)) {
if IsClip(ctx) || (s.cfg.AutoClip && ctxutil.IsTerminal(ctx)) {
if err := clipboard.CopyTo(ctx, name, []byte(password), s.cfg.ClipTimeout); err != nil {
return ExitError(ExitIO, err, "failed to copy to clipboard: %s", err)
}
Expand Down
26 changes: 26 additions & 0 deletions internal/action/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,32 @@ func TestGenerate(t *testing.T) {
assert.NoError(t, act.Generate(gptest.CliCtxWithFlags(ctx, t, map[string]string{"force": "true", "xkcd": "true", "print": "true", "lang": "en"}, "foobar", "baz")))
buf.Reset()
})

// generate --force foobar 24 w/ autoclip and output redirection
t.Run("generate --force foobar 24", func(t *testing.T) {
ov := act.cfg.AutoClip
defer func() {
act.cfg.AutoClip = ov
}()
act.cfg.AutoClip = true
ctx := ctxutil.WithTerminal(ctx, false)
assert.NoError(t, act.Generate(gptest.CliCtxWithFlags(ctx, t, map[string]string{"force": "true"}, "foobar", "24")))
assert.Contains(t, buf.String(), "Not printing secrets by default")
buf.Reset()
})

// generate --force foobar 24 w/ autoclip and no output redirection
t.Run("generate --force foobar 24", func(t *testing.T) {
ov := act.cfg.AutoClip
defer func() {
act.cfg.AutoClip = ov
}()
act.cfg.AutoClip = true
ctx := ctxutil.WithTerminal(ctx, true)
assert.NoError(t, act.Generate(gptest.CliCtxWithFlags(ctx, t, map[string]string{"force": "true"}, "foobar", "24")))
assert.Contains(t, buf.String(), "Copied to clipboard")
buf.Reset()
})
}

func passIsAlphaNum(t *testing.T, buf string, want bool) {
Expand Down

0 comments on commit c53a985

Please sign in to comment.