Skip to main content

snip_sync/
cli.rs

1use clap::{Parser, Subcommand, ValueEnum};
2use std::path::PathBuf;
3
4#[derive(Parser)]
5#[command(
6    name = "snip-sync",
7    version,
8    about = "gRPC snippet sync server for snp"
9)]
10pub struct Cli {
11    #[command(subcommand)]
12    pub command: Option<Command>,
13}
14
15#[derive(Subcommand)]
16pub enum Command {
17    /// Start the sync server (default if no subcommand given)
18    Serve,
19    /// Initialize config, directories, and dev certificates
20    Init {
21        #[arg(long)]
22        force_cert: bool,
23        #[arg(long)]
24        skip_cert: bool,
25    },
26    /// Generate self-signed dev certificates
27    Cert {
28        #[arg(long)]
29        force: bool,
30        #[arg(long)]
31        out_dir: Option<PathBuf>,
32    },
33    /// Open config file in $EDITOR
34    Edit,
35    /// Stop a running server
36    Stop {
37        #[arg(long)]
38        force: bool,
39    },
40    /// Restart the server (stop + serve)
41    Restart {
42        #[arg(long)]
43        force: bool,
44    },
45    /// Install, inspect, or remove boot-time startup registration
46    Startup {
47        #[command(subcommand)]
48        command: StartupCommand,
49    },
50    /// Check for and install an update from crates.io when Cargo-managed
51    Update {
52        #[arg(long)]
53        dry_run: bool,
54        #[arg(long)]
55        locked: bool,
56    },
57    /// Internal Windows self-replacement helper.
58    #[command(name = "__self-replace", hide = true)]
59    SelfReplace {
60        #[arg(long)]
61        candidate: PathBuf,
62        #[arg(long)]
63        destination: PathBuf,
64        #[arg(long)]
65        restart: bool,
66        #[arg(long)]
67        managed_restart: bool,
68    },
69    /// Health check; start server if unhealthy/down
70    Croncheck {
71        #[arg(long)]
72        verbose: bool,
73    },
74    /// Print resolved paths
75    Paths {
76        #[arg(long)]
77        json: bool,
78    },
79    /// Generate shell completions
80    Completions {
81        #[arg(value_enum)]
82        shell: clap_complete::Shell,
83    },
84    /// Print version
85    Version,
86}
87
88#[derive(Subcommand)]
89pub enum StartupCommand {
90    /// Install startup registration using the selected or detected manager
91    Install {
92        #[arg(long, value_enum, default_value_t = StartupMethodArg::Auto)]
93        method: StartupMethodArg,
94    },
95    /// Print exact manual startup commands and rendered configuration
96    Instructions {
97        #[arg(long, value_enum, default_value_t = StartupMethodArg::Auto)]
98        method: StartupMethodArg,
99    },
100    /// Remove snip-sync-owned startup registration
101    Uninstall,
102}
103
104#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
105pub enum StartupMethodArg {
106    Auto,
107    Systemd,
108    Launchd,
109    Cron,
110    #[value(name = "task-scheduler")]
111    TaskScheduler,
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117    use clap::CommandFactory;
118
119    #[test]
120    fn test_cli_struct_valid() {
121        Cli::command().debug_assert();
122    }
123
124    #[test]
125    fn test_parse_serve() {
126        let cli = Cli::try_parse_from(["snip-sync", "serve"]).unwrap();
127        assert!(matches!(cli.command, Some(Command::Serve)));
128    }
129
130    #[test]
131    fn test_parse_no_subcommand() {
132        let cli = Cli::try_parse_from(["snip-sync"]).unwrap();
133        assert!(cli.command.is_none());
134    }
135
136    #[test]
137    fn test_parse_init() {
138        let cli = Cli::try_parse_from(["snip-sync", "init", "--force-cert"]).unwrap();
139        match cli.command {
140            Some(Command::Init {
141                force_cert,
142                skip_cert,
143            }) => {
144                assert!(force_cert);
145                assert!(!skip_cert);
146            }
147            _ => panic!("Expected Init command"),
148        }
149    }
150
151    #[test]
152    fn test_parse_cert() {
153        let cli = Cli::try_parse_from(["snip-sync", "cert", "--force"]).unwrap();
154        match cli.command {
155            Some(Command::Cert { force, out_dir }) => {
156                assert!(force);
157                assert!(out_dir.is_none());
158            }
159            _ => panic!("Expected Cert command"),
160        }
161    }
162
163    #[test]
164    fn test_parse_stop() {
165        let cli = Cli::try_parse_from(["snip-sync", "stop"]).unwrap();
166        assert!(matches!(cli.command, Some(Command::Stop { force: false })));
167    }
168
169    #[test]
170    fn test_parse_startup_commands() {
171        let cli = Cli::try_parse_from([
172            "snip-sync",
173            "startup",
174            "install",
175            "--method",
176            "task-scheduler",
177        ])
178        .unwrap();
179        assert!(matches!(
180            cli.command,
181            Some(Command::Startup {
182                command: StartupCommand::Install {
183                    method: StartupMethodArg::TaskScheduler
184                }
185            })
186        ));
187    }
188
189    #[test]
190    fn test_parse_version() {
191        let cli = Cli::try_parse_from(["snip-sync", "version"]).unwrap();
192        assert!(matches!(cli.command, Some(Command::Version)));
193    }
194
195    #[test]
196    fn test_parse_update() {
197        let cli = Cli::try_parse_from(["snip-sync", "update", "--dry-run", "--locked"]).unwrap();
198        match cli.command {
199            Some(Command::Update { dry_run, locked }) => {
200                assert!(dry_run);
201                assert!(locked);
202            }
203            _ => panic!("Expected Update command"),
204        }
205    }
206}