Skip to content

Commit

Permalink
feat: cli实现
Browse files Browse the repository at this point in the history
  • Loading branch information
HKMV committed May 21, 2024
1 parent 355a234 commit 09c96df
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ fern = "0.6"
#log4rs = "1.3.0"
yaml-rust = "0.4.5"
service-manager = "0.6.1"
clap = { version = "4.5.4", features = ["derive"] }
clap_derive = "4.5.4"
30 changes: 27 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,47 @@ use crate::core::sangfor::Sangfor;
use std::io::{Error, Write};
use std::path::PathBuf;
use std::time::SystemTime;
use clap::{Command, Parser};
use serde::{Deserialize, Serialize};
use service_manager::{ServiceInstallCtx, ServiceLabel, ServiceManager, ServiceStartCtx, ServiceStopCtx, ServiceUninstallCtx};
use util::service::Service;
use crate::util::cmd::Cli;

mod core;
mod test;
mod util;

#[tokio::main]
async fn main() {
util::logs::init("nal.log", log::LevelFilter::Debug).expect("初始化日志出错");

let cli = util::cmd::Cli::parse();
let service = Service::new("net-auto-login");
if cli.install {
service.install();
return;
}
if cli.uninstall {
service.uninstall();
return;
}
if cli.start {
service.start();
return;
}
if cli.stop {
service.stop();
return;
}
if cli.run {
handler().await;
return;
}
}

async fn handler(){
let config = nal::init_config();
info!("config: {config:#?}");

let service = Service::new("net-auto-login");

/*let expression = "* 1 * * * * *";
let schedule = Schedule::from_str(expression).unwrap();
println!("All stars: Upcoming fire times for '{}':", expression);
Expand Down
20 changes: 20 additions & 0 deletions src/util/cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use clap_derive::Parser;

#[derive(Parser)]
#[command(version, about, long_about = None)]
pub struct Cli {
#[arg(short = 'i')]
pub install: bool,

#[arg(short,long)]
pub uninstall: bool,

#[arg(long)]
pub start: bool,

#[arg(long)]
pub stop: bool,

#[arg(short,long)]
pub run: bool
}
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod logs;
pub mod rc4;
pub mod service;
pub mod cmd;
16 changes: 8 additions & 8 deletions src/util/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Service {
}

/// 安装到系统服务
fn install(&self) {
pub fn install(&self) {
// 使用底层服务管理平台安装我们的服务
self.service_manage.install(ServiceInstallCtx {
label: self.name.clone(),
Expand All @@ -46,33 +46,33 @@ impl Service {
working_directory: None, // 服务进程的工作目录的可选字符串。
environment: None, // 用于提供服务进程的环境变量的可选列表。
}).expect("Failed to install");
info!("{:?}服务安装完成。",self.path.clone())
info!("{:}服务安装完成。",self.name.clone().to_string())
}

/// 从系统服务卸载
fn uninstall(&self) {
pub fn uninstall(&self) {
// 使用底层服务管理平台卸载我们的服务
self.service_manage.uninstall(ServiceUninstallCtx {
label: self.name.clone()
}).expect("Failed to uninstall");
info!("{:?}服务卸载完成。",self.path.clone())
info!("{:}服务卸载完成。",self.name.clone().to_string())
}

/// 启动这个服务
fn start(&self) {
pub fn start(&self) {
// 使用底层服务管理平台启动我们的服务
self.service_manage.start(ServiceStartCtx {
label: self.name.clone()
}).expect("Failed to start");
info!("{:?}服务启动完成。",self.path.clone())
info!("{:}服务启动完成。",self.name.clone().to_string())
}

/// 停止这个服务
fn stop(&self) {
pub fn stop(&self) {
// 使用底层服务管理平台停止我们的服务
self.service_manage.stop(ServiceStopCtx {
label: self.name.clone()
}).expect("Failed to stop");
info!("{:?}服务停止完成。",self.path.clone())
info!("{:}服务停止完成。",self.name.clone().to_string())
}
}

0 comments on commit 09c96df

Please sign in to comment.