auths-cli 0.0.1-rc.8

Command-line interface for Auths decentralized identity system
Documentation
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
//! SSH agent daemon commands (start, stop, status).

use anyhow::{Context, Result, anyhow};
use clap::{Parser, Subcommand, ValueEnum};
use serde::Serialize;
use std::fs;
use std::path::PathBuf;

use crate::core::fs::{create_restricted_dir, write_sensitive_file};
use crate::ux::format::{JsonResponse, is_json_mode};

#[cfg(unix)]
use nix::sys::signal::{self, Signal};
#[cfg(unix)]
use nix::unistd::Pid;

/// Default socket filename within ~/.auths
const DEFAULT_SOCKET_NAME: &str = "agent.sock";
/// PID file name
const PID_FILE_NAME: &str = "agent.pid";
/// Environment file for SSH_AUTH_SOCK
const ENV_FILE_NAME: &str = "agent.env";
/// Log file for daemon output
const LOG_FILE_NAME: &str = "agent.log";

#[derive(Parser, Debug, Clone)]
#[command(
    name = "agent",
    about = "SSH agent daemon management (start, stop, status)."
)]
pub struct AgentCommand {
    #[command(subcommand)]
    pub command: AgentSubcommand,
}

#[derive(Subcommand, Debug, Clone)]
pub enum AgentSubcommand {
    /// Start the SSH agent daemon
    Start {
        /// Custom socket path (default: ~/.auths/agent.sock)
        #[arg(long, help = "Custom Unix socket path")]
        socket: Option<PathBuf>,

        /// Run in foreground (don't daemonize)
        #[arg(long, help = "Run in foreground instead of daemonizing")]
        foreground: bool,

        /// Idle timeout (e.g., "30m", "1h", "0" for never)
        #[arg(long, default_value = "30m", help = "Idle timeout before auto-lock")]
        timeout: String,
    },

    /// Stop the SSH agent daemon
    Stop,

    /// Show agent status
    Status,

    /// Output shell environment for SSH_AUTH_SOCK (use with eval)
    Env {
        /// Shell format for output
        #[arg(long, value_enum, default_value = "bash", help = "Shell format")]
        shell: ShellFormat,
    },

    /// Lock the agent (clear keys from memory)
    Lock,

    /// Unlock the agent (re-load keys)
    Unlock {
        /// Key alias to unlock
        #[arg(
            long = "agent-key-alias",
            visible_alias = "key",
            default_value = "default",
            help = "Key alias to unlock"
        )]
        agent_key_alias: String,
    },

    /// Install as a system service (launchd on macOS, systemd on Linux)
    InstallService {
        /// Don't install, just print the service file
        #[arg(long, help = "Print service file without installing")]
        dry_run: bool,

        /// Force overwrite if service already exists
        #[arg(long, help = "Overwrite existing service file")]
        force: bool,

        /// Service manager to use (auto-detect if not specified)
        #[arg(long, value_enum, help = "Service manager (auto-detect by default)")]
        manager: Option<ServiceManager>,
    },

    /// Uninstall the system service
    UninstallService,
}

/// Service manager type for platform-specific service installation
#[derive(ValueEnum, Clone, Debug, PartialEq)]
pub enum ServiceManager {
    /// macOS launchd
    Launchd,
    /// Linux systemd (user mode)
    Systemd,
}

/// Shell format for environment output
#[derive(ValueEnum, Clone, Debug, Default)]
pub enum ShellFormat {
    #[default]
    Bash,
    Zsh,
    Fish,
}

/// Status information about the agent
#[derive(Serialize, Debug)]
pub struct AgentStatus {
    pub running: bool,
    pub pid: Option<u32>,
    pub socket_path: Option<String>,
    pub socket_exists: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uptime_secs: Option<u64>,
}

