Skip to content

Commit

Permalink
Add -C to display secret and copy to clipboard (#1272)
Browse files Browse the repository at this point in the history
Fixes #1178

Signed-off-by: Dominik Schulz <[email protected]>
  • Loading branch information
dominikschulz authored Apr 24, 2020
1 parent 780425f commit c2bcc74
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
4 changes: 4 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func setupApp(ctx context.Context, sv semver.Version) *cli.App {
Name: "clip, c",
Usage: "Copy the first line of the secret into the clipboard",
},
&cli.BoolFlag{
Name: "alsoclip, C",
Usage: "Copy the first line of the secret into the clipboard and show everything",
},
}

app.Commands = getCommands(ctx, action, app)
Expand Down
4 changes: 4 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,10 @@ func getCommands(ctx context.Context, action *ap.Action, app *cli.App) []*cli.Co
Aliases: []string{"c"},
Usage: "Copy the first line of the secret into the clipboard",
},
&cli.BoolFlag{
Name: "alsoclip, C",
Usage: "Copy the first line of the secret and show everything",
},
&cli.BoolFlag{
Name: "qr",
Usage: "Print the first line of the secret as QR Code",
Expand Down
16 changes: 16 additions & 0 deletions pkg/action/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
ctxKeyPrintQR
ctxKeyRevision
ctxKeyKey
ctxKeyOnlyClip
)

// WithClip returns a context with the value for clip (for copy to clipboard)
Expand All @@ -28,6 +29,21 @@ func IsClip(ctx context.Context) bool {
return bv
}

// WithOnlyClip returns a context with the value for clip (for copy to clipboard)
// set
func WithOnlyClip(ctx context.Context, clip bool) context.Context {
return context.WithValue(ctx, ctxKeyOnlyClip, clip)
}

// IsOnlyClip returns the value of clip or the default (false)
func IsOnlyClip(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyOnlyClip).(bool)
if !ok {
return false
}
return bv
}

// WithForce returns a context with the value for force set
func WithForce(ctx context.Context, force bool) context.Context {
return context.WithValue(ctx, ctxKeyForce, force)
Expand Down
1 change: 0 additions & 1 deletion pkg/action/find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ func TestFind(t *testing.T) {
assert.NoError(t, act.Find(ctx, c))
out = strings.TrimSpace(buf.String())
assert.Contains(t, out, "Found exact match in 'foo'")
assert.NotContains(t, out, "Copying password instead.")
buf.Reset()

// safecontent case with force flag set
Expand Down
18 changes: 15 additions & 3 deletions pkg/action/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func (s *Action) Show(ctx context.Context, c *cli.Context) error {
name := c.Args().First()

ctx = s.Store.WithConfig(ctx, name)
ctx = WithClip(ctx, c.Bool("clip"))
ctx = WithOnlyClip(ctx, c.Bool("clip"))
ctx = WithClip(ctx, c.Bool("alsoclip"))
ctx = WithForce(ctx, c.Bool("force"))
ctx = WithPrintQR(ctx, c.Bool("qr"))
ctx = WithPasswordOnly(ctx, c.Bool("password"))
Expand Down Expand Up @@ -103,13 +104,24 @@ func (s *Action) showHandleOutput(ctx context.Context, name string, sec store.Se
return s.showHandleYAMLError(ctx, name, key, err)
}
if IsClip(ctx) {
return clipboard.CopyTo(ctx, name, []byte(val))
if err := clipboard.CopyTo(ctx, name, []byte(val)); err != nil {
return err
}
if IsOnlyClip(ctx) {
return nil
}
}
content = val
case IsPrintQR(ctx):
return s.showPrintQR(ctx, name, sec.Password())
case IsClip(ctx):
return clipboard.CopyTo(ctx, name, []byte(sec.Password()))
if err := clipboard.CopyTo(ctx, name, []byte(sec.Password())); err != nil {
return err
}
if IsOnlyClip(ctx) {
return nil
}
fallthrough
default:
switch {
case IsPasswordOnly(ctx):
Expand Down

0 comments on commit c2bcc74

Please sign in to comment.