printnanny_dev/
octoprint.rs1use anyhow::Result;
2use clap::ArgEnum;
3use printnanny_services::config::PrintNannyConfig;
4use std::process::{Child, Command};
5
6#[derive(Copy, Eq, PartialEq, Debug, Clone, clap::ArgEnum)]
7pub enum OctoPrintAction {
8 PipInstall,
9 PipUninstall,
10}
11
12impl OctoPrintAction {
13 pub fn possible_values() -> impl Iterator<Item = clap::PossibleValue<'static>> {
14 OctoPrintAction::value_variants()
15 .iter()
16 .filter_map(clap::ArgEnum::to_possible_value)
17 }
18}
19
20impl std::str::FromStr for OctoPrintAction {
21 type Err = String;
22
23 fn from_str(s: &str) -> Result<Self, Self::Err> {
24 for variant in Self::value_variants() {
25 if variant.to_possible_value().unwrap().matches(s, false) {
26 return Ok(*variant);
27 }
28 }
29 Err(format!("Invalid variant: {}", s))
30 }
31}
32
33pub struct OctoPrintCmd {
34 pub action: OctoPrintAction,
35 pub config: PrintNannyConfig,
36 pub package: Option<String>,
37}
38
39impl OctoPrintCmd {
40 pub fn new(action: OctoPrintAction, config: PrintNannyConfig, package: Option<String>) -> Self {
41 Self {
42 action,
43 config,
44 package,
45 }
46 }
47
48 pub fn handle_pip_install(self) -> Result<Child> {
49 let package = &self.package.expect("package is required");
50 let args = &["install", "--upgrade", "--force-reinstall", package];
51 let cmd = self.config.paths.octoprint_pip();
52 let output = Command::new(cmd).args(args).spawn()?;
53 Ok(output)
54 }
55 pub fn handle_pip_uninstall(self) -> Result<Child> {
56 let package = &self.package.expect("package is required");
57 let args = &["uninstall", package];
58 let cmd = self.config.paths.octoprint_pip();
59 let output = Command::new(cmd).args(args).spawn()?;
60 Ok(output)
61 }
62 pub fn handle(self) -> Result<Child> {
63 match self.action {
64 OctoPrintAction::PipInstall => self.handle_pip_install(),
65 OctoPrintAction::PipUninstall => self.handle_pip_uninstall(),
66 }
67 }
68}