Skip to content

Commit

Permalink
Merge pull request #8 from kpitt/simplify-clipboard
Browse files Browse the repository at this point in the history
Simplify clipboard options
  • Loading branch information
kpitt authored Sep 28, 2022
2 parents 2569260 + 743b4ff commit 66e4d8a
Show file tree
Hide file tree
Showing 25 changed files with 47 additions and 235 deletions.
4 changes: 2 additions & 2 deletions docs/commands/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Note: To manage mounts use `gopass mounts`.

```
$ gopass config
$ gopass config autoclip
$ gopass config autoclip false
$ gopass config autoimport
$ gopass config autoimport false
```

## Flags
Expand Down
13 changes: 2 additions & 11 deletions docs/commands/find.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
# `find` command

The `find` command will attempt to do a simple substring match on the names of all secrets.
If there is a single match it will directly invoke `show` and display the result.
If there are multiple matches a selection will be shown.
The `find` command does a simple substring match on the names of all secrets,
and lists the matching secrets.

Note: The find command will not fall back to a fuzzy search.

## Synopsis

```
$ gopass find entry
$ gopass find -f entry
$ gopass find -c entry
```

## Flags

Flag | Aliases | Description
---- | ------- | -----------
`--clip` | `-c` | Copy the password into the clipboard.
6 changes: 1 addition & 5 deletions docs/commands/generate.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $ gopass generate entry key [length]

Flag | Aliases | Description
---- | ------- | -----------
`--clip` | `-c` | Copy the generated password into the clipboard. Default: Value of `autoclip`
`--clip` | `-c` | Copy the generated password into the clipboard. Default: false.
`--print` | `-p` | Print the generated password to the terminal. Default: false.
`--force` | `-f` | Force overwriting an existing entry.
`--edit` | `-e` | Generate a password and open the entry for editing in `$EDITOR`.
Expand All @@ -43,10 +43,6 @@ Generator | Description
`memorable` | Generate a memorable password. The length argument specifies the minimum lenght of characters. Please note that the password might be longer if not all necessary rules were satisfied by the minimum length solution.
`external` | Use the external generator from `$GOPASS_EXTERNAL_PWGEN`

## Relevant configuration options

* `autoclip` only applies to `generate`. If set the generated password is automatically copied to the clipboard - unless `--clip` is explicitly set to `--clip=false`

## Templates

When creating a new entry gopass will look for the most specific template
Expand Down
2 changes: 0 additions & 2 deletions docs/commands/show.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ $ gopass show entry --password
Flag | Aliases | Description
---- | ------- | -----------
`--clip` | `-c` | Copy the password value into the clipboard and don't show the content.
`--alsoclip` | `-C` | Copy the password value into the clipboard and show the content.
`--qr` | | Encode the password field as a QR code and print it. Note: When combining with `-c`/`-C` the unencoded password is copied. Not the QR code.
`--password` | `-o` | Display only the password. For use in scripts. Takes precedence over other flags.
`--revision` | `-r` | Display a specific revision of the entry. Use an exact version identifier from `gopass history` or the special `-<N>` syntax. Does not work with native (e.g. git) refs.
Expand All @@ -42,7 +41,6 @@ TODO: We need to specify the expectations around new lines.
* When no flag is set the `show` command will display the full content of the secret and will parse it to support key-value lookup and YAML entries.
* The `--noparsing` flag will disable all parsing of the output, this can help debugging YAML secrets for example, where `key: 0123` actually parses into octal for 83.
* The `--clip` flag will copy the value of the `Password` field to the clipboard and doesn't display any part of the secret.
* The `--alsoclip` option will copy the value of the `Password` field but also display the secret content depending on the `safecontent` setting, i.e. obstructing the `Password` field if `safecontent` is `true` or just displaying it if not.
* The `--qr` flags operates complementary to other flags. It will *additionally* format the value of the `Password` entry as a QR code and display it. Other than that it will honor the other options, e.g. `gopass show --qr` will display the QR code *and* the whole secret content below. One special case is the `-o` flag, this flag doesn't make a lot of sense in combination, so if both `--qr` and `-o` are given only the QR code will be displayed.
* Arbitrary git refs are not supported as arguments to the `--revision` flag. Using those might work, but this is explicitly not supported and bug reports will be closed as `wont-fix`. The main issue with using arbitrary git refs is that git versions a whole repository, not single files. So the revision `HEAD^` might not have any changes for a given entry. Thus we only support specifc revisions obtained from `gopass history` or our custom syntax `-N` where N is an integer identifying a specific commit before `HEAD` (cf. `HEAD~N`).

