-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclipboard.go
More file actions
36 lines (33 loc) · 795 Bytes
/
Copy pathclipboard.go
File metadata and controls
36 lines (33 loc) · 795 Bytes
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
package main
import (
"fmt"
"os/exec"
"runtime"
"strings"
)
func copyToClipboard(text string) error {
var cmd *exec.Cmd
switch runtime.GOOS {
case "darwin":
cmd = exec.Command("pbcopy")
case "linux":
switch {
case commandExists("xclip"):
cmd = exec.Command("xclip", "-selection", "clipboard")
case commandExists("xsel"):
cmd = exec.Command("xsel", "--clipboard", "--input")
case commandExists("wl-copy"):
cmd = exec.Command("wl-copy")
default:
return fmt.Errorf("no clipboard tool found (install xclip, xsel, or wl-clipboard)")
}
default:
return fmt.Errorf("clipboard not supported on %s", runtime.GOOS)
}
cmd.Stdin = strings.NewReader(text)
return cmd.Run()
}
func commandExists(name string) bool {
_, err := exec.LookPath(name)
return err == nil
}