/// Ensures the SSH agent is running, starting it if necessary.
///
/// Returns `Ok(true)` if the agent was already running, `Ok(false)` if it was started.
/// Returns an error if the agent could not be started.
///
/// This function can be called from commands that need the agent to be running
/// before performing signing operations, enabling auto-start functionality.
#[allow(dead_code)] // Used by bin/sign.rs (cross-target usage not tracked by lint)
pub fn ensure_agent_running(quiet: bool) -> Result<bool> {
    let socket_path = get_default_socket_path()?;

    // Check if already running via connection attempt (avoids TOCTOU on socket path)
    if let Some(pid) = read_pid()?
        && is_process_running(pid)
        && socket_is_connectable(&socket_path)
    {
        return Ok(true); // Already running
    }

    // Start agent
    if !quiet {
        eprintln!("Agent not running, starting...");
    }

    // Use default timeout of 30m
    start_agent(None, false, "30m", quiet)?;

    // Poll for socket connectivity with 2s timeout
    let timeout = std::time::Duration::from_secs(2);
    let start = std::time::Instant::now();
    while start.elapsed() < timeout {
        if let Some(pid) = read_pid()?
            && is_process_running(pid)
            && socket_is_connectable(&socket_path)
        {
            if !quiet {
                eprintln!("Agent started (PID {})", pid);
            }
            return Ok(false); // Just started
        }
        std::thread::sleep(std::time::Duration::from_millis(100));
    }

    Err(anyhow!("Failed to start agent within 2 seconds"))
}

pub fn handle_agent(cmd: AgentCommand) -> Result<()> {
    match cmd.command {
        AgentSubcommand::Start {
            socket,
            foreground,
            timeout,
        } => start_agent(socket, foreground, &timeout, false),
        AgentSubcommand::Stop => stop_agent(),
        AgentSubcommand::Status => show_status(),
        AgentSubcommand::Env { shell } => output_env(shell),
        AgentSubcommand::Lock => lock_agent(),
        AgentSubcommand::Unlock { agent_key_alias } => unlock_agent(&agent_key_alias),
        AgentSubcommand::InstallService {
            dry_run,
            force,
            manager,
        } => install_service(dry_run, force, manager),
        AgentSubcommand::UninstallService => uninstall_service(),
    }
}

/// Parse a timeout string like "30m", "1h", "0", "5s"
fn parse_timeout(s: &str) -> Result<std::time::Duration> {
    use std::time::Duration;

    let s = s.trim();
    if s == "0" {
        return Ok(Duration::ZERO);
    }

    // Try to parse as number + suffix using strip_suffix
    let (num_str, suffix) = if let Some(stripped) = s.strip_suffix('s') {
        (stripped, "s")
    } else if let Some(stripped) = s.strip_suffix('m') {
        (stripped, "m")
    } else if let Some(stripped) = s.strip_suffix('h') {
        (stripped, "h")
    } else {
        // Assume minutes if no suffix
        (s, "m")
    };

    let num: u64 = num_str
        .parse()
        .with_context(|| format!("Invalid timeout number: {}", num_str))?;

    let secs = match suffix {
        "s" => num,
        "m" => num * 60,
        "h" => num * 3600,
        _ => return Err(anyhow!("Invalid timeout suffix: {}", suffix)),
    };

    Ok(Duration::from_secs(secs))
}

/// Get the auths directory path (~/.auths), respecting AUTHS_HOME.
fn get_auths_dir() -> Result<PathBuf> {
    auths_core::paths::auths_home().map_err(|e| anyhow!(e))
}

/// Get the default socket path
pub fn get_default_socket_path() -> Result<PathBuf> {
    Ok(get_auths_dir()?.join(DEFAULT_SOCKET_NAME))
}

/// Get the PID file path
fn get_pid_file_path() -> Result<PathBuf> {
    Ok(get_auths_dir()?.join(PID_FILE_NAME))
}

/// Get the environment file path
fn get_env_file_path() -> Result<PathBuf> {
    Ok(get_auths_dir()?.join(ENV_FILE_NAME))
}

/// Get the log file path
fn get_log_file_path() -> Result<PathBuf> {
    Ok(get_auths_dir()?.join(LOG_FILE_NAME))
}

