Skip to main content

snip_sync/
cli.rs

1use clap::{Parser, Subcommand};
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    /// Check for and install an update from crates.io when Cargo-managed
46    Update {
47        #[arg(long)]
48        dry_run: bool,
49        #[arg(long)]
50        locked: bool,
51    },
52    /// Health check; start server if unhealthy/down
53    Croncheck {
54        #[arg(long)]
55        verbose: bool,
56    },
57    /// Print resolved paths
58    Paths {
59        #[arg(long)]
60        json: bool,
61    },
62    /// Generate shell completions
63    Completions {
64        #[arg(value_enum)]
65        shell: clap_complete::Shell,
66    },
67    /// Print version
68    Version,
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74    use clap::CommandFactory;
75
76    #[test]
77    fn test_cli_struct_valid() {
78        Cli::command().debug_assert();
79    }
80
81    #[test]
82    fn test_parse_serve() {
83        let cli = Cli::try_parse_from(["snip-sync", "serve"]).unwrap();
84        assert!(matches!(cli.command, Some(Command::Serve)));
85    }
86
87    #[test]
88    fn test_parse_no_subcommand() {
89        let cli = Cli::try_parse_from(["snip-sync"]).unwrap();
90        assert!(cli.command.is_none());
91    }
92
93    #[test]
94    fn test_parse_init() {
95        let cli = Cli::try_parse_from(["snip-sync", "init", "--force-cert"]).unwrap();
96        match cli.command {
97            Some(Command::Init {
98                force_cert,
99                skip_cert,
100            }) => {
101                assert!(force_cert);
102                assert!(!skip_cert);
103            }
104            _ => panic!("Expected Init command"),
105        }
106    }
107
108    #[test]
109    fn test_parse_cert() {
110        let cli = Cli::try_parse_from(["snip-sync", "cert", "--force"]).unwrap();
111        match cli.command {
112            Some(Command::Cert { force, out_dir }) => {
113                assert!(force);
114                assert!(out_dir.is_none());
115            }
116            _ => panic!("Expected Cert command"),
117        }
118    }
119
120    #[test]
121    fn test_parse_stop() {
122        let cli = Cli::try_parse_from(["snip-sync", "stop"]).unwrap();
123        assert!(matches!(cli.command, Some(Command::Stop { force: false })));
124    }
125
126    #[test]
127    fn test_parse_version() {
128        let cli = Cli::try_parse_from(["snip-sync", "version"]).unwrap();
129        assert!(matches!(cli.command, Some(Command::Version)));
130    }
131
132    #[test]
133    fn test_parse_update() {
134        let cli = Cli::try_parse_from(["snip-sync", "update", "--dry-run", "--locked"]).unwrap();
135        match cli.command {
136            Some(Command::Update { dry_run, locked }) => {
137                assert!(dry_run);
138                assert!(locked);
139            }
140            _ => panic!("Expected Update command"),
141        }
142    }
143}