forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clipboard.go
38 lines (30 loc) · 1.04 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
package clipboard
import (
"context"
"fmt"
"github.com/justwatchcom/gopass/pkg/ctxutil"
"github.com/justwatchcom/gopass/pkg/out"
"github.com/atotto/clipboard"
"github.com/fatih/color"
"github.com/pkg/errors"
)
var (
// ErrNotSupported is returned when the clipboard is not accessible
ErrNotSupported = fmt.Errorf("WARNING: No clipboard available. Install xsel or xclip or use -p 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.Yellow(ctx, "%s", ErrNotSupported)
return nil
}
if err := clipboard.WriteAll(string(content)); err != nil {
return errors.Wrapf(err, "failed to write to clipboard")
}
if err := clear(ctx, content, ctxutil.GetClipTimeout(ctx)); err != nil {
return errors.Wrapf(err, "failed to clear clipboard")
}
out.Print(ctx, "Copied %s to clipboard. Will clear in %d seconds.", color.YellowString(name), ctxutil.GetClipTimeout(ctx))
return nil
}