119 lines
2.9 KiB
Go
119 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"regexp"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
// Text aus der Zwischenablage lesen
|
|
clipText, err := getClipboard()
|
|
if err != nil {
|
|
fmt.Printf("fehler beim lesen der zwischenablage: %v\n", err)
|
|
return
|
|
}
|
|
|
|
// Entferne Zeilenumbrüche am ende
|
|
clipText = strings.TrimSpace(clipText)
|
|
if clipText == "" {
|
|
fmt.Println("die zwischenablage ist leer")
|
|
return
|
|
}
|
|
|
|
// Text an snips.sh senden
|
|
output, err := sendToSnips(clipText)
|
|
if err != nil {
|
|
fmt.Printf("fehler beim senden an snips.sh: %v\n", err)
|
|
return
|
|
}
|
|
|
|
// URL aus der antwort extrahieren
|
|
url := extractURL(output)
|
|
if url == "" {
|
|
fmt.Println("keine url in der antwort gefunden")
|
|
return
|
|
}
|
|
|
|
// URL in die zwischenablage kopieren
|
|
err = setClipboard(url)
|
|
if err != nil {
|
|
fmt.Printf("fehler beim kopieren der url: %v\n", err)
|
|
return
|
|
}
|
|
|
|
fmt.Println(url)
|
|
}
|
|
|
|
func getClipboard() (string, error) {
|
|
var cmd *exec.Cmd
|
|
switch runtime.GOOS {
|
|
case "windows":
|
|
cmd = exec.Command("powershell", "-command", "Get-Clipboard")
|
|
case "linux":
|
|
// Prüfe, ob xclip installiert ist
|
|
if _, err := exec.LookPath("xclip"); err == nil {
|
|
cmd = exec.Command("xclip", "-selection", "clipboard", "-o")
|
|
} else if _, err := exec.LookPath("xsel"); err == nil {
|
|
cmd = exec.Command("xsel", "--clipboard", "--output")
|
|
} else {
|
|
return "", fmt.Errorf("weder xclip noch xsel sind installiert")
|
|
}
|
|
case "darwin": // macOS
|
|
cmd = exec.Command("pbpaste")
|
|
default:
|
|
return "", fmt.Errorf("betriebssystem %s wird nicht unterstützt", runtime.GOOS)
|
|
}
|
|
|
|
output, err := cmd.Output()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(output), nil
|
|
}
|
|
|
|
func setClipboard(text string) error {
|
|
var cmd *exec.Cmd
|
|
switch runtime.GOOS {
|
|
case "windows":
|
|
cmd = exec.Command("powershell", "-command", fmt.Sprintf("Set-Clipboard -Value '%s'", text))
|
|
case "linux":
|
|
// Prüfe, ob xclip installiert ist
|
|
if _, err := exec.LookPath("xclip"); err == nil {
|
|
cmd = exec.Command("xclip", "-selection", "clipboard")
|
|
} else if _, err := exec.LookPath("xsel"); err == nil {
|
|
cmd = exec.Command("xsel", "--clipboard", "--input")
|
|
} else {
|
|
return fmt.Errorf("weder xclip noch xsel sind installiert")
|
|
}
|
|
case "darwin": // macOS
|
|
cmd = exec.Command("pbcopy")
|
|
default:
|
|
return fmt.Errorf("betriebssystem %s wird nicht unterstützt", runtime.GOOS)
|
|
}
|
|
|
|
cmd.Stdin = strings.NewReader(text)
|
|
return cmd.Run()
|
|
}
|
|
|
|
func sendToSnips(text string) (string, error) {
|
|
cmd := exec.Command("ssh", "-T", "snips.sh")
|
|
cmd.Stdin = strings.NewReader(text)
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return "", fmt.Errorf("SSH-Fehler: %v\nOutput: %s", err, string(output))
|
|
}
|
|
return string(output), nil
|
|
}
|
|
|
|
func extractURL(output string) string {
|
|
re := regexp.MustCompile(`f:([a-zA-Z0-9]+)@snips\.sh`)
|
|
matches := re.FindStringSubmatch(output)
|
|
if len(matches) > 1 {
|
|
return fmt.Sprintf("https://snips.sh/f/%s", matches[1])
|
|
}
|
|
return ""
|
|
}
|