Skip to main content

e_app/input/
mod.rs

1/// API
2pub mod api;
3pub use api::*;
4use e_utils::{Error, Result};
5use serde::{Deserialize, Serialize};
6use std::ffi::OsString;
7use structopt::{clap::arg_enum, StructOpt};
8
9arg_enum! {
10    /// Api接口
11    #[derive(Deserialize, Serialize, Debug, StructOpt, Clone, PartialEq, Copy)]
12    pub enum OptsApi {
13        None,
14        Os,
15        Office,
16        Clean,
17        Drive,
18        Net
19    }
20}
21
22/// e-app
23///
24/// ------------------------------------------------------
25///
26///
27#[derive(StructOpt, Debug, Clone, Serialize, Deserialize)]
28#[structopt(name = "", setting = structopt::clap::AppSettings::TrailingVarArg,)]
29#[structopt(after_help = r#"
30# Example
31-----------------------------------------------------------
32# Cmd Example Active SYSTEM
33```sh
34e-app.exe --api os --task rkms -- kms.03k.org
35e-app.exe --api os --task active -- YVWGF-BXNMC-HTQYQ-CPQ99-66QFC
36e-app.exe --api os --task check
37```
38-----------------------------------------------------------
39
40# Cmd Example deActive SYSTEM
41```sh
42e-app.exe --api os --task ckms
43e-app.exe --api os --task deactive
44e-app.exe --api os --task check
45```
46-----------------------------------------------------------
47
48# Cmd Example empty task
49```sh
50e-app.exe --api clean --task empty_recycle_bin
51e-app.exe --api clean --task empty_access_log
52e-app.exe --api clean --task empty_netshare
53e-app.exe --api clean --task empty_activity_history
54e-app.exe --api clean --task empty_run_history
55```
56-----------------------------------------------------------
57
58# Cmd Example Drive commands
59```sh
60# Find drive from node
61e-app.exe --init --api Drive --full --task findnodes --filter "Intel(R) Ethernet Controller (3) I225-V #4" -- =net
62# Remove drive
63e-app.exe --init --api Drive --full --task remove --args /force --filter "Intel(R) Ethernet Controller (3) I225-V #4" -- =net
64# Disable drive
65e-app.exe --init --api Drive --full --task disable --args /force --filter "Intel(R) Ethernet Controller (3) I225-V #4" -- =net
66# Enable drive
67e-app.exe --init --api Drive --full --task enable --args /force --filter "Intel(R) Ethernet Controller (3) I225-V #4" -- =net
68# Restart drive
69e-app.exe --init --api Drive --full --task restart --args /force --filter "Intel(R) Ethernet Controller (3) I225-V #4" -- =net
70# Scan all new drive
71e-app.exe --api Drive --task scan
72# Delete drive throgh node info : /reboot
73e-app.exe --init --api Drive --full --task delete-find --args /force --filter "Intel(R) Ethernet Controller (3) I225-V #4" -- =net
74# Delete with uninstall drive throgh inf file : /reboot
75e-app.exe --api Drive --task delete --args F:\device.inf /uninstall /force
76# Add drive throgh inf file : /reboot
77e-app.exe --api Drive --task add --args F:\device.inf /install /force
78# Add more drive throgh inf file : /reboot
79e-app.exe --api Drive --task add-file --args F:\drives /install /force
80# Output more or one drive <oem#.inf | *> <target directory>
81e-app.exe --api Drive --task export --args * F:\drives
82```
83-----------------------------------------------------------
84"#)]
85#[allow(clippy::struct_excessive_bools)]
86pub struct Opts {
87  /// API接口
88  #[structopt(required = true, short, long, possible_values = &OptsApi::variants(), case_insensitive = true)]
89  pub api: OptsApi,
90  /// 任务
91  #[structopt(long, required = false, default_value = "")]
92  pub task: String,
93  /// 是否初始化
94  #[structopt(long)]
95  pub init: bool,
96  /// 是否完整信息
97  #[structopt(long)]
98  pub full: bool,
99  /// 筛选排除
100  #[structopt(long, required = false)]
101  pub filter: Vec<String>,
102  /// 扩展参数
103  #[structopt(long, required = false)]
104  pub args: Vec<String>,
105  /// 扩展指令
106  #[structopt(required = false, last = true)]
107  pub command: Vec<String>,
108}
109impl Default for Opts {
110  fn default() -> Self {
111    Self {
112      api: OptsApi::None,
113      task: String::new(),
114      init: false,
115      full: false,
116      // verbose: 0,
117      args: Vec::new(),
118      filter: Vec::new(),
119      command: Vec::new(),
120    }
121  }
122}
123
124#[cfg(not(tarpaulin_include))]
125impl Opts {
126  /// # Example
127  ///```rust
128  /// fn main() -> e_utils::Result<()> {
129  ///   if e_app::input::Opts::check_empty() {
130  ///     let _ = app::MyApp::launch()?;
131  ///   } else {
132  ///     let opts = e_app::input::Opts::new(None as Option<Vec<String>>)?;
133  ///     let res = e_app::input::api(opts)?;
134  ///     println!("{}", res.to_str()?);
135  ///   }
136  ///   Ok(())
137  /// }
138  /// ```
139  pub fn new<I>(args: Option<I>) -> Result<Self>
140  where
141    Self: Sized,
142    I: IntoIterator,
143    I::Item: Into<OsString> + Clone,
144  {
145    match args {
146      Some(arg) => match Opts::from_iter_safe(arg) {
147        Ok(opt) => Ok(opt),
148        Err(e) => Err(Error::String(e.to_string())),
149      },
150      None => Ok(Opts::from_args()),
151    }
152  }
153  /// # 检查空
154  pub fn check_empty() -> bool {
155    std::env::args().len() == 1
156  }
157}