-
-
Notifications
You must be signed in to change notification settings - Fork 485
/
unclip.go
68 lines (50 loc) · 1.71 KB
/
unclip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package clipboard
import (
"context"
"fmt"
"os"
"github.com/atotto/clipboard"
"github.com/gopasspw/gopass/internal/notify"
"github.com/gopasspw/gopass/internal/pwschemes/argon2id"
"github.com/gopasspw/gopass/pkg/debug"
)
// Clear will attempt to erase the clipboard.
func Clear(ctx context.Context, name string, checksum string, force bool) error {
clipboardClearCMD := os.Getenv("GOPASS_CLIPBOARD_CLEAR_CMD")
if clipboardClearCMD != "" {
if err := callCommand(ctx, clipboardClearCMD, name, []byte(checksum)); err != nil {
_ = notify.Notify(ctx, "gopass - clipboard", "failed to call clipboard clear command")
return fmt.Errorf("failed to call clipboard clear command: %w", err)
}
debug.Log("clipboard cleared (%s)", checksum)
return nil
}
if clipboard.Unsupported {
return ErrNotSupported
}
cur, err := clipboard.ReadAll()
if err != nil {
return fmt.Errorf("failed to read clipboard: %w", err)
}
match, err := argon2id.Validate(cur, checksum)
if err != nil {
debug.Log("failed to validate checksum %s: %s", checksum, err)
return nil
}
if !match && !force {
return nil
}
if err := clipboard.WriteAll(""); err != nil {
_ = notify.Notify(ctx, "gopass - clipboard", "Failed to clear clipboard")
return fmt.Errorf("failed to write clipboard: %w", err)
}
if err := clearClipboardHistory(ctx); err != nil {
_ = notify.Notify(ctx, "gopass - clipboard", "Failed to clear clipboard history")
return fmt.Errorf("failed to clear clipboard history: %w", err)
}
if err := notify.Notify(ctx, "gopass - clipboard", "Clipboard has been cleared"); err != nil {
return fmt.Errorf("failed to send unclip notification: %w", err)
}
debug.Log("clipboard cleared (%s)", checksum)
return nil
}