-
-
Notifications
You must be signed in to change notification settings - Fork 478
/
clipboard.go
53 lines (44 loc) · 1.67 KB
/
clipboard.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
package clipboard
import (
"context"
"fmt"
"os"
"github.com/gopasspw/gopass/internal/notify"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/atotto/clipboard"
"github.com/fatih/color"
)
var (
// ErrNotSupported is returned when the clipboard is not accessible
ErrNotSupported = fmt.Errorf("WARNING: No clipboard available. Install xsel or xclip or use -f to print to console")
)
// CopyTo copies the given data to the clipboard and enqueues automatic
// clearing of the clipboard
func CopyTo(ctx context.Context, name string, content []byte) error {
if clipboard.Unsupported {
out.Print(ctx, "%s", ErrNotSupported)
_ = notify.Notify(ctx, "gopass - clipboard", fmt.Sprintf("%s", ErrNotSupported))
return nil
}
if err := clipboard.WriteAll(string(content)); err != nil {
_ = notify.Notify(ctx, "gopass - clipboard", "failed to write to clipboard")
return fmt.Errorf("failed to write to clipboard: %w", err)
}
if err := clear(ctx, content, ctxutil.GetClipTimeout(ctx)); err != nil {
_ = notify.Notify(ctx, "gopass - clipboard", "failed to clear clipboard")
return fmt.Errorf("failed to clear clipboard: %w", err)
}
out.Print(ctx, "✔ Copied %s to clipboard. Will clear in %d seconds.", color.YellowString(name), ctxutil.GetClipTimeout(ctx))
_ = notify.Notify(ctx, "gopass - clipboard", fmt.Sprintf("✔ Copied %s to clipboard. Will clear in %d seconds.", name, ctxutil.GetClipTimeout(ctx)))
return nil
}
func killProc(pid int) {
// err should be always nil, but just to be sure
proc, err := os.FindProcess(pid)
if err != nil {
return
}
// we ignore this error as we're going to return nil anyway
_ = proc.Kill()
}