Skip to main content

claude_wrapper/command/
install.rs

1//! `claude install` -- install a Claude Code native build.
2
3#[cfg(feature = "async")]
4use crate::Claude;
5use crate::command::ClaudeCommand;
6#[cfg(feature = "async")]
7use crate::error::Result;
8#[cfg(feature = "async")]
9use crate::exec;
10use crate::exec::CommandOutput;
11
12/// Run `claude install [target] [--force]`.
13///
14/// Installs a Claude Code native build. `target` accepts `"stable"`,
15/// `"latest"`, or a specific version; omit it to use the CLI's default.
16///
17/// # Example
18///
19/// ```no_run
20/// # #[cfg(feature = "async")] {
21/// use claude_wrapper::{Claude, ClaudeCommand, InstallCommand};
22///
23/// # async fn example() -> claude_wrapper::Result<()> {
24/// let claude = Claude::builder().build()?;
25/// let out = InstallCommand::new()
26///     .target("stable")
27///     .force()
28///     .execute(&claude)
29///     .await?;
30/// println!("{}", out.stdout);
31/// # Ok(()) }
32/// # }
33/// ```
34#[derive(Debug, Clone, Default)]
35pub struct InstallCommand {
36    target: Option<String>,
37    force: bool,
38}
39
40impl InstallCommand {
41    #[must_use]
42    pub fn new() -> Self {
43        Self::default()
44    }
45
46    /// Target to install: `"stable"`, `"latest"`, or a specific version.
47    #[must_use]
48    pub fn target(mut self, target: impl Into<String>) -> Self {
49        self.target = Some(target.into());
50        self
51    }
52
53    /// Force installation even if already installed.
54    #[must_use]
55    pub fn force(mut self) -> Self {
56        self.force = true;
57        self
58    }
59}
60
61impl ClaudeCommand for InstallCommand {
62    type Output = CommandOutput;
63
64    fn args(&self) -> Vec<String> {
65        let mut args = vec!["install".to_string()];
66        if self.force {
67            args.push("--force".to_string());
68        }
69        if let Some(ref target) = self.target {
70            args.push(target.clone());
71        }
72        args
73    }
74
75    #[cfg(feature = "async")]
76    async fn execute(&self, claude: &Claude) -> Result<CommandOutput> {
77        exec::run_claude(claude, self.args()).await
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn install_command_defaults() {
87        assert_eq!(ClaudeCommand::args(&InstallCommand::new()), vec!["install"]);
88    }
89
90    #[test]
91    fn install_command_with_target_and_force() {
92        let cmd = InstallCommand::new().target("latest").force();
93        assert_eq!(
94            ClaudeCommand::args(&cmd),
95            vec!["install", "--force", "latest"]
96        );
97    }
98}