| 1 | |
| 2 | |
| 3 | |
| 4 | package desktop |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "runtime" |
| 12 | "strings" |
| 13 | "time" |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | |
| 18 | func Notify(title, msg string) { |
| 19 | var cmd *exec.Cmd |
| 20 | switch runtime.GOOS { |
| 21 | case "darwin": |
| 22 | cmd = exec.Command("osascript", "-e", "on run argv", "-e", "display notification (item 2 of argv) with title (item 1 of argv)", "-e", "end run", title, msg) |
| 23 | case "linux": |
| 24 | if _, err := exec.LookPath("notify-send"); err != nil { |
| 25 | return |
| 26 | } |
| 27 | cmd = exec.Command("notify-send", "--", title, msg) |
| 28 | default: |
| 29 | return |
| 30 | } |
| 31 | if cmd.Start() == nil { |
| 32 | go func() { _ = cmd.Wait() }() |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | func Ask(ctx context.Context, title, msg string) (allow, ok bool) { |
| 41 | switch runtime.GOOS { |
| 42 | case "darwin": |
| 43 | res := run(ctx, "osascript", "-e", "on run argv", |
| 44 | "-e", `display dialog (item 2 of argv) with title (item 1 of argv) buttons {"Deny", "Allow"} default button "Deny" with icon caution`, |
| 45 | "-e", "end run", title, msg) |
| 46 | switch { |
| 47 | case res.err == nil: |
| 48 | return strings.Contains(res.stdout, "button returned:Allow"), true |
| 49 | case res.timedOut: |
| 50 | return false, true |
| 51 | } |
| 52 | |
| 53 | |
| 54 | return false, strings.Contains(res.stderr, "(-128)") |
| 55 | case "linux": |
| 56 | if os.Getenv("DISPLAY") == "" && os.Getenv("WAYLAND_DISPLAY") == "" { |
| 57 | return false, false |
| 58 | } |
| 59 | if _, err := exec.LookPath("zenity"); err == nil { |
| 60 | res := run(ctx, "zenity", "--question", "--title", title, "--text", msg, |
| 61 | "--ok-label=Allow", "--cancel-label=Deny", "--default-cancel", "--no-markup") |
| 62 | if allow, ok := linuxAnswer(res, 1, "cannot open display", "failed to open display", "unable to init server"); ok { |
| 63 | return allow, true |
| 64 | } |
| 65 | } |
| 66 | if _, err := exec.LookPath("kdialog"); err == nil { |
| 67 | res := run(ctx, "kdialog", "--title", title, "--warningyesno", msg, |
| 68 | "--yes-label", "Allow", "--no-label", "Deny") |
| 69 | if allow, ok := linuxAnswer(res, 2, "could not connect to display", "cannot connect to x server", "no qt platform plugin"); ok { |
| 70 | return allow, true |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | return false, false |
| 75 | } |
| 76 | |
| 77 | |
| 78 | |
| 79 | func linuxAnswer(res result, maxDeny int, noDisplay ...string) (allow, ok bool) { |
| 80 | if res.err == nil { |
| 81 | return true, true |
| 82 | } |
| 83 | if res.timedOut { |
| 84 | return false, true |
| 85 | } |
| 86 | if res.code < 1 || res.code > maxDeny { |
| 87 | return false, false |
| 88 | } |
| 89 | stderr := strings.ToLower(res.stderr) |
| 90 | for _, s := range noDisplay { |
| 91 | if strings.Contains(stderr, s) { |
| 92 | return false, false |
| 93 | } |
| 94 | } |
| 95 | return false, true |
| 96 | } |
| 97 | |
| 98 | type result struct { |
| 99 | stdout, stderr string |
| 100 | err error |
| 101 | code int |
| 102 | timedOut bool |
| 103 | } |
| 104 | |
| 105 | func run(ctx context.Context, name string, args ...string) result { |
| 106 | cmd := exec.CommandContext(ctx, name, args...) |
| 107 | var stdout, stderr strings.Builder |
| 108 | cmd.Stdout, cmd.Stderr = &stdout, &stderr |
| 109 | cmd.WaitDelay = time.Second |
| 110 | err := cmd.Run() |
| 111 | res := result{stdout: stdout.String(), stderr: stderr.String(), err: err, code: -1} |
| 112 | var ee *exec.ExitError |
| 113 | if errors.As(err, &ee) { |
| 114 | res.code = ee.ExitCode() |
| 115 | |
| 116 | res.timedOut = res.code == -1 && ctx.Err() != nil |
| 117 | } |
| 118 | return res |
| 119 | } |