summon-switcher 0.1.0

A tiny macOS command-line tool for opening, focusing, and cycling applications from declarative keybindings.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Optional summon daemon for a warm hot path.

pub mod client;
pub mod protocol;
pub mod server;

use std::ffi::OsStr;
use std::fs::OpenOptions;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::thread;
use std::time::{Duration, Instant};

use thiserror::Error;

use crate::daemon::client::ClientError;
use crate::daemon::protocol::{Request, RequestEnvelope, Status};
use crate::runner::{self, RunOutput};

const LAUNCH_AGENT_LABEL: &str = "dev.liamwh.summond";

/// How the CLI should use the daemon.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum DaemonMode {
    /// Try the daemon first and fall back to direct mode.
    #[default]
    Auto,
    /// Skip the daemon and always run direct mode.
    Off,
    /// Require the daemon; do not fall back to direct mode.
    Required,
}

impl DaemonMode {
    /// Resolves the daemon mode from the `SUMMON_DAEMON` environment variable.
    #[must_use]
    pub fn from_env() -> Self {
        match std::env::var("SUMMON_DAEMON")
            .ok()
            .as_deref()
            .map(str::trim)
        {
            Some("off" | "false" | "0" | "direct") => Self::Off,
            Some("required" | "on" | "true" | "1") => Self::Required,
            _ => Self::Auto,
        }
    }
}

/// Errors from daemon control operations.
#[derive(Debug, Error)]
pub enum DaemonError {
    /// The current user home directory could not be resolved.
    #[error("HOME environment variable is not set")]
    NoHome,

    /// The daemon could not be started.
    #[error("Could not start summon daemon: {0}")]
    Start(String),

    /// The daemon could not be stopped.
    #[error("Could not stop summon daemon: {0}")]
    Stop(String),

    /// The daemon did not become ready in time.
    #[error("Summon daemon did not become ready at {0}")]
    StartTimeout(String),

    /// The launch agent plist could not be written.
    #[error("Could not write summon launch agent at {path}: {reason}")]
    LaunchAgentWrite {
        /// The plist path.
        path: String,
        /// The underlying failure.
        reason: String,
    },

    /// A launchctl invocation failed.
    #[error("launchctl {command} failed: {reason}")]
    LaunchCtl {
        /// The launchctl command that failed.
        command: String,
        /// stderr or stdout from launchctl.
        reason: String,
    },

