Skip to content

Commit

Permalink
feat: 去掉 cli config
Browse files Browse the repository at this point in the history
  • Loading branch information
1739616529 committed Jul 4, 2024
1 parent 16e86a0 commit d55dea8
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 49 deletions.
4 changes: 0 additions & 4 deletions crates/nsv/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use clap::Parser;

use crate::command::Commands;
use crate::config::NsvConfig;



Expand All @@ -10,9 +9,6 @@ use crate::config::NsvConfig;
pub struct Cli {
// pub core: NsvCore,

#[clap(flatten)]
pub config: NsvConfig,

#[clap(subcommand)]
pub subcmd: Commands,
}
Expand Down
4 changes: 2 additions & 2 deletions crates/nsv/src/command/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use async_trait::async_trait;
use root::core::{node::NsvCoreError, NsvCore};

use super::Command;
use crate::{config::NsvConfig, print_log_1};
use crate::{ print_log_1};
use root::core::add::AddVersion;
use thiserror::Error;

Expand All @@ -13,7 +13,7 @@ pub struct Add {

#[async_trait]
impl Command for Add {
async fn apply(&self, _config: &NsvConfig, core: &mut NsvCore) -> Result<(), NsvCoreError> {
async fn apply(&self, core: &mut NsvCore) -> Result<(), NsvCoreError> {

core.add_version(self.version.clone()).await?;

Expand Down
20 changes: 10 additions & 10 deletions crates/nsv/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use r#use::Use;
use root::core::{NsvCore, node::NsvCoreError};
use add::Add;

use crate::{config::NsvConfig, print_log_err};
use crate::{print_log_err};

#[derive(clap::Parser, Debug)]
pub enum Commands {
Expand All @@ -18,28 +18,28 @@ pub enum Commands {
Add(Add),
}
impl Commands {
pub async fn call(&self, config: NsvConfig, core: &mut NsvCore) {
pub async fn call(&self, core: &mut NsvCore) {
match self {
Self::Use(cmd) => cmd.call(config, core).await,
Self::Add(cmd) => cmd.call(config, core).await,
Self::Use(cmd) => cmd.call( core).await,
Self::Add(cmd) => cmd.call( core).await,
}
}
}

#[async_trait]
pub trait Command {
async fn call(&self, config: NsvConfig, core: &mut NsvCore) {
match self.apply(&config, core).await {
async fn call(&self, core: &mut NsvCore) {
match self.apply(core).await {
Ok(()) => (),
Err(err) => self.handle_err(err, &config, core),
Err(err) => self.handle_err(err, core),
}
}

async fn apply(&self, config: &NsvConfig, core: &mut NsvCore) -> Result<(), NsvCoreError>;
async fn apply(&self, core: &mut NsvCore) -> Result<(), NsvCoreError>;

fn handle_err(&self, err: NsvCoreError, config: &NsvConfig, core: &mut NsvCore) {
fn handle_err(&self, err: NsvCoreError, core: &mut NsvCore) {
let err_s = format!("{:?}", err);
print_log_err!("{}.\nconfig: {:?} \ncontext: {:?}", err_s, config, core.context);
print_log_err!("{}.\ncontext: {:?}", err_s, core.context);
std::process::exit(1);
}
}
3 changes: 1 addition & 2 deletions crates/nsv/src/command/use.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use async_trait::async_trait;
use root::core::{NsvCore, r#use::UseVersion, node::NsvCoreError};

use crate::config::NsvConfig;
use thiserror::Error;

use super::Command;
Expand All @@ -14,7 +13,7 @@ pub struct Use {

#[async_trait]
impl Command for Use {
async fn apply(&self, _config: &NsvConfig, core: &mut NsvCore) -> Result<(), NsvCoreError> {
async fn apply(&self, core: &mut NsvCore) -> Result<(), NsvCoreError> {
if let Err(err) = core.use_version(self.version.clone()).await {
println!("err1: {:?}", err)
}
Expand Down
28 changes: 0 additions & 28 deletions crates/nsv/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,28 +0,0 @@
use url::Url;

#[derive(clap::Parser, Debug)]
pub struct NsvConfig {

// /// 标签
// #[clap(
// long,
// env = "TARGET",
// short = 't',
// hide_env_values = true,
// value_parser = ["lts", "current", "latest"],
// )]
// pub target: String,

/// 下载node源地址
#[clap(
long,
env = "ORIGIN",
default_value = "https://nodejs.org/dist",
global = true,
hide_env_values = true,
short = 'o',
)]
pub origin: Url,


}
2 changes: 1 addition & 1 deletion crates/nsv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ async fn main() {

let mut nsv_core = NsvCore::build(config);
nsv_core.init().await;
cli.subcmd.call(cli.config, &mut nsv_core).await
cli.subcmd.call(&mut nsv_core).await
}
2 changes: 1 addition & 1 deletion crates/root/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#[derive(Debug, Clone)]
pub struct Config {
pub origin: &'static str,
// pub version: String,

}

impl Config {
Expand Down
4 changes: 3 additions & 1 deletion crates/root/src/core/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ pub trait NodeVersion {

#[async_trait]
impl NodeVersion for NsvCore {
async fn unzip_node_item(&self, node_item: &NodeVersionItem) -> Result<(), NsvCoreError> {

async fn unzip_node_item(&self, node_item: &NodeVersionItem) -> Result<(), NsvCoreError> {
let node_file_dir = self.get_node_file_path(node_item);

if !node_file_dir.exists() {
Expand Down Expand Up @@ -170,6 +171,7 @@ impl NodeVersion for NsvCore {

Ok(())
}

async fn vail_and_download_file(&self, node_item: &NodeVersionItem) -> bool {
let local_node_item_dir = self.get_node_file_path(node_item);
if !local_node_item_dir.exists() {
Expand Down
1 change: 1 addition & 0 deletions crates/root/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod util;
pub mod core;
pub mod config;
pub mod context;
pub mod node;

0 comments on commit d55dea8

Please sign in to comment.