1use std::fmt::Display;
2use std::path::PathBuf;
3use std::thread::sleep;
4use clap::{Parser};
5use lazy_static::lazy_static;
6use easy_trace::instruments::tracing;
7
8#[derive(Parser, Debug)]
9#[command(author, version, about)]
10pub struct PkgCli {
11 #[clap(long, short = 'i')]
13 pub install_service: bool,
14 #[clap(long, short = 'r')]
16 pub uninstall_service: bool,
17 #[arg(long, short = 'l')]
19 pub link_files: bool,
20 #[arg(long, short = 'd')]
22 pub remove_files: bool,
23 #[arg(long, short = 's')]
25 pub stop_services: bool,
26 #[arg(long, short = 't')]
28 pub launch_tray: bool,
29 #[arg(long, short = 'p')]
31 pub update_files: bool,
32 #[arg(long, short = 'u')]
35 pub check_and_update: bool,
36
37}
38
39
40lazy_static! {
41 pub static ref PKG_CLI: PkgCli = {
42 let cli = PkgCli::parse();
43 cli
44 };
45}
46
47pub fn parse_args() -> &'static PKG_CLI {
48 &PKG_CLI
49}
50
51impl PkgCli {
52 pub fn is_uninstall(&self) -> bool {
53 self.stop_services && self.remove_files
54 }
55
56 pub fn uninstall_args() -> &'static str {
57 "-sdr"
58 }
59
60 pub fn install_args() -> &'static str {
61 "-ltpi"
62 }
63
64 pub fn update_args() -> &'static str {
65 "-sultpi"
66 }
67}
68
69#[derive(PartialEq)]
70pub enum ElevatorMode {
71 Install,
72 Uninstall,
73 Update,
74}
75
76impl Display for ElevatorMode {
77 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78 match self {
79 ElevatorMode::Install => write!(f, "{}", PkgCli::install_args()),
80 ElevatorMode::Uninstall => write!(f, "{}", PkgCli::uninstall_args()),
81 ElevatorMode::Update => write!(f, "{}", PkgCli::update_args()),
82 }
83 }
84}
85
86
87
88pub fn launch_elevator(mode: ElevatorMode, working_dir: &PathBuf) -> sharing::PisPasResult<()> {
89 tracing::info!("Elevator in {} with args => {}", working_dir.display(), mode);
90 let child = std::process::Command::new(sharing::paths::get_elevator_path(working_dir))
91 .arg(mode.to_string())
92 .spawn()?;
93 tracing::info!("Elevated process launched detached with pid => {:?}", child.id());
94 if mode != ElevatorMode::Update {
96 sleep(std::time::Duration::from_secs(3));
97 }
98 Ok(())
99}
100