| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "time" |
| 11 | |
| 12 | tea "github.com/charmbracelet/bubbletea" |
| 13 | ) |
| 14 | |
| 15 | // restartQuiet is how long the keyboard must be quiet before the dashboard |
| 16 | // restarts onto a new klaus, so a restart never lands mid-interaction. |
| 17 | const restartQuiet = 10 * time.Second |
| 18 | |
| 19 | // binCheck notices when klaus on PATH names a different file than the |
| 20 | // running dashboard's binary, as when self-update installs a new klaus into |
| 21 | // the nix profile, so the dashboard can restart onto it. |
| 22 | type binCheck struct { |
| 23 | running string // the running binary, symlinks resolved |
| 24 | path func() string // the PATH to search |
| 25 | failed map[string]bool // files an exec failed for: never tried again |
| 26 | // version runs a klaus with --version; nil runs it for real. |
| 27 | version func(path string) (string, error) |
| 28 | } |
| 29 | |
| 30 | func newBinCheck() *binCheck { |
| 31 | exe, err := os.Executable() |
| 32 | if err != nil { |
| 33 | return nil |
| 34 | } |
| 35 | if r, err := filepath.EvalSymlinks(exe); err == nil { |
| 36 | exe = r |
| 37 | } |
| 38 | return &binCheck{running: exe, path: func() string { return os.Getenv("PATH") }, failed: map[string]bool{}} |
| 39 | } |
| 40 | |
| 41 | // candidate returns the file klaus on PATH resolves to, symlinks resolved, |
| 42 | // when that is not the running binary or a file an exec failed for. |
| 43 | func (b *binCheck) candidate() (string, bool) { |
| 44 | if b == nil { |
| 45 | return "", false |
| 46 | } |
| 47 | for _, dir := range filepath.SplitList(b.path()) { |
| 48 | if !filepath.IsAbs(dir) { |
| 49 | continue // as exec.LookPath, which refuses relative entries |
| 50 | } |
| 51 | p, err := exec.LookPath(filepath.Join(dir, "klaus")) |
| 52 | if err != nil { |
| 53 | continue |
| 54 | } |
| 55 | r, err := filepath.EvalSymlinks(p) |
| 56 | if err != nil || r == b.running || b.failed[r] { |
| 57 | return "", false |
| 58 | } |
| 59 | return r, true |
| 60 | } |
| 61 | return "", false |
| 62 | } |
| 63 | |
| 64 | // binUpdateMsg reports a new klaus on PATH that runs. |
| 65 | type binUpdateMsg struct { |
| 66 | path string |
| 67 | version string // as its --version reports it, e.g. 0.4.1-1a2b3c4 |
| 68 | } |
| 69 | |
| 70 | // restartWaitMsg re-checks whether the dashboard is idle enough to restart. |
| 71 | type restartWaitMsg struct{} |
| 72 | |
| 73 | // restartTargetMsg is what klaus on PATH names just before a restart: the |
| 74 | // new klaus to exec, or nil when there is none any more, as when a rollback |
| 75 | // put back the running binary. |
| 76 | type restartTargetMsg struct{ target *binUpdateMsg } |
| 77 | |
| 78 | // probeBin returns the new klaus on PATH if it runs --version, else nil. |
| 79 | // The probe can take seconds, so PATH is resolved again after it: if it no |
| 80 | // longer names the probed file, as when a rollback ran meanwhile, there is |
| 81 | // nothing to restart onto, and the next check starts over. |
| 82 | func probeBin(b *binCheck) *binUpdateMsg { |
| 83 | p, ok := b.candidate() |
| 84 | if !ok { |
| 85 | return nil |
| 86 | } |
| 87 | version := b.version |
| 88 | if version == nil { |
| 89 | version = runVersion |
| 90 | } |
| 91 | v, err := version(p) |
| 92 | if err != nil { |
| 93 | return nil // not runnable (yet): the next check looks again |
| 94 | } |
| 95 | if now, ok := b.candidate(); !ok || now != p { |
| 96 | return nil |
| 97 | } |
| 98 | return &binUpdateMsg{path: p, version: v} |
| 99 | } |
| 100 | |
| 101 | // runVersion runs a klaus with --version and returns its version. |
| 102 | func runVersion(path string) (string, error) { |
| 103 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 104 | defer cancel() |
| 105 | var out bytes.Buffer |
| 106 | c := exec.CommandContext(ctx, path, "--version") |
| 107 | c.Stdout = &out |
| 108 | if err := c.Run(); err != nil { |
| 109 | return "", err |
| 110 | } |
| 111 | v, _, _ := strings.Cut(strings.TrimSpace(out.String()), "\n") |
| 112 | return strings.TrimPrefix(v, "klaus version "), nil |
| 113 | } |
| 114 | |
| 115 | // checkBinCmd looks for a new klaus on PATH and, if it runs --version, |
| 116 | // reports it. |
| 117 | func checkBinCmd(b *binCheck) tea.Cmd { |
| 118 | return func() tea.Msg { |
| 119 | if next := probeBin(b); next != nil { |
| 120 | return *next |
| 121 | } |
| 122 | return nil |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | func restartWaitCmd() tea.Cmd { |
| 127 | return tea.Tick(time.Second, func(time.Time) tea.Msg { return restartWaitMsg{} }) |
| 128 | } |
| 129 | |
| 130 | // restartWhenIdle waits while a key was pressed in the last restartQuiet. |
| 131 | // Once the keyboard is quiet, it resolves klaus on PATH again, since the |
| 132 | // profile may have changed while it waited. |
| 133 | func (m dashboardModel) restartWhenIdle(now time.Time) (dashboardModel, tea.Cmd) { |
| 134 | if now.Sub(m.lastKeyAt) < restartQuiet { |
| 135 | return m, restartWaitCmd() |
| 136 | } |
| 137 | b := m.bin |
| 138 | return m, func() tea.Msg { return restartTargetMsg{target: probeBin(b)} } |
| 139 | } |
| 140 | |
| 141 | // restartOnto quits so the dashboard can exec target, the klaus PATH names |
| 142 | // now. With no target, the pending restart and its notice are dropped. |
| 143 | func (m dashboardModel) restartOnto(target *binUpdateMsg, now time.Time) (dashboardModel, tea.Cmd) { |
| 144 | if target == nil { |
| 145 | m.restart, m.binNotice, m.binNoticeErr = nil, "", false |
| 146 | return m, nil |
| 147 | } |
| 148 | m.restart = target |
| 149 | m.binNotice, m.binNoticeErr = "klaus updated to "+target.version+"; restarting", false |
| 150 | if now.Sub(m.lastKeyAt) < restartQuiet { |
| 151 | return m, restartWaitCmd() // a key came in meanwhile |
| 152 | } |
| 153 | m.restarting = true |
| 154 | return m, tea.Quit |
| 155 | } |