Skip to content

Commit

Permalink
optimize error prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
CNCSMonster committed Nov 5, 2024
1 parent 56d3eee commit 93f6aef
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xdotter"
version = "0.0.8"
version = "0.0.9"
edition = "2021"
authors = ["cncsmonster <[email protected]>"]
description = "A simple dotfile manager"
Expand All @@ -13,7 +13,7 @@ path = "src/main.rs"

[dependencies]
anyhow = "1.0.86"
clap = {version="4.5.2",features=["cargo","wrap_help"]}
clap = { version = "4.5.2", features = ["cargo", "wrap_help"] }
clap_complete = "4.5.5"
dirs = "5.0.1"
env_logger = "0.11.3"
Expand Down
5 changes: 3 additions & 2 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use std::io;
use std::{fs, os::unix::fs::symlink, path::Path};

// 创建路径为link的软链接到actual_path
pub fn create_symlink(actual_path: &str, link: &str) -> Result<(), Error> {
pub fn create_symlink(actual_path: &str, link: &str) -> anyhow::Result<()> {
// 获取actual_path的绝对路径
let actual_path = std::fs::canonicalize(actual_path)?;
let actual_path = std::fs::canonicalize(actual_path)
.map_err(|e| anyhow!("failed to get absolute path of actual path {actual_path}:{e}"))?;
// 获取link的绝对路径
let home_dir = if dirs::home_dir().is_none() {
return Err(anyhow::anyhow!("home dir not found"));
Expand Down
28 changes: 18 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ enum LinkAction {
Link(String),
}

fn new_cmd()->Result<()> {
fn new_cmd() -> Result<()> {
let config = Config {
dependencies: Some(hashmap! {
"go".to_string() => "testdata/go".to_string(),
Expand All @@ -43,7 +43,7 @@ fn new_cmd()->Result<()> {
Ok(())
}

fn deploy_cmd(am: &ArgMatches) ->Result<()>{
fn deploy_cmd(am: &ArgMatches) -> Result<()> {
info!("deploying...");
let dry_run = get_dry_run(am);
let interactive = get_interactive(am);
Expand Down Expand Up @@ -99,7 +99,7 @@ fn deploy_on(conf: &str) -> Result<()> {
}
Ok(())
}
fn undeploy_cmd(am: &ArgMatches) ->Result<()>{
fn undeploy_cmd(am: &ArgMatches) -> Result<()> {
info!("undeploying...");
let dry_run = get_dry_run(am);
let interactive = get_interactive(am);
Expand Down Expand Up @@ -153,10 +153,13 @@ fn undeploy_on(conf: &str) -> Result<(), Error> {
}
Ok(())
}
fn completions_cmd(am:&ArgMatches)->Result<()>{
let shell=am.get_one::<Shell>("shell").ok_or_else(||anyhow!("Shell name missing")).unwrap();
let mut cli=xdotter_cli();
let bin_name=cli.get_name().to_string();
fn completions_cmd(am: &ArgMatches) -> Result<()> {
let shell = am
.get_one::<Shell>("shell")
.ok_or_else(|| anyhow!("Shell name missing"))
.unwrap();
let mut cli = xdotter_cli();
let bin_name = cli.get_name().to_string();
clap_complete::generate(*shell, &mut cli, bin_name, &mut std::io::stdout().lock());
Ok(())
}
Expand All @@ -170,7 +173,11 @@ fn xdotter_cli() -> Command {
Delete all the symlinks created by the deploy command.
"});
let completions_cmd = clap::Command::new("completions")
.arg(arg!(-s --shell <shell> "Specify the shell to generate completions for").value_parser(clap::value_parser!(Shell)).required(true))
.arg(
arg!(-s --shell <shell> "Specify the shell to generate completions for")
.value_parser(clap::value_parser!(Shell))
.required(true),
)
.about("Generate shell completions");
clap::Command::new("xdotter")
.version(env!("CARGO_PKG_VERSION"))
Expand Down Expand Up @@ -230,10 +237,11 @@ fn main() {
Some(("new", _)) => new_cmd(),
Some(("deploy", sub_m)) => deploy_cmd(sub_m),
Some(("undeploy", sub_m)) => undeploy_cmd(sub_m),
Some(("completions",sub_m))=>completions_cmd(sub_m),
Some(("completions", sub_m)) => completions_cmd(sub_m),
Some((_, sub_m)) => deploy_cmd(sub_m),
None => deploy_cmd(&am),
}.unwrap_or_else(|e|{
}
.unwrap_or_else(|e| {
panic!("{e}");
});
}

0 comments on commit 93f6aef

Please sign in to comment.