/// Read PID from file
fn read_pid() -> Result<Option<u32>> {
    let pid_path = get_pid_file_path()?;
    if !pid_path.exists() {
        return Ok(None);
    }

    let content = fs::read_to_string(&pid_path)
        .with_context(|| format!("Failed to read PID file: {:?}", pid_path))?;

    let pid: u32 = content
        .trim()
        .parse()
        .with_context(|| format!("Invalid PID in file: {}", content.trim()))?;

    Ok(Some(pid))
}

/// Check if a process with the given PID is running
#[cfg(unix)]
fn is_process_running(pid: u32) -> bool {
    // Try to send signal 0 (doesn't actually send anything, just checks if process exists)
    signal::kill(Pid::from_raw(pid as i32), None).is_ok()
}

#[cfg(not(unix))]
fn is_process_running(_pid: u32) -> bool {
    false
}

/// Test if the agent socket is connectable (avoids TOCTOU vs exists() check).
#[cfg(unix)]
fn socket_is_connectable(path: &std::path::Path) -> bool {
    std::os::unix::net::UnixStream::connect(path).is_ok()
}

#[cfg(not(unix))]
fn socket_is_connectable(_path: &std::path::Path) -> bool {
    false
}

/// Start the agent
fn start_agent(
    socket_path: Option<PathBuf>,
    foreground: bool,
    timeout_str: &str,
    quiet: bool,
) -> Result<()> {
    let auths_dir = get_auths_dir()?;
    create_restricted_dir(&auths_dir)
        .with_context(|| format!("Failed to create auths directory: {:?}", auths_dir))?;

    let socket = socket_path.unwrap_or_else(|| get_default_socket_path().unwrap());
    let pid_path = get_pid_file_path()?;
    let env_path = get_env_file_path()?;
    let timeout = parse_timeout(timeout_str)?;

    // Check if already running
    if let Some(pid) = read_pid()? {
        if is_process_running(pid) {
            return Err(anyhow!(
                "Agent already running (PID {}). Use 'auths agent stop' first.",
                pid
            ));
        }
        // Stale PID file, clean it up
        let _ = fs::remove_file(&pid_path);
    }

    // Remove stale socket atomically (no TOCTOU exists-then-remove)
    match fs::remove_file(&socket) {
        Ok(()) => {}
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
        Err(e) => {
            return Err(anyhow!("Failed to remove stale socket {:?}: {}", socket, e));
        }
    }

    if foreground {
        // Run in foreground
        run_agent_foreground(&socket, &pid_path, &env_path, timeout)
    } else {
        // Daemonize
        #[cfg(unix)]
        {
            daemonize_agent(
                &socket,
                &pid_path,
                &env_path,
                &get_log_file_path()?,
                timeout_str,
                quiet,
            )
        }
        #[cfg(not(unix))]
        {
            Err(anyhow!(
                "Daemonization not supported on this platform. Use --foreground."
            ))
        }
    }
}

/// Run the agent in the foreground (Unix only)
#[cfg(unix)]
fn run_agent_foreground(
    socket: &PathBuf,
    pid_path: &PathBuf,
    env_path: &PathBuf,
    timeout: std::time::Duration,
) -> Result<()> {
    use auths_core::AgentHandle;
    use std::sync::Arc;

    // Write PID file
    let pid = std::process::id();
    write_sensitive_file(pid_path, pid.to_string())
        .with_context(|| format!("Failed to write PID file: {:?}", pid_path))?;

    // Write environment file
    let socket_str = socket
        .to_str()
        .ok_or_else(|| anyhow!("Socket path is not valid UTF-8"))?;
    let env_content = format!("export SSH_AUTH_SOCK=\"{}\"\n", socket_str);
    write_sensitive_file(env_path, &env_content)
        .with_context(|| format!("Failed to write env file: {:?}", env_path))?;

    eprintln!("Starting SSH agent (foreground)...");
    eprintln!("Socket: {}", socket_str);
    eprintln!("PID: {}", pid);
    if timeout.is_zero() {
        eprintln!("Idle timeout: disabled");
    } else {
        eprintln!("Idle timeout: {:?}", timeout);
    }
    eprintln!();
    eprintln!("To use this agent in your shell:");
    eprintln!("  eval $(cat {})", env_path.display());
    eprintln!("  # or");
    eprintln!("  export SSH_AUTH_SOCK=\"{}\"", socket_str);
    eprintln!();
    eprintln!("Press Ctrl+C to stop.");

    // Create agent handle with timeout
    let handle = Arc::new(AgentHandle::with_pid_file_and_timeout(
        socket.clone(),
        pid_path.clone(),
        timeout,
    ));

    // Run the listener
    let rt = tokio::runtime::Runtime::new().context("Failed to create tokio runtime")?;
    let result = rt.block_on(async {
        auths_core::api::start_agent_listener_with_handle(handle.clone()).await
    });

    // Cleanup on exit
    let _ = fs::remove_file(pid_path);
    let _ = fs::remove_file(env_path);
    let _ = fs::remove_file(socket);

    result.map_err(|e| anyhow!("Agent error: {}", e))
}