Expand Down
5 changes: 2 additions & 3 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ During start up, gopass will look for a configuration file at `$HOME/.config/gop
All configuration options are also available for reading and writing through the sub-command `gopass config`.

* To display all values: `gopass config`
* To display a single value: `gopass config autoclip`
* To update a single value: `gopass config autoclip false`
* To display a single value: `gopass config autoimport`
* To update a single value: `gopass config autoimport false`
* As many other sub-commands this command accepts a `--store` flag to operate on a given sub-store, provided the sub-store is a remote one.

This is a list of available options:

| **Option** | **Type** | Description |
| ---------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `autoclip` | `bool` | Always copy the password created by `gopass generate`. Only applies to generate. |
| `autoimport` | `bool` | Import missing keys stored in the pass repository without asking. |
| `cliptimeout` | `int` | How many seconds the secret is stored when using `-c`. |
| `exportkeys` | `bool` | Export public keys of all recipients to the store. |
Expand Down
5 changes: 1 addition & 4 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ The generated password for golang.org/gopher is:
Eech4ahRoy2oowi0ohl
```

The `generate` command will ask for any missing arguments, like the name of the secret or the length. By default the password is copied to clipboard. If you don't want the password to be copied, but displayed instead, use the `-p` flag to print it.

By default the password is copied to clipboard, but you can disable this using the `AutoClip` option, which, when set to`false`, will neither display, nor print the password. This is overridden by the `-p` or `-c` flags.
The `generate` command will ask for any missing arguments, like the name of the secret or the length. Use the `-p` flag to print the generated password, or the `-c` flag to copy it to the clipboard.

### Edit a secret

