Start ssh-agent when adding a key and none is running

ysm starts its own agent on $XDG_RUNTIME_DIR/ysm-agent.sock, reconnects
to it on later runs, and shows agent status in the header.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aleksey ShakhmatovandClaude Opus 5.5 committed 2026-10-30 15:18:59 +03:00
1 parent 9408ab31a9
commit f680bf3b9d
4 files changed
+119 -7

No files matched your search

+34 -6
View File
@@ -40,8 +40,14 @@ const (
type (
keysMsg struct {
keys []Key
err error
keys []Key
agent bool
err error
}
agentMsg struct {
started bool
err error
key Key
}
ykMsg string
tickMsg struct{}
@@ -106,6 +112,7 @@ type model struct {
form *form
detail string
yubikey string
agent bool
busy bool // an external command owns the terminal / YubiKey
status string
statusB bool // status is an error
@@ -122,7 +129,7 @@ func defaultComment() string {
}
func (m model) loadCmd() tea.Cmd {
return func() tea.Msg { k, err := loadKeys(m.dir); return keysMsg{k, err} }
return func() tea.Msg { k, err := loadKeys(m.dir); return keysMsg{k, agentRunning(), err} }
}
func ykCmd() tea.Msg { return ykMsg(yubikeyStatus()) }
@@ -154,7 +161,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if msg.err != nil {
m.setStatus(msg.err.Error(), true)
}
m.keys = msg.keys
m.keys, m.agent = msg.keys, msg.agent
m.cursor = min(m.cursor, max(len(m.keys)-1, 0))
case tickMsg:
if m.busy { // don't poke the YubiKey while ssh-keygen waits for touch
@@ -169,6 +176,17 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} else if was != "" && m.yubikey == "" {
m.setStatus("YubiKey removed", false)
}
case agentMsg:
if msg.err != nil {
m.setStatus("✗ "+msg.err.Error(), true)
return m, nil
}
m.agent = true
what := "add " + msg.key.Name + " to agent"
if msg.started {
what = "start ssh-agent and " + what
}
return m, m.run(what, interactive(m.dir, "ssh-add", msg.key.Path))
case detailMsg:
m.detail, m.mode = string(msg), detailMode
case execMsg:
@@ -289,7 +307,8 @@ func (m model) updateList(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
return m, m.run("import keys from YubiKey", interactive(m.dir, "ssh-keygen", "-K"))
case "a":
if needPriv() {
return m, m.run("add "+k.Name+" to agent", interactive(m.dir, "ssh-add", k.Path))
key := *k
return m, func() tea.Msg { started, err := ensureAgent(); return agentMsg{started, err, key} }
}
case "x":
if needKey() {
@@ -412,7 +431,15 @@ func (m model) View() string {
if m.yubikey != "" {
yk = goodSt.Render("● " + m.yubikey)
}
b.WriteString(titleSt.Render("🔑 ysm") + mutedSt.Render(" — SSH keys in "+m.dir) + " " + yk + "\n\n")
agent := badSt.Render("○ no ssh-agent") + mutedSt.Render(" (a starts one)")
if m.agent {
agent = goodSt.Render("● ssh-agent")
if sock := os.Getenv("SSH_AUTH_SOCK"); sock == agentSocket() {
// ysm's own agent: the user's shell needs this to use it.
agent += mutedSt.Render(" export SSH_AUTH_SOCK=" + sock)
}
}
b.WriteString(titleSt.Render("🔑 ysm") + mutedSt.Render(" — SSH keys in "+m.dir) + " " + yk + "\n" + agent + "\n\n")
switch m.mode {
case detailMode:
@@ -541,6 +568,7 @@ func main() {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
connectAgent()
if _, err := tea.NewProgram(model{dir: dir}, tea.WithAltScreen()).Run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)