/// Run the agent in the foreground (non-Unix stub)
#[cfg(not(unix))]
fn run_agent_foreground(
    _socket: &PathBuf,
    _pid_path: &PathBuf,
    _env_path: &PathBuf,
    _timeout: std::time::Duration,
) -> Result<()> {
    Err(anyhow!(
        "SSH agent is not supported on this platform (requires Unix domain sockets)"
    ))
}

/// Daemonize the agent process (Unix only)
#[cfg(unix)]
fn daemonize_agent(
    socket: &std::path::Path,
    _pid_path: &std::path::Path,
    env_path: &std::path::Path,
    log_path: &std::path::Path,
    timeout_str: &str,
    quiet: bool,
) -> Result<()> {
    use std::os::unix::process::CommandExt;
    use std::process::Command;

    let socket_str = socket
        .to_str()
        .ok_or_else(|| anyhow!("Socket path is not valid UTF-8"))?;

    // Get the path to the current executable
    let exe = std::env::current_exe().context("Failed to get current executable path")?;

    // Fork by re-executing ourselves with --foreground
    // The child will detach and become the daemon
    let log_file = fs::File::create(log_path)
        .with_context(|| format!("Failed to create log file: {:?}", log_path))?;
    let log_file_err = log_file
        .try_clone()
        .context("Failed to clone log file handle")?;

    let mut cmd = Command::new(&exe);
    cmd.arg("agent")
        .arg("start")
        .arg("--foreground")
        .arg("--socket")
        .arg(socket_str)
        .arg("--timeout")
        .arg(timeout_str)
        .stdout(log_file)
        .stderr(log_file_err);

    // Use process_group(0) to create a new process group (detach from terminal)
    unsafe {
        cmd.pre_exec(|| {
            // Create new session (detach from controlling terminal)
            nix::unistd::setsid().map_err(std::io::Error::other)?;
            Ok(())
        });
    }

    let child = cmd.spawn().context("Failed to spawn daemon process")?;

    if !quiet {
        eprintln!("Agent daemon started (PID {})", child.id());
        eprintln!("Socket: {}", socket_str);
        eprintln!("Log file: {}", log_path.display());
        eprintln!();
        eprintln!("To use this agent:");
        eprintln!("  eval $(auths agent env)");
        eprintln!("  # or");
        eprintln!("  export SSH_AUTH_SOCK=\"{}\"", socket_str);
    }

    // Write environment file for the parent to report
    let env_content = format!("export SSH_AUTH_SOCK=\"{}\"\n", socket_str);
    write_sensitive_file(env_path, &env_content)
        .with_context(|| format!("Failed to write env file: {:?}", env_path))?;

    Ok(())
}

