|
| 1 | +//go:build darwin |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | + |
| 15 | + "fyne.io/systray" |
| 16 | + |
| 17 | + "github.com/mideco-tech/codex-tg/internal/config" |
| 18 | + "github.com/mideco-tech/codex-tg/internal/trayapp" |
| 19 | +) |
| 20 | + |
| 21 | +var templateIcon = []byte{ |
| 22 | + 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, |
| 23 | + 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, |
| 24 | + 0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4, 0x89, 0x00, 0x00, 0x00, |
| 25 | + 0x0a, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0x60, 0x00, 0x00, 0x00, |
| 26 | + 0x02, 0x00, 0x01, 0xe2, 0x21, 0xbc, 0x33, 0x00, 0x00, 0x00, 0x00, 0x49, |
| 27 | + 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, |
| 28 | +} |
| 29 | + |
| 30 | +func main() { |
| 31 | + systray.Run(onReady, func() {}) |
| 32 | +} |
| 33 | + |
| 34 | +func onReady() { |
| 35 | + ctrGo := resolveCTRGo() |
| 36 | + systray.SetTitle("ctg") |
| 37 | + systray.SetTooltip("codex-tg") |
| 38 | + if len(templateIcon) > 0 { |
| 39 | + systray.SetTemplateIcon(templateIcon, templateIcon) |
| 40 | + } |
| 41 | + |
| 42 | + statusItem := systray.AddMenuItem("Status: checking...", "Show service status") |
| 43 | + startItem := systray.AddMenuItem("Start", "Start codex-tg service") |
| 44 | + stopItem := systray.AddMenuItem("Stop", "Stop codex-tg service") |
| 45 | + restartItem := systray.AddMenuItem("Restart", "Restart codex-tg service") |
| 46 | + loginItem := systray.AddMenuItemCheckbox("Start with system", "Toggle login LaunchAgent", false) |
| 47 | + systray.AddSeparator() |
| 48 | + doctorItem := systray.AddMenuItem("Run doctor", "Run ctr-go doctor in Terminal") |
| 49 | + configItem := systray.AddMenuItem("Open config", "Open config.env") |
| 50 | + logsItem := systray.AddMenuItem("Open logs", "Open codex-tg logs") |
| 51 | + setupItem := systray.AddMenuItem("Open Terminal Setup", "Run friendly setup wizard") |
| 52 | + systray.AddSeparator() |
| 53 | + quitItem := systray.AddMenuItem("Quit", "Quit tray app") |
| 54 | + |
| 55 | + refresh := func() { |
| 56 | + status := readServiceStatus(ctrGo) |
| 57 | + statusItem.SetTitle("Status: " + status.summary) |
| 58 | + if status.startAtLogin { |
| 59 | + loginItem.Check() |
| 60 | + } else { |
| 61 | + loginItem.Uncheck() |
| 62 | + } |
| 63 | + if !status.configExists { |
| 64 | + statusItem.SetTitle("Status: Needs setup") |
| 65 | + } |
| 66 | + } |
| 67 | + refresh() |
| 68 | + go func() { |
| 69 | + ticker := time.NewTicker(10 * time.Second) |
| 70 | + defer ticker.Stop() |
| 71 | + for range ticker.C { |
| 72 | + refresh() |
| 73 | + } |
| 74 | + }() |
| 75 | + |
| 76 | + go func() { |
| 77 | + for { |
| 78 | + select { |
| 79 | + case <-statusItem.ClickedCh: |
| 80 | + runTerminalCommand(ctrGo, "service", "status") |
| 81 | + case <-startItem.ClickedCh: |
| 82 | + runAndRefresh(refresh, ctrGo, trayapp.ActionStart) |
| 83 | + case <-stopItem.ClickedCh: |
| 84 | + runAndRefresh(refresh, ctrGo, trayapp.ActionStop) |
| 85 | + case <-restartItem.ClickedCh: |
| 86 | + runAndRefresh(refresh, ctrGo, trayapp.ActionRestart) |
| 87 | + case <-loginItem.ClickedCh: |
| 88 | + if readServiceStatus(ctrGo).startAtLogin { |
| 89 | + runAndRefresh(refresh, ctrGo, trayapp.ActionDisableLogin) |
| 90 | + } else { |
| 91 | + runAndRefresh(refresh, ctrGo, trayapp.ActionEnableLogin) |
| 92 | + } |
| 93 | + case <-doctorItem.ClickedCh: |
| 94 | + runTerminalCommand(ctrGo, "doctor") |
| 95 | + case <-configItem.ClickedCh: |
| 96 | + _ = exec.Command("open", config.ConfigFilePath()).Start() |
| 97 | + case <-logsItem.ClickedCh: |
| 98 | + _ = exec.Command("open", config.DefaultPaths().LogDir).Start() |
| 99 | + case <-setupItem.ClickedCh: |
| 100 | + runTerminalCommand(ctrGo, trayapp.ServiceSetupArgs()...) |
| 101 | + case <-quitItem.ClickedCh: |
| 102 | + systray.Quit() |
| 103 | + return |
| 104 | + } |
| 105 | + } |
| 106 | + }() |
| 107 | +} |
| 108 | + |
| 109 | +type serviceStatus struct { |
| 110 | + summary string |
| 111 | + startAtLogin bool |
| 112 | + configExists bool |
| 113 | +} |
| 114 | + |
| 115 | +func readServiceStatus(ctrGo string) serviceStatus { |
| 116 | + out, err := runCTRGo(ctrGo, trayapp.ActionStatus) |
| 117 | + status := serviceStatus{summary: "unknown", configExists: fileExists(config.ConfigFilePath())} |
| 118 | + if err != nil { |
| 119 | + if !status.configExists { |
| 120 | + status.summary = "needs setup" |
| 121 | + } |
| 122 | + return status |
| 123 | + } |
| 124 | + text := string(out) |
| 125 | + status.startAtLogin = strings.Contains(text, "Start with system: true") |
| 126 | + if strings.Contains(text, "Loaded: true") { |
| 127 | + status.summary = "running" |
| 128 | + } else if status.configExists { |
| 129 | + status.summary = "stopped" |
| 130 | + } |
| 131 | + return status |
| 132 | +} |
| 133 | + |
| 134 | +func runAndRefresh(refresh func(), ctrGo string, action trayapp.Action) { |
| 135 | + _, _ = runCTRGo(ctrGo, action) |
| 136 | + refresh() |
| 137 | +} |
| 138 | + |
| 139 | +func runCTRGo(ctrGo string, action trayapp.Action) ([]byte, error) { |
| 140 | + args, err := trayapp.CTRGoArgs(action) |
| 141 | + if err != nil { |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 145 | + defer cancel() |
| 146 | + cmd := exec.CommandContext(ctx, ctrGo, args...) |
| 147 | + return cmd.CombinedOutput() |
| 148 | +} |
| 149 | + |
| 150 | +func runTerminalCommand(ctrGo string, args ...string) { |
| 151 | + var script bytes.Buffer |
| 152 | + script.WriteString(shellQuote(ctrGo)) |
| 153 | + for _, arg := range args { |
| 154 | + script.WriteByte(' ') |
| 155 | + script.WriteString(shellQuote(arg)) |
| 156 | + } |
| 157 | + escaped := strings.ReplaceAll(script.String(), `"`, `\"`) |
| 158 | + _ = exec.Command("osascript", "-e", fmt.Sprintf(`tell application "Terminal" to do script "%s"`, escaped)).Start() |
| 159 | +} |
| 160 | + |
| 161 | +func resolveCTRGo() string { |
| 162 | + if value := strings.TrimSpace(os.Getenv("CTR_GO_BIN")); value != "" { |
| 163 | + return value |
| 164 | + } |
| 165 | + if found, err := exec.LookPath("ctr-go"); err == nil { |
| 166 | + return found |
| 167 | + } |
| 168 | + if exe, err := os.Executable(); err == nil { |
| 169 | + candidate := filepath.Join(filepath.Dir(exe), "ctr-go") |
| 170 | + if fileExists(candidate) { |
| 171 | + return candidate |
| 172 | + } |
| 173 | + } |
| 174 | + return "/usr/local/bin/ctr-go" |
| 175 | +} |
| 176 | + |
| 177 | +func fileExists(path string) bool { |
| 178 | + _, err := os.Stat(path) |
| 179 | + return err == nil |
| 180 | +} |
| 181 | + |
| 182 | +func shellQuote(value string) string { |
| 183 | + return "'" + strings.ReplaceAll(value, "'", "'\\''") + "'" |
| 184 | +} |
0 commit comments