Skip to main content

chc_service/
cli.rs

1use std::{net::IpAddr, str::FromStr};
2
3use crate::ChcService;
4
5#[derive(clap::Parser, Debug)]
6#[command(name = "hc-chc-service")]
7#[command(version, about = "Run a local chc server")]
8pub struct LocalChcServerCli {
9    /// The network interface to use (e.g., 127.0.0.1).
10    #[arg(short, long, default_value = "127.0.0.1")]
11    pub interface: Option<String>,
12
13    /// The port to bind to. Will default to an available port if not passed.
14    #[arg(short, long)]
15    pub port: Option<u16>,
16}
17
18impl TryInto<ChcService> for LocalChcServerCli {
19    type Error = anyhow::Error;
20
21    fn try_into(self) -> Result<ChcService, Self::Error> {
22        let address = IpAddr::from_str(&self.interface.unwrap_or_default())?;
23        let port = self
24            .port
25            .unwrap_or_else(|| portpicker::pick_unused_port().expect("No available port found"));
26        Ok(ChcService::new(address, port))
27    }
28}