/// Stop the agent
fn stop_agent() -> Result<()> {
    let pid_path = get_pid_file_path()?;
    let socket_path = get_default_socket_path()?;
    let env_path = get_env_file_path()?;

    let pid = read_pid()?
        .ok_or_else(|| anyhow!("Agent not running (no PID file found at {:?})", pid_path))?;

    if !is_process_running(pid) {
        // Process not running, clean up stale files
        eprintln!("Agent process {} not found. Cleaning up stale files.", pid);
        let _ = fs::remove_file(&pid_path);
        let _ = fs::remove_file(&socket_path);
        let _ = fs::remove_file(&env_path);
        return Ok(());
    }

    eprintln!("Stopping agent (PID {})...", pid);

    // Send SIGTERM
    #[cfg(unix)]
    {
        signal::kill(Pid::from_raw(pid as i32), Signal::SIGTERM)
            .with_context(|| format!("Failed to send SIGTERM to PID {}", pid))?;
    }

    #[cfg(not(unix))]
    {
        return Err(anyhow!("Stopping agent not supported on this platform"));
    }

    #[cfg(unix)]
    {
        // Wait for process to terminate (with timeout)
        let start = std::time::Instant::now();
        let timeout = std::time::Duration::from_secs(5);

        while start.elapsed() < timeout {
            if !is_process_running(pid) {
                break;
            }
            std::thread::sleep(std::time::Duration::from_millis(100));
        }

        if is_process_running(pid) {
            eprintln!("Process did not terminate gracefully, sending SIGKILL...");
            let _ = signal::kill(Pid::from_raw(pid as i32), Signal::SIGKILL);
        }

        // Clean up files
        let _ = fs::remove_file(&pid_path);
        let _ = fs::remove_file(&socket_path);
        let _ = fs::remove_file(&env_path);

        eprintln!("Agent stopped.");
        Ok(())
    }
}

/// Show agent status
fn show_status() -> Result<()> {
    let pid_path = get_pid_file_path()?;
    let socket_path = get_default_socket_path()?;

    let pid = read_pid()?;
    let running = pid.map(is_process_running).unwrap_or(false);
    let socket_exists = socket_path.exists();

    let status = AgentStatus {
        running,
        pid: if running { pid } else { None },
        socket_path: if socket_exists {
            Some(socket_path.to_string_lossy().to_string())
        } else {
            None
        },
        socket_exists,
        uptime_secs: None, // Would need to track start time to implement
    };

    if is_json_mode() {
        JsonResponse::success("agent status", status).print()?;
    } else if running {
        eprintln!("Agent Status: RUNNING");
        if let Some(p) = status.pid {
            eprintln!("  PID: {}", p);
        }
        if let Some(ref sock) = status.socket_path {
            eprintln!("  Socket: {}", sock);
        }
        eprintln!();
        eprintln!("To use this agent:");
        eprintln!("  eval $(auths agent env)");
    } else {
        eprintln!("Agent Status: STOPPED");
        if pid.is_some() && !running {
            eprintln!("  (Stale PID file found at {:?})", pid_path);
        }
        eprintln!();
        eprintln!("To start the agent:");
        eprintln!("  auths agent start");
    }

    Ok(())
}

/// Output environment variables for shell integration
fn output_env(shell: ShellFormat) -> Result<()> {
    let socket_path = get_default_socket_path()?;

    // Check if agent is running
    let pid = read_pid()?;
    let running = pid.map(is_process_running).unwrap_or(false);

    if !running {
        eprintln!("Error: Agent is not running.");
        eprintln!("Start the agent with: auths agent start");
        std::process::exit(1);
    }

    // Check if socket exists
    if !socket_path.exists() {
        eprintln!("Error: Socket file not found at {:?}", socket_path);
        eprintln!("The agent may have crashed. Try: auths agent start");
        std::process::exit(1);
    }

    // Get socket path as string
    let socket_str = socket_path
        .to_str()
        .ok_or_else(|| anyhow!("Socket path is not valid UTF-8"))?;

    // Output in appropriate shell format
    match shell {
        ShellFormat::Bash | ShellFormat::Zsh => {
            println!("export SSH_AUTH_SOCK=\"{}\"", socket_str);
        }
        ShellFormat::Fish => {
            println!("set -x SSH_AUTH_SOCK \"{}\"", socket_str);
        }
    }

    Ok(())
}