Expand Down Expand Up @@ -410,7 +408,6 @@ gopass allows editing the config from the command-line. This is similar to how g
```bash
$ gopass config
askformore: false
autoclip: true
autoimport: false
cliptimeout: 10
noconfirm: false
Expand Down
20 changes: 3 additions & 17 deletions internal/action/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ func ShowFlags() []cli.Flag {
Aliases: []string{"c"},
Usage: "Copy the password value into the clipboard",
},
&cli.BoolFlag{
Name: "alsoclip",
Aliases: []string{"C"},
Usage: "Copy the password and show everything",
},
&cli.BoolFlag{
Name: "qr",
Usage: "Print the password as a QR Code",
Expand Down Expand Up @@ -302,22 +297,13 @@ func (s *Action) GetCommands() []*cli.Command {
{
Name: "find",
Usage: "Search for secrets",
ArgsUsage: "[needle]",
ArgsUsage: "<pattern>",
Description: "" +
"This command will first attempt a simple pattern match on the name of the " +
"secret. If there is an exact match it will be shown directly; if there are " +
"multiple matches, a selection will be shown.",
"List all secrets that match the specified search pattern.",
Before: s.IsInitialized,
Action: s.FindNoFuzzy,
Action: s.Find,
Aliases: []string{"search"},
BashComplete: s.Complete,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "clip",
Aliases: []string{"c"},
Usage: "Copy the password into the clipboard",
},
},
},
{
Name: "fsck",
Expand Down
9 changes: 3 additions & 6 deletions internal/action/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ func TestConfig(t *testing.T) { //nolint:paralleltest

c := gptest.CliCtx(ctx, t)
assert.NoError(t, act.Config(c))
want := `autoclip: true
autoimport: true
want := `autoimport: true
cliptimeout: 45
exportkeys: true
nopager: false
Expand Down Expand Up @@ -74,8 +73,7 @@ parsing: true
defer buf.Reset()

act.printConfigValues(ctx)
want := `autoclip: true
autoimport: true
want := `autoimport: true
cliptimeout: 45
exportkeys: true
nopager: true
Expand Down Expand Up @@ -107,8 +105,7 @@ parsing: true
defer buf.Reset()

act.ConfigComplete(gptest.CliCtx(ctx, t))
want := `autoclip
autoimport
want := `autoimport
cliptimeout
exportkeys
nopager
Expand Down
34 changes: 0 additions & 34 deletions internal/action/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ const (
ctxKeyPrintQR
ctxKeyRevision
ctxKeyKey
ctxKeyOnlyClip
ctxKeyAlsoClip
ctxKeyPrintChars
)

Expand All @@ -31,38 +29,6 @@ func IsClip(ctx context.Context) bool {
return bv
}

// WithAlsoClip returns a context with the value for alsoclip (copy to
// clipboard and print to stdout) set.
func WithAlsoClip(ctx context.Context, clip bool) context.Context {
return context.WithValue(ctx, ctxKeyAlsoClip, clip)
}

// IsAlsoClip returns the value for alsoclip of the dfeault (false).
func IsAlsoClip(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyAlsoClip).(bool)
if !ok {
return false
}

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
}

// WithPasswordOnly returns a context with the value of password only set.
func WithPasswordOnly(ctx context.Context, pw bool) context.Context {
return context.WithValue(ctx, ctxKeyPasswordOnly, pw)
Expand Down
18 changes: 0 additions & 18 deletions internal/action/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,3 @@ func TestWithKey(t *testing.T) {
assert.Equal(t, "", GetKey(ctx))
assert.Equal(t, "foo", GetKey(WithKey(ctx, "foo")))
}

func TestWithOnlyClip(t *testing.T) {
t.Parallel()

ctx := context.Background()

assert.False(t, IsOnlyClip(ctx))
assert.True(t, IsOnlyClip(WithOnlyClip(ctx, true)))
}

func TestWithAlsoClip(t *testing.T) {
t.Parallel()

ctx := context.Background()

assert.False(t, IsAlsoClip(ctx))
assert.True(t, IsAlsoClip(WithAlsoClip(ctx, true)))
}
4 changes: 0 additions & 4 deletions internal/action/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ func TestCopy(t *testing.T) { //nolint:paralleltest
require.NoError(t, err)
require.NotNil(t, act)

act.cfg.AutoClip = false

buf := &bytes.Buffer{}
out.Stdout = buf
stdout = buf
Expand Down Expand Up @@ -110,8 +108,6 @@ func TestCopyGpg(t *testing.T) { //nolint:paralleltest
require.NoError(t, err)
require.NotNil(t, act)

act.cfg.AutoClip = false

buf := &bytes.Buffer{}
out.Stdout = buf
stdout = buf
Expand Down
26 changes: 9 additions & 17 deletions internal/action/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,20 @@ import (
"github.com/urfave/cli/v2"
)

// FindNoFuzzy runs find without fuzzy search.
func (s *Action) FindNoFuzzy(c *cli.Context) error {
return s.findCmd(c, nil, false)
}

// Find runs find.
// Find runs the find command action, without fuzzy search.
func (s *Action) Find(c *cli.Context) error {
return s.findCmd(c, s.show, true)
}
if !c.Args().Present() {
return exit.Error(exit.Usage, nil, "Usage: %s find <pattern>", s.Name)
}

func (s *Action) findCmd(c *cli.Context, cb showFunc, fuzzy bool) error {
ctx := ctxutil.WithGlobalFlags(c)
if c.IsSet("clip") {
ctx = WithOnlyClip(ctx, c.Bool("clip"))
ctx = WithClip(ctx, c.Bool("clip"))
}

if !c.Args().Present() {
return exit.Error(exit.Usage, nil, "Usage: %s find <NEEDLE>", s.Name)
}
return s.find(ctx, c, c.Args().First(), nil, false)
}

return s.find(ctx, c, c.Args().First(), cb, fuzzy)
// FindFuzzy runs a fuzzy find of the specified name and attempts to show the result.
func (s *Action) FindFuzzy(c *cli.Context, name string) error {
return s.find(c.Context, c, name, s.show, true)
}

// see action.show - context, cli context, name, key, rescurse.
Expand Down
18 changes: 8 additions & 10 deletions internal/action/find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ func TestFind(t *testing.T) { //nolint:paralleltest
require.NoError(t, err)
require.NotNil(t, act)

act.cfg.AutoClip = false

buf := &bytes.Buffer{}
out.Stdout = buf
stdout = buf
Expand All @@ -48,25 +46,25 @@ func TestFind(t *testing.T) { //nolint:paralleltest

// find
c := gptest.CliCtx(ctx, t)
if err := act.Find(c); err == nil || err.Error() != fmt.Sprintf("Usage: %s find <NEEDLE>", actName) {
if err := act.Find(c); err == nil || err.Error() != fmt.Sprintf("Usage: %s find <pattern>", actName) {
t.Errorf("Should fail: %s", err)
}

// find fo
// find fo (with fuzzy search)
c = gptest.CliCtxWithFlags(ctx, t, nil, "fo")
assert.NoError(t, act.Find(c))
assert.NoError(t, act.FindFuzzy(c, "fo"))
assert.Contains(t, strings.TrimSpace(buf.String()), "Found exact match in \"foo\"\nsecret")
buf.Reset()

// find fo (no fuzzy search)
c = gptest.CliCtxWithFlags(ctx, t, nil, "fo")
assert.NoError(t, act.FindNoFuzzy(c))
assert.NoError(t, act.Find(c))
assert.Equal(t, strings.TrimSpace(buf.String()), "foo")
buf.Reset()

// find yo
c = gptest.CliCtx(ctx, t, "yo")
assert.Error(t, act.Find(c))
c = gptest.CliCtx(ctx, t)
assert.Error(t, act.FindFuzzy(c, "yo"))
buf.Reset()

// add some secrets
Expand All @@ -78,8 +76,8 @@ func TestFind(t *testing.T) { //nolint:paralleltest
buf.Reset()

// find bar
c = gptest.CliCtx(ctx, t, "bar")
assert.NoError(t, act.Find(c))
c = gptest.CliCtx(ctx, t)
assert.NoError(t, act.FindFuzzy(c, "bar"))
assert.Equal(t, "bar/baz\nbar/zab", strings.TrimSpace(buf.String()))
buf.Reset()

Expand Down
13 changes: 2 additions & 11 deletions internal/action/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,11 @@ func (s *Action) generateCopyOrPrint(ctx context.Context, c *cli.Context, name,

out.OKf(ctx, "Password for entry %q generated", entry)

// 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)) {
// copy to clipboard if explicitly requested with -c
if IsClip(ctx) {
if err := clipboard.CopyTo(ctx, name, []byte(password), s.cfg.ClipTimeout); err != nil {
return exit.Error(exit.IO, err, "failed to copy to clipboard: %s", err)
}
// if autoclip is on and we're not printing the password to the terminal
// at least leave a notice that we did indeed copy it.
if s.cfg.AutoClip && !c.Bool("print") {
out.Print(ctx, "Copied to clipboard")

return nil
}
}

if !c.Bool("print") {
Expand Down
Loading

0 comments on commit 66e4d8a

Please sign in to comment.