| 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 | name string // the program name the dashboard was started as (invokedAs) |
| 25 | path func() string // the PATH to search |
| 26 | failed map[string]bool // files an exec failed for: never tried again |
| 27 | // home is the PATH directory whose valley-flow was the running binary |
| 28 | // when a dashboard started as valley-flow began, else "". It tells a |
| 29 | // rollback of that installation from another klaus on PATH. |
| 30 | home string |
| 31 | // version runs a klaus with --version; nil runs it for real. |
| 32 | version func(path string) (string, error) |
| 33 | } |
| 34 | |
| 35 | func newBinCheck() *binCheck { |
| 36 | exe, err := os.Executable() |
| 37 | if err != nil { |
| 38 | return nil |
| 39 | } |
| 40 | if r, err := filepath.EvalSymlinks(exe); err == nil { |
| 41 | exe = r |
| 42 | } |
| 43 | return startBinCheck(exe, invokedAs(os.Args[0]), func() string { return os.Getenv("PATH") }) |
| 44 | } |
| 45 | |
| 46 | // startBinCheck is the check of a dashboard running the binary running, |
| 47 | // started as program name, searching path. It notes the dashboard's home |
| 48 | // as path is now, so call it as the dashboard starts. |
| 49 | func startBinCheck(running, name string, path func() string) *binCheck { |
| 50 | b := &binCheck{running: running, name: name, path: path, failed: map[string]bool{}} |
| 51 | if name == helperName { |
| 52 | if dir, p, ok := firstOnPath(path(), name); ok { |
| 53 | if r, err := filepath.EvalSymlinks(p); err == nil && r == running { |
| 54 | b.home = dir |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | return b |
| 59 | } |
| 60 | |
| 61 | // firstOnPath returns the first executable name in path's absolute |
| 62 | // directories, and that directory. A relative entry is skipped, as valley |
| 63 | // flow skips it, where exec.LookPath would stop at it. |
| 64 | func firstOnPath(path, name string) (dir, file string, ok bool) { |
| 65 | for _, dir := range filepath.SplitList(path) { |
| 66 | if !filepath.IsAbs(dir) { |
| 67 | continue |
| 68 | } |
| 69 | if p, err := exec.LookPath(filepath.Join(dir, name)); err == nil { |
| 70 | return dir, p, true |
| 71 | } |
| 72 | } |
| 73 | return "", "", false |
| 74 | } |
| 75 | |
| 76 | // candidate returns the file the dashboard would restart onto, symlinks |
| 77 | // resolved, when that is not the running binary or a file an exec failed |
| 78 | // for. That is the first file on PATH with the name the dashboard was |
| 79 | // started as, since the restart keeps its arguments, the program name |
| 80 | // among them: for valley-flow, the file valley flow runs. The exception is |
| 81 | // a home that lost valley-flow but holds klaus, as a profile rolled back |
| 82 | // to a klaus that ships no valley-flow does: that klaus is the candidate, |
| 83 | // so a valley-flow elsewhere on PATH does not hide the rollback. |
| 84 | func (b *binCheck) candidate() (string, bool) { |
| 85 | if b == nil { |
| 86 | return "", false |
| 87 | } |
| 88 | p, ok := b.rolledBack() |
| 89 | if !ok { |
| 90 | _, p, ok = firstOnPath(b.path(), b.name) |
| 91 | } |
| 92 | if !ok { |
| 93 | return "", false |
| 94 | } |
| 95 | r, err := filepath.EvalSymlinks(p) |
| 96 | if err != nil || r == b.running || b.failed[r] { |
| 97 | return "", false |
| 98 | } |
| 99 | return r, true |
| 100 | } |
| 101 | |
| 102 | // rolledBack returns the home's klaus when the home no longer holds the |
| 103 | // name the dashboard was started as. |
| 104 | func (b *binCheck) rolledBack() (string, bool) { |
| 105 | if b.home == "" { |
| 106 | return "", false |
| 107 | } |
| 108 | if _, err := exec.LookPath(filepath.Join(b.home, b.name)); err == nil { |
| 109 | return "", false |
| 110 | } |
| 111 | p, err := exec.LookPath(filepath.Join(b.home, "klaus")) |
| 112 | return p, err == nil |
| 113 | } |
| 114 | |
| 115 | // binUpdateMsg reports a new klaus on PATH that runs. |
| 116 | type binUpdateMsg struct { |
| 117 | path string |
| 118 | version string // as its --version reports it, e.g. 0.4.1-1a2b3c4 |
| 119 | } |
| 120 | |
| 121 | // restartWaitMsg re-checks whether the dashboard is idle enough to restart. |
| 122 | type restartWaitMsg struct{} |
| 123 | |
| 124 | // restartTargetMsg is what klaus on PATH names just before a restart: the |
| 125 | // new klaus to exec, or nil when there is none any more, as when a rollback |
| 126 | // put back the running binary. |
| 127 | type restartTargetMsg struct{ target *binUpdateMsg } |
| 128 | |
| 129 | // probeBin returns the new klaus on PATH if it runs --version, else nil. |
| 130 | // The probe can take seconds, so PATH is resolved again after it: if it no |
| 131 | // longer names the probed file, as when a rollback ran meanwhile, there is |
| 132 | // nothing to restart onto, and the next check starts over. |
| 133 | func probeBin(b *binCheck) *binUpdateMsg { |
| 134 | p, ok := b.candidate() |
| 135 | if !ok { |
| 136 | return nil |
| 137 | } |
| 138 | version := b.version |
| 139 | if version == nil { |
| 140 | version = runVersion |
| 141 | } |
| 142 | v, err := version(p) |
| 143 | if err != nil { |
| 144 | return nil // not runnable (yet): the next check looks again |
| 145 | } |
| 146 | if now, ok := b.candidate(); !ok || now != p { |
| 147 | return nil |
| 148 | } |
| 149 | return &binUpdateMsg{path: p, version: v} |
| 150 | } |
| 151 | |
| 152 | // runVersion runs a klaus with --version and returns its version. |
| 153 | func runVersion(path string) (string, error) { |
| 154 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 155 | defer cancel() |
| 156 | var out bytes.Buffer |
| 157 | c := exec.CommandContext(ctx, path, "--version") |
| 158 | c.Stdout = &out |
| 159 | if err := c.Run(); err != nil { |
| 160 | return "", err |
| 161 | } |
| 162 | v, _, _ := strings.Cut(strings.TrimSpace(out.String()), "\n") |
| 163 | return strings.TrimPrefix(v, "klaus version "), nil |
| 164 | } |
| 165 | |
| 166 | // checkBinCmd looks for a new klaus on PATH and, if it runs --version, |
| 167 | // reports it. |
| 168 | func checkBinCmd(b *binCheck) tea.Cmd { |
| 169 | return func() tea.Msg { |
| 170 | if next := probeBin(b); next != nil { |
| 171 | return *next |
| 172 | } |
| 173 | return nil |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | func restartWaitCmd() tea.Cmd { |
| 178 | return tea.Tick(time.Second, func(time.Time) tea.Msg { return restartWaitMsg{} }) |
| 179 | } |
| 180 | |
| 181 | // restartWhenIdle waits while a key was pressed in the last restartQuiet. |
| 182 | // Once the keyboard is quiet, it resolves klaus on PATH again, since the |
| 183 | // profile may have changed while it waited. |
| 184 | func (m dashboardModel) restartWhenIdle(now time.Time) (dashboardModel, tea.Cmd) { |
| 185 | if now.Sub(m.lastKeyAt) < restartQuiet { |
| 186 | return m, restartWaitCmd() |
| 187 | } |
| 188 | b := m.bin |
| 189 | return m, func() tea.Msg { return restartTargetMsg{target: probeBin(b)} } |
| 190 | } |
| 191 | |
| 192 | // restartOnto quits so the dashboard can exec target, the klaus PATH names |
| 193 | // now. With no target, the pending restart and its notice are dropped. |
| 194 | func (m dashboardModel) restartOnto(target *binUpdateMsg, now time.Time) (dashboardModel, tea.Cmd) { |
| 195 | if target == nil { |
| 196 | m.restart, m.binNotice, m.binNoticeErr = nil, "", false |
| 197 | return m, nil |
| 198 | } |
| 199 | m.restart = target |
| 200 | m.binNotice, m.binNoticeErr = "klaus updated to "+target.version+"; restarting", false |
| 201 | if now.Sub(m.lastKeyAt) < restartQuiet { |
| 202 | return m, restartWaitCmd() // a key came in meanwhile |
| 203 | } |
| 204 | m.restarting = true |
| 205 | return m, tea.Quit |
| 206 | } |