/// Lock the agent (clear keys from memory).
/// IMPORTANT: Uses auths_core::agent::remove_all_identities which relies on Unix
/// domain sockets. Do NOT remove this #[cfg(unix)] — it will break Windows CI.
#[cfg(unix)]
fn lock_agent() -> Result<()> {
    let pid = read_pid()?;
    let running = pid.map(is_process_running).unwrap_or(false);

    if !running {
        return Err(anyhow!("Agent is not running"));
    }

    let socket_path = get_default_socket_path()?;
    auths_core::agent::remove_all_identities(&socket_path)
        .map_err(|e| anyhow!("Failed to lock agent: {}", e))?;

    eprintln!("Agent locked — all keys removed from memory.");
    eprintln!("Use `auths agent unlock <key-alias>` to reload a key.");

    Ok(())
}

#[cfg(not(unix))]
fn lock_agent() -> Result<()> {
    Err(anyhow!(
        "Agent lock is not supported on this platform (requires Unix domain sockets)"
    ))
}

/// Unlock the agent (re-load a key into memory).
/// IMPORTANT: Uses auths_core::agent::add_identity which relies on Unix domain
/// sockets. Do NOT remove this #[cfg(unix)] — it will break Windows CI.
#[cfg(unix)]
fn unlock_agent(key_alias: &str) -> Result<()> {
    let pid = read_pid()?;
    let running = pid.map(is_process_running).unwrap_or(false);

    if !running {
        return Err(anyhow!("Agent is not running"));
    }

    let socket_path = get_default_socket_path()?;

    // Load encrypted key from platform keychain
    let keychain = auths_core::storage::keychain::get_platform_keychain()
        .map_err(|e| anyhow!("Failed to get platform keychain: {}", e))?;
    let (_identity_did, _role, encrypted_data) = keychain
        .load_key(&auths_core::storage::keychain::KeyAlias::new_unchecked(
            key_alias,
        ))
        .map_err(|e| anyhow!("Failed to load key '{}': {}", key_alias, e))?;

    // Prompt for passphrase
    let passphrase = rpassword::prompt_password(format!("Passphrase for '{}': ", key_alias))
        .context("Failed to read passphrase")?;

    // Decrypt the key
    let key_bytes = auths_core::crypto::signer::decrypt_keypair(&encrypted_data, &passphrase)
        .map_err(|e| anyhow!("Failed to decrypt key '{}': {}", key_alias, e))?;

    // Add to agent
    auths_core::agent::add_identity(&socket_path, &key_bytes)
        .map_err(|e| anyhow!("Failed to add key to agent: {}", e))?;

    eprintln!("Agent unlocked — key '{}' loaded.", key_alias);

    Ok(())
}

#[cfg(not(unix))]
fn unlock_agent(_key_alias: &str) -> Result<()> {
    Err(anyhow!(
        "Agent unlock is not supported on this platform (requires Unix domain sockets)"
    ))
}

// --- Service Installation ---

/// Detect the available service manager on the current platform
fn detect_service_manager() -> Option<ServiceManager> {
    #[cfg(target_os = "macos")]
    {
        Some(ServiceManager::Launchd)
    }
    #[cfg(target_os = "linux")]
    {
        // Check if systemd is running
        if std::path::Path::new("/run/systemd/system").exists() {
            Some(ServiceManager::Systemd)
        } else {
            None
        }
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        None
    }
}

/// Get the launchd plist path
fn get_launchd_plist_path() -> Result<PathBuf> {
    let home = dirs::home_dir().ok_or_else(|| anyhow!("Could not determine home directory"))?;
    Ok(home
        .join("Library")
        .join("LaunchAgents")
        .join("com.auths.agent.plist"))
}

/// Get the systemd unit file path
fn get_systemd_unit_path() -> Result<PathBuf> {
    let home = dirs::home_dir().ok_or_else(|| anyhow!("Could not determine home directory"))?;
    Ok(home
        .join(".config")
        .join("systemd")
        .join("user")
        .join("auths-agent.service"))
}

