Skip to content

Commit d584540

Browse files
authored
feat: show upt version/tools in help txt (#41)
1 parent b1046e2 commit d584540

5 files changed

Lines changed: 35 additions & 29 deletions

File tree

src/error.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fmt;
55
pub enum UptError {
66
NoVendor(String),
77
NoTask,
8-
NotFoundTool,
8+
NoDetectVendor,
99
InvalidAction(String),
1010
InvalidArgs(String),
1111
DisplyHelp(String),
@@ -17,11 +17,11 @@ impl fmt::Display for UptError {
1717
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1818
use UptError::*;
1919
match self {
20-
NoVendor(v) => write!(f, "The vendor {} is not supported.", v),
20+
NoVendor(v) => write!(f, "The package management tool '{}' is not supported.", v),
2121
NoTask => write!(f, "The package management tool cannot perform the task."),
22-
NotFoundTool => write!(
22+
NoDetectVendor => write!(
2323
f,
24-
"No found package management tool, use `$UPT_TOOL` to specify one."
24+
"No package management tool avaiable, use `$UPT_TOOL` to specify one."
2525
),
2626
InvalidAction(v) => write!(f, "Invalid action '{}'.", v),
2727
InvalidArgs(v) => write!(f, "Invalid arguments.\n\n{}", v),

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ mod utils;
88
mod vendor;
99

1010
pub use error::UptError;
11-
pub use vendor::{detect_tool, init as init_vendor, Vendor};
11+
pub use vendor::{detect_vendor, init_vendor, Vendor};

src/macros.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ macro_rules! vendors {
1515
},
1616
)+
1717
) => {
18-
pub fn init(name: &str) -> Result<$crate::Vendor, $crate::UptError> {
18+
pub fn init_vendor(name: &str) -> Result<$crate::Vendor, $crate::UptError> {
1919
use $crate::action::must_from_str;
2020
match name {
2121
$(
@@ -58,9 +58,9 @@ macro_rules! vendors {
5858
}
5959
}
6060

61-
macro_rules! os_tools {
61+
macro_rules! os_vendors {
6262
($($os:literal => $($tool:literal),+);+$(;)?) => {
63-
pub fn detect_tool() -> std::result::Result<$crate::Vendor, $crate::UptError> {
63+
pub fn detect_vendor() -> std::result::Result<$crate::Vendor, $crate::UptError> {
6464
let os = crate::utils::detect_os().unwrap_or_default();
6565
let tools: Vec<&str> = match os.as_str() {
6666
$(
@@ -69,8 +69,8 @@ macro_rules! os_tools {
6969
_ => vec!["apt", "dnf", "pacman"],
7070
};
7171
match $crate::utils::find_tool(&tools) {
72-
Some(tool) => $crate::vendor::init(&tool),
73-
None => Err(UptError::NotFoundTool),
72+
Some(tool) => $crate::vendor::init_vendor(&tool),
73+
None => Err(UptError::NoDetectVendor),
7474
}
7575
}
7676
};

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::env;
22
use std::path::Path;
33
use std::process::{self, Command};
4-
use upt::{detect_tool, init_vendor, UptError, Vendor};
4+
use upt::{detect_vendor, init_vendor, UptError, Vendor};
55

66
fn main() {
77
match run() {
@@ -34,11 +34,11 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
3434
}
3535

3636
fn create_cmd(vendor: &Vendor, args: &[String]) -> Result<String, UptError> {
37-
let task = vendor.parse(args)?;
3837
let tool = match std::env::var("UPT_TOOL") {
3938
Ok(v) => init_vendor(&v)?,
40-
Err(_) => detect_tool()?,
39+
Err(_) => detect_vendor()?,
4140
};
41+
let task = vendor.parse(args, tool.name())?;
4242
let cmd = tool.eval(&task)?;
4343
Ok(cmd)
4444
}

src/vendor.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::action::Action;
22
use crate::error::UptError;
33
use crate::task::Task;
44

5-
os_tools!(
5+
os_vendors!(
66
"windows" => "scoop", "choco", "winget";
77
"macos" => "brew", "port";
88
// apt
@@ -407,10 +407,14 @@ pub struct Vendor {
407407
}
408408

409409
impl Vendor {
410+
pub fn name(&self) -> &str {
411+
&self.name
412+
}
413+
410414
/// Parse command line, figure out the task to perform
411-
pub fn parse(&self, args: &[String]) -> Result<Task, UptError> {
415+
pub fn parse(&self, args: &[String], upt_tool: &str) -> Result<Task, UptError> {
412416
if self.is_help(args) {
413-
return Err(UptError::DisplyHelp(self.help()));
417+
return Err(UptError::DisplyHelp(self.help(upt_tool)));
414418
}
415419
if let Some((Some(pkg), yes)) = self.install.parse(args, &self.confirm) {
416420
return Ok(Task::Install { pkg, confirm: yes });
@@ -436,7 +440,7 @@ impl Vendor {
436440
if self.list_installed.parse(args, "").is_some() {
437441
return Ok(Task::ListInstalled);
438442
}
439-
Err(UptError::InvalidArgs(self.help()))
443+
Err(UptError::InvalidArgs(self.help(upt_tool)))
440444
}
441445

442446
/// Convert the task to command line, which invokes the os's package management tool.
@@ -473,7 +477,7 @@ impl Vendor {
473477
}
474478

475479
/// Dump help message
476-
fn help(&self) -> String {
480+
fn help(&self, upt_tool: &str) -> String {
477481
let mut lines: Vec<String> = Vec::new();
478482
lines.push(String::from("Usage: "));
479483
let helps = vec![
@@ -495,9 +499,11 @@ impl Vendor {
495499
for (cmd, description) in &helps {
496500
lines.push(format!(" {:<width$} {}", cmd, description, width = width));
497501
}
502+
lines.push(String::new());
503+
lines.push(format!("Upt version: {}", env!("CARGO_PKG_VERSION")));
504+
lines.push(format!("Upt tool: {}", upt_tool));
498505
if !self.confirm.is_empty() {
499-
lines.push(String::new());
500-
lines.push(format!("Automatic answer yes to prompts: {}", self.confirm));
506+
lines.push(format!("Confirm options: {}", self.confirm));
501507
}
502508
lines.join("\n")
503509
}
@@ -509,25 +515,25 @@ mod tests {
509515

510516
macro_rules! check_parse {
511517
($vendor:expr, [$($arg:expr),*], ($task:tt, $pkg:expr, $confirm:expr)) => {
512-
assert_eq!($vendor.parse(&vec![ $($arg.to_string()),* ]).unwrap(), Task::$task { pkg: $pkg.to_string(), confirm: $confirm })
518+
assert_eq!($vendor.parse(&vec![ $($arg.to_string()),* ], "-").unwrap(), Task::$task { pkg: $pkg.to_string(), confirm: $confirm })
513519
};
514520
($vendor:expr, [$($arg:expr),*], ($task:tt, pkg=$pkg:expr)) => {
515-
assert_eq!($vendor.parse(&vec![ $($arg.to_string()),* ]).unwrap(), Task::$task { pkg: $pkg.to_string() })
521+
assert_eq!($vendor.parse(&vec![ $($arg.to_string()),* ], "-").unwrap(), Task::$task { pkg: $pkg.to_string() })
516522
};
517523
($vendor:expr, [$($arg:expr),*], ($task:tt, confirm=$confirm:expr)) => {
518-
assert_eq!($vendor.parse(&vec![ $($arg.to_string()),* ]).unwrap(), Task::$task { confirm: $confirm })
524+
assert_eq!($vendor.parse(&vec![ $($arg.to_string()),* ], "-").unwrap(), Task::$task { confirm: $confirm })
519525
};
520526
($vendor:expr, [$($arg:expr),*], $task:tt) => {
521-
assert_eq!($vendor.parse(&vec![ $($arg.to_string()),* ]).unwrap(), Task::$task)
527+
assert_eq!($vendor.parse(&vec![ $($arg.to_string()),* ], "-").unwrap(), Task::$task)
522528
};
523529
($vendor:expr, [$($arg:expr),*]) => {
524-
assert!($vendor.parse(&vec![ $($arg.to_string()),* ]).is_err())
530+
assert!($vendor.parse(&vec![ $($arg.to_string()),* ], "-").is_err())
525531
}
526532
}
527533

528534
#[test]
529535
fn test_parse() {
530-
let upt = init("upt").unwrap();
536+
let upt = init_vendor("upt").unwrap();
531537
check_parse!(upt, ["upt", "install", "vim"], (Install, "vim", false));
532538
check_parse!(upt, ["upt", "install", "-y", "vim"], (Install, "vim", true));
533539
check_parse!(
@@ -600,7 +606,7 @@ mod tests {
600606

601607
#[test]
602608
fn test_eval() {
603-
let upt = init("upt").unwrap();
609+
let upt = init_vendor("upt").unwrap();
604610
check_eval!(upt, (Install, "vim", false), "upt install vim");
605611
check_eval!(upt, (Install, "vim jq", true), "upt install -y vim jq");
606612
check_eval!(upt, (Remove, "vim jq", false), "upt remove vim jq");
@@ -612,7 +618,7 @@ mod tests {
612618
check_eval!(upt, (UpgradeAll, confirm = true), "upt upgrade -y");
613619
check_eval!(upt, ListInstalled, "upt list");
614620

615-
let pacman = init("pacman").unwrap();
621+
let pacman = init_vendor("pacman").unwrap();
616622
check_eval!(pacman, (Install, "vim", false), "pacman -S vim");
617623
check_eval!(
618624
pacman,
@@ -636,7 +642,7 @@ mod tests {
636642
#[test]
637643
fn test_vendors() {
638644
for tool in support_tools() {
639-
init(tool).unwrap();
645+
init_vendor(tool).unwrap();
640646
}
641647
}
642648
}

0 commit comments

Comments
 (0)