-
-
Notifications
You must be signed in to change notification settings - Fork 478
/
unclip.go
44 lines (34 loc) · 1.08 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
package clipboard
import (
"context"
"crypto/sha256"
"fmt"
"github.com/gopasspw/gopass/internal/notify"
"github.com/atotto/clipboard"
)
// Clear will attempt to erase the clipboard
func Clear(ctx context.Context, checksum string, force bool) error {
if clipboard.Unsupported {
return ErrNotSupported
}
cur, err := clipboard.ReadAll()
if err != nil {
return fmt.Errorf("failed to read clipboard: %w", err)
}
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(cur)))
if hash != checksum && !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)
}
return nil
}