mirror of
https://github.com/skidoodle/ctx.git
synced 2026-04-28 03:07:41 +02:00
af9e0a5372
Implement CopyFile for darwin, linux, and windows and call it from main to copy generated output to the clipboard. Enable CGO for builds, update goreleaser and release workflow to use macOS, and add a cross-platform test workflow that verifies the clipboard.
39 lines
724 B
Go
39 lines
724 B
Go
//go:build linux
|
|
|
|
package clipboard
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func CopyFile(path string) error {
|
|
absPath, err := filepath.Abs(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
uri := fmt.Sprintf("file://%s", absPath)
|
|
|
|
if isCommandAvailable("wl-copy") {
|
|
cmd := exec.Command("wl-copy", "--type", "text/uri-list")
|
|
cmd.Stdin = strings.NewReader(uri)
|
|
return cmd.Run()
|
|
}
|
|
|
|
if isCommandAvailable("xclip") {
|
|
cmd := exec.Command("xclip", "-selection", "clipboard", "-t", "text/uri-list")
|
|
cmd.Stdin = strings.NewReader(uri)
|
|
return cmd.Run()
|
|
}
|
|
|
|
return fmt.Errorf("install 'wl-copy' or 'xclip'")
|
|
}
|
|
|
|
func isCommandAvailable(name string) bool {
|
|
_, err := exec.LookPath(name)
|
|
return err == nil
|
|
}
|