/// Generate launchd plist content
fn generate_launchd_plist() -> Result<String> {
    let exe_path = std::env::current_exe().context("Failed to get current executable path")?;
    let exe_str = exe_path
        .to_str()
        .ok_or_else(|| anyhow!("Executable path is not valid UTF-8"))?;

    let socket_path = get_default_socket_path()?;
    let socket_str = socket_path
        .to_str()
        .ok_or_else(|| anyhow!("Socket path is not valid UTF-8"))?;

    let log_path = get_log_file_path()?;
    let log_str = log_path
        .to_str()
        .ok_or_else(|| anyhow!("Log path is not valid UTF-8"))?;

    Ok(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>com.auths.agent</string>
    <key>ProgramArguments</key>
    <array>
        <string>{exe}</string>
        <string>agent</string>
        <string>start</string>
        <string>--foreground</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>{log}</string>
    <key>StandardErrorPath</key>
    <string>{log}</string>
    <key>EnvironmentVariables</key>
    <dict>
        <key>SSH_AUTH_SOCK</key>
        <string>{socket}</string>
    </dict>
</dict>
</plist>
"#,
        exe = exe_str,
        log = log_str,
        socket = socket_str
    ))
}

/// Generate systemd unit file content
fn generate_systemd_unit() -> Result<String> {
    let exe_path = std::env::current_exe().context("Failed to get current executable path")?;
    let exe_str = exe_path
        .to_str()
        .ok_or_else(|| anyhow!("Executable path is not valid UTF-8"))?;

    Ok(format!(
        r#"[Unit]
Description=Auths SSH Agent
Documentation=https://github.com/auths-rs/auths

[Service]
Type=simple
ExecStart={exe} agent start --foreground
Restart=on-failure
RestartSec=5

[Install]
WantedBy=default.target
"#,
        exe = exe_str
    ))
}

/// Install the service
fn install_service(dry_run: bool, force: bool, manager: Option<ServiceManager>) -> Result<()> {
    let manager = manager
        .or_else(detect_service_manager)
        .ok_or_else(|| anyhow!("No supported service manager found on this platform"))?;

    match manager {
        ServiceManager::Launchd => install_launchd_service(dry_run, force),
        ServiceManager::Systemd => install_systemd_service(dry_run, force),
    }
}

/// Install launchd service (macOS)
fn install_launchd_service(dry_run: bool, force: bool) -> Result<()> {
    let plist_content = generate_launchd_plist()?;
    let plist_path = get_launchd_plist_path()?;

    if dry_run {
        eprintln!("Would install to: {}", plist_path.display());
        eprintln!();
        println!("{}", plist_content);
        return Ok(());
    }

    // Check if already exists
    if plist_path.exists() && !force {
        return Err(anyhow!(
            "Service already installed at {}. Use --force to overwrite.",
            plist_path.display()
        ));
    }

    // Create parent directory
    if let Some(parent) = plist_path.parent() {
        fs::create_dir_all(parent)
            .with_context(|| format!("Failed to create directory: {:?}", parent))?;
    }

    // Write plist file
    fs::write(&plist_path, &plist_content)
        .with_context(|| format!("Failed to write plist: {:?}", plist_path))?;

    eprintln!("Installed launchd service: {}", plist_path.display());
    eprintln!();
    eprintln!("To start the service now:");
    eprintln!("  launchctl load {}", plist_path.display());
    eprintln!();
    eprintln!("The agent will start automatically on login.");

    Ok(())
}