    /// The daemon client failed.
    #[error(transparent)]
    Client(#[from] ClientError),

    /// The daemon server failed.
    #[error(transparent)]
    Server(#[from] server::ServerError),
}

/// Runs a binding through the daemon when configured, otherwise direct mode.
pub fn run_binding_or_direct(name: &str, config_path: &Path, verbose: u8) -> RunOutput {
    let request = RequestEnvelope::new(Request::RunBinding {
        name: name.to_string(),
        config_path: config_path.to_path_buf(),
        verbose,
    });

    match DaemonMode::from_env() {
        DaemonMode::Off => runner::run_binding_from_path(name, config_path, verbose),
        DaemonMode::Required => run_required(request),
        DaemonMode::Auto => run_auto(request, || {
            runner::run_binding_from_path(name, config_path, verbose)
        }),
    }
}

/// Runs an app request through the daemon when configured, otherwise direct mode.
pub fn run_app_or_direct(app: &str, verbose: u8) -> RunOutput {
    let request = RequestEnvelope::new(Request::RunApp {
        app: app.to_string(),
        verbose,
    });

    match DaemonMode::from_env() {
        DaemonMode::Off => runner::run_app(app, verbose),
        DaemonMode::Required => run_required(request),
        DaemonMode::Auto => run_auto(request, || runner::run_app(app, verbose)),
    }
}

/// Starts the daemon and waits until it is ready.
///
/// # Errors
///
/// Returns an error if the daemon could not be started or did not become ready.
pub fn start() -> Result<Status, DaemonError> {
    let socket_path = socket_path()?;

    if let Ok(status) = client::ping(&socket_path) {
        return Ok(status);
    }

    ensure_started()?;
    wait_until_ready(&socket_path, Duration::from_secs(2))
}

/// Returns the current daemon status.
pub fn status() -> Result<Status, DaemonError> {
    let socket_path = socket_path()?;
    client::ping(&socket_path).map_err(Into::into)
}

/// Resolves the daemon log path.
pub fn log_path() -> Result<PathBuf, DaemonError> {
    if let Ok(path) = std::env::var("SUMMOND_LOG_PATH") {
        return Ok(PathBuf::from(path));
    }

    let home = std::env::var("HOME").map_err(|_| DaemonError::NoHome)?;
    Ok(PathBuf::from(home)
        .join("Library")
        .join("Logs")
        .join("summon")
        .join("summond.log"))
}

/// Stops the daemon if it is running.
pub fn stop() -> Result<(), DaemonError> {
    if should_use_launch_agent() {
        stop_launch_agent()?;
        return Ok(());
    }

    let socket_path = socket_path()?;
    client::stop(&socket_path).map_err(Into::into)
}

/// Runs the foreground daemon server loop.
pub fn run_server() -> Result<(), DaemonError> {
    let socket_path = socket_path()?;
    server::serve(&socket_path).map_err(Into::into)
}

/// Resolves the daemon socket path.
pub fn socket_path() -> Result<PathBuf, DaemonError> {
    if let Ok(path) = std::env::var("SUMMOND_SOCKET_PATH") {
        return Ok(PathBuf::from(path));
    }

    if let Ok(path) = std::env::var("XDG_CACHE_HOME") {
        return Ok(PathBuf::from(path).join("summon").join("summond.sock"));
    }

    let home = std::env::var("HOME").map_err(|_| DaemonError::NoHome)?;
    Ok(PathBuf::from(home)
        .join(".cache")
        .join("summon")
        .join("summond.sock"))
}

fn run_auto<F>(request: RequestEnvelope, fallback: F) -> RunOutput
where
    F: FnOnce() -> RunOutput,
{
    let socket_path = match socket_path() {
        Ok(path) => path,
        Err(_) => return fallback(),
    };
    let mut fallback = Some(fallback);

    match client::run(&socket_path, request) {
        Ok(output) => maybe_fallback_to_direct(output, || {
            fallback
                .take()
                .expect("fallback should only be consumed once")()
        }),
        Err(ClientError::Unavailable { .. }) => {
            let _ = ensure_started();
            fallback
                .take()
                .expect("fallback should only be consumed once")()
        }
        Err(_) => fallback
            .take()
            .expect("fallback should only be consumed once")(),
    }
}

fn run_required(request: RequestEnvelope) -> RunOutput {
    let socket_path = match socket_path() {
        Ok(path) => path,
        Err(err) => return daemon_failure(err.to_string()),
    };

    match client::run(&socket_path, request) {
        Ok(output) => output,
        Err(err) => daemon_failure(err.to_string()),
    }
}

fn ensure_started() -> Result<(), DaemonError> {
    if should_use_launch_agent() {
        install_or_restart_launch_agent()
    } else {
        spawn_transient_process()
    }
}

fn should_use_launch_agent() -> bool {
    std::env::var_os("SUMMOND_SOCKET_PATH").is_none()
}

fn install_or_restart_launch_agent() -> Result<(), DaemonError> {
    let plist_path = launch_agent_path()?;
    let current_exe = std::env::current_exe().map_err(|err| DaemonError::Start(err.to_string()))?;
    let socket_path = socket_path()?;
    let log_path = log_path()?;

    if let Some(parent) = plist_path.parent() {
        std::fs::create_dir_all(parent).map_err(|err| DaemonError::LaunchAgentWrite {
            path: plist_path.display().to_string(),
            reason: err.to_string(),
        })?;
    }
    if let Some(parent) = log_path.parent() {
        std::fs::create_dir_all(parent).map_err(|err| DaemonError::LaunchAgentWrite {
            path: parent.display().to_string(),
            reason: err.to_string(),
        })?;
    }

    std::fs::write(
        &plist_path,
        launch_agent_plist(&current_exe, &socket_path, &log_path),
    )
    .map_err(|err| DaemonError::LaunchAgentWrite {
        path: plist_path.display().to_string(),
        reason: err.to_string(),
    })?;

    let domain = launchctl_domain();
    let plist = plist_path.as_os_str();
    let _ = launchctl(
        [OsStr::new("bootout"), OsStr::new(&domain), plist],
        "bootout",
        true,
    );
    launchctl(
        [OsStr::new("bootstrap"), OsStr::new(&domain), plist],
        "bootstrap",
        false,
    )?;
    launchctl(
        [
            OsStr::new("kickstart"),
            OsStr::new("-k"),
            OsStr::new(&format!("{domain}/{LAUNCH_AGENT_LABEL}")),
        ],
        "kickstart",
        false,
    )?;

    Ok(())
}

fn stop_launch_agent() -> Result<(), DaemonError> {
    let domain = launchctl_domain();
    let plist_path = launch_agent_path()?;
    let _ = launchctl(
        [
            OsStr::new("bootout"),
            OsStr::new(&domain),
            plist_path.as_os_str(),
        ],
        "bootout",
        true,
    );

    let socket_path = socket_path()?;
    let deadline = Instant::now() + Duration::from_secs(2);
    loop {
        match client::ping(&socket_path) {
            Err(ClientError::Unavailable { .. }) => {
                let _ = std::fs::remove_file(&socket_path);
                return Ok(());
            }
            Err(_) => return Ok(()),
            Ok(_) if Instant::now() < deadline => thread::sleep(Duration::from_millis(50)),
            Ok(_) => {
                return Err(DaemonError::Stop(format!(
                    "daemon at {} did not stop after launchctl bootout",
                    socket_path.display()
                )));
            }
        }
    }
}

fn launch_agent_path() -> Result<PathBuf, DaemonError> {
    let home = std::env::var("HOME").map_err(|_| DaemonError::NoHome)?;
    Ok(PathBuf::from(home)
        .join("Library")
        .join("LaunchAgents")
        .join(format!("{LAUNCH_AGENT_LABEL}.plist")))
}

fn launch_agent_plist(executable: &Path, socket_path: &Path, log_path: &Path) -> String {
    format!(
        r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>{label}</string>
  <key>ProgramArguments</key>
  <array>
    <string>{exe}</string>
    <string>daemon</string>
    <string>run</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <true/>
  <key>EnvironmentVariables</key>
  <dict>
    <key>SUMMOND_SOCKET_PATH</key>
    <string>{socket_path}</string>
    <key>SUMMOND_LOG_PATH</key>
    <string>{log_path}</string>
  </dict>
  <key>StandardOutPath</key>
  <string>{log_path}</string>
  <key>StandardErrorPath</key>
  <string>{log_path}</string>
  <key>ProcessType</key>
  <string>Interactive</string>
  <key>LimitLoadToSessionType</key>
  <array>
    <string>Aqua</string>
  </array>
</dict>
</plist>
"#,
        label = LAUNCH_AGENT_LABEL,
        exe = executable.display(),
        socket_path = socket_path.display(),
        log_path = log_path.display()
    )
}

fn launchctl_domain() -> String {
    format!("gui/{}", unsafe { libc::geteuid() })
}

fn launchctl<I, S>(args: I, command: &str, allow_failure: bool) -> Result<(), DaemonError>
where
    I: IntoIterator<Item = S>,
    S: AsRef<OsStr>,
{
    let output = std::process::Command::new("launchctl")
        .args(args)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .map_err(|err| DaemonError::LaunchCtl {
            command: command.to_string(),
            reason: err.to_string(),
        })?;

    if allow_failure || output.status.success() {
        return Ok(());
    }

    let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
    let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
    let reason = if !stderr.is_empty() { stderr } else { stdout };

    Err(DaemonError::LaunchCtl {
        command: command.to_string(),
        reason,
    })
}

fn spawn_transient_process() -> Result<(), DaemonError> {
    let socket_path = socket_path()?;
    let log_path = log_path()?;
    if let Some(parent) = socket_path.parent() {
        std::fs::create_dir_all(parent).map_err(|err| DaemonError::Start(err.to_string()))?;
    }
    if let Some(parent) = log_path.parent() {
        std::fs::create_dir_all(parent).map_err(|err| DaemonError::Start(err.to_string()))?;
    }

    let log_file = OpenOptions::new()
        .create(true)
        .append(true)
        .open(&log_path)
        .map_err(|err| DaemonError::Start(err.to_string()))?;
    let log_file_clone = log_file
        .try_clone()
        .map_err(|err| DaemonError::Start(err.to_string()))?;

    let current_exe = std::env::current_exe().map_err(|err| DaemonError::Start(err.to_string()))?;
    std::process::Command::new(current_exe)
        .args(["daemon", "run"])
        .stdin(Stdio::null())
        .stdout(Stdio::from(log_file_clone))
        .stderr(Stdio::from(log_file))
        .spawn()
        .map(|_| ())
        .map_err(|err| DaemonError::Start(err.to_string()))
}

fn wait_until_ready(socket_path: &Path, timeout: Duration) -> Result<Status, DaemonError> {
    let deadline = Instant::now() + timeout;
    loop {
        match client::ping(socket_path) {
            Ok(status) => return Ok(status),
            Err(_) if Instant::now() < deadline => thread::sleep(Duration::from_millis(50)),
            Err(_) => {
                return Err(DaemonError::StartTimeout(socket_path.display().to_string()));
            }
        }
    }
}

fn daemon_failure(message: String) -> RunOutput {
    RunOutput {
        success: false,
        should_fallback_direct: false,
        stdout: String::new(),
        stderr: format!("Daemon error: {message}\n"),
    }
}

fn maybe_fallback_to_direct<F>(daemon_output: RunOutput, fallback: F) -> RunOutput
where
    F: FnOnce() -> RunOutput,
{
    if daemon_output.success || !daemon_output.should_fallback_direct {
        return daemon_output;
    }

    let direct_output = fallback();
    if direct_output.success {
        return direct_output;
    }

    RunOutput {
        success: false,
        should_fallback_direct: false,
        stdout: direct_output.stdout,
        stderr: format!(
            "{}Direct fallback also failed:\n{}",
            daemon_output.stderr, direct_output.stderr
        ),
    }
}

#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
    use super::*;

    #[test]
    fn launch_agent_plist_includes_shared_log_path() {
        let plist = launch_agent_plist(
            Path::new("/Users/test/bin/summon"),
            Path::new("/Users/test/.cache/summon/summond.sock"),
            Path::new("/Users/test/Library/Logs/summon/summond.log"),
        );

        assert!(plist.contains("<key>SUMMOND_SOCKET_PATH</key>"));
        assert!(plist.contains("<key>StandardOutPath</key>"));
        assert!(plist.contains("<key>StandardErrorPath</key>"));
        assert!(plist.contains("/Users/test/Library/Logs/summon/summond.log"));
    }

    #[test]
    fn direct_fallback_prefers_successful_direct_run() {
        let daemon_output = RunOutput {
            success: false,
            should_fallback_direct: true,
            stdout: String::new(),
            stderr: "daemon failed\n".into(),
        };

        let direct_output = maybe_fallback_to_direct(daemon_output, || RunOutput {
            success: true,
            should_fallback_direct: false,
            stdout: String::new(),
            stderr: String::new(),
        });

        assert!(direct_output.success);
    }

    #[test]
    fn direct_fallback_preserves_daemon_context_when_both_fail() {
        let output = maybe_fallback_to_direct(
            RunOutput {
                success: false,
                should_fallback_direct: true,
                stdout: String::new(),
                stderr: "daemon failed\n".into(),
            },
            || RunOutput {
                success: false,
                should_fallback_direct: false,
                stdout: String::new(),
                stderr: "direct failed\n".into(),
            },
        );

        assert!(!output.success);
        assert!(output.stderr.contains("daemon failed"));
        assert!(output.stderr.contains("Direct fallback also failed"));
        assert!(output.stderr.contains("direct failed"));
    }
}