/// Install systemd service (Linux)
fn install_systemd_service(dry_run: bool, force: bool) -> Result<()> {
    let unit_content = generate_systemd_unit()?;
    let unit_path = get_systemd_unit_path()?;

    if dry_run {
        eprintln!("Would install to: {}", unit_path.display());
        eprintln!();
        println!("{}", unit_content);
        return Ok(());
    }

    // Check if already exists
    if unit_path.exists() && !force {
        return Err(anyhow!(
            "Service already installed at {}. Use --force to overwrite.",
            unit_path.display()
        ));
    }

    // Create parent directory
    if let Some(parent) = unit_path.parent() {
        fs::create_dir_all(parent)
            .with_context(|| format!("Failed to create directory: {:?}", parent))?;
    }

    // Write unit file
    fs::write(&unit_path, &unit_content)
        .with_context(|| format!("Failed to write unit file: {:?}", unit_path))?;

    eprintln!("Installed systemd service: {}", unit_path.display());
    eprintln!();
    eprintln!("To enable and start the service:");
    eprintln!("  systemctl --user daemon-reload");
    eprintln!("  systemctl --user enable --now auths-agent");
    eprintln!();
    eprintln!("The agent will start automatically on login.");

    Ok(())
}

/// Uninstall the service
fn uninstall_service() -> Result<()> {
    let manager = detect_service_manager()
        .ok_or_else(|| anyhow!("No supported service manager found on this platform"))?;

    match manager {
        ServiceManager::Launchd => uninstall_launchd_service(),
        ServiceManager::Systemd => uninstall_systemd_service(),
    }
}

/// Uninstall launchd service (macOS)
fn uninstall_launchd_service() -> Result<()> {
    let plist_path = get_launchd_plist_path()?;

    if !plist_path.exists() {
        return Err(anyhow!("Service not installed at {}", plist_path.display()));
    }

    eprintln!("Unloading launchd service...");
    let _ = std::process::Command::new("launchctl")
        .arg("unload")
        .arg(&plist_path)
        .status();

    fs::remove_file(&plist_path)
        .with_context(|| format!("Failed to remove plist: {:?}", plist_path))?;

    eprintln!("Uninstalled launchd service: {}", plist_path.display());
    Ok(())
}

/// Uninstall systemd service (Linux)
fn uninstall_systemd_service() -> Result<()> {
    let unit_path = get_systemd_unit_path()?;

    if !unit_path.exists() {
        return Err(anyhow!("Service not installed at {}", unit_path.display()));
    }

    eprintln!("Stopping and disabling systemd service...");
    let _ = std::process::Command::new("systemctl")
        .args(["--user", "disable", "--now", "auths-agent"])
        .status();

    fs::remove_file(&unit_path)
        .with_context(|| format!("Failed to remove unit file: {:?}", unit_path))?;

    let _ = std::process::Command::new("systemctl")
        .args(["--user", "daemon-reload"])
        .status();

    eprintln!("Uninstalled systemd service: {}", unit_path.display());
    Ok(())
}

impl crate::commands::executable::ExecutableCommand for AgentCommand {
    fn execute(&self, _ctx: &crate::config::CliConfig) -> anyhow::Result<()> {
        handle_agent(self.clone())
    }
}

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

    #[test]
    fn test_get_auths_dir() {
        let dir = get_auths_dir().unwrap();
        assert!(dir.ends_with(".auths"));
    }

    #[test]
    fn test_get_default_socket_path() {
        let path = get_default_socket_path().unwrap();
        assert!(path.ends_with("agent.sock"));
    }

    #[test]
    fn test_shell_format_default() {
        // Default should be Bash
        let format: ShellFormat = Default::default();
        assert!(matches!(format, ShellFormat::Bash));
    }

    #[test]
    fn test_parse_timeout() {
        use std::time::Duration;

        // Zero timeout
        assert_eq!(parse_timeout("0").unwrap(), Duration::ZERO);

        // Seconds
        assert_eq!(parse_timeout("30s").unwrap(), Duration::from_secs(30));

        // Minutes
        assert_eq!(parse_timeout("5m").unwrap(), Duration::from_secs(300));
        assert_eq!(parse_timeout("30m").unwrap(), Duration::from_secs(1800));

        // Hours
        assert_eq!(parse_timeout("1h").unwrap(), Duration::from_secs(3600));
        assert_eq!(parse_timeout("2h").unwrap(), Duration::from_secs(7200));

        // No suffix defaults to minutes
        assert_eq!(parse_timeout("30").unwrap(), Duration::from_secs(1800));
    }
}