-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathmain.rs
111 lines (100 loc) · 2.76 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#[cfg(feature = "launch")]
pub mod launch;
pub mod standard;
#[cfg(feature = "systemd")]
pub mod systemd;
pub mod xunlei_asset;
use std::io::Write;
use std::path::PathBuf;
use clap::{Args, Parser, Subcommand};
#[derive(Parser)]
#[clap(author, version, about, arg_required_else_help = true)]
struct Opt {
/// Enable debug mode
#[clap(short, long, global = true)]
debug: bool,
#[clap(subcommand)]
commands: Commands,
}
#[derive(Subcommand)]
pub enum Commands {
#[cfg(feature = "systemd")]
/// Install xunlei
Install(Config),
#[cfg(feature = "systemd")]
/// Uninstall xunlei
Uninstall,
#[cfg(feature = "launch")]
/// Launch xunlei
Launch(Config),
}
#[derive(Args)]
pub struct Config {
/// Xunlei internal mode
#[clap(short, long)]
internal: bool,
/// Xunlei web-ui port
#[clap(short, long, default_value = "5055", value_parser = parser_port_in_range)]
port: u16,
/// Xunlei config directory
#[clap(short, long, default_value = standard::SYNOPKG_PKGBASE)]
config_path: PathBuf,
/// Xunlei download directory
#[clap(short, long, default_value = standard::TMP_DOWNLOAD_PATH)]
download_path: PathBuf,
}
fn main() -> anyhow::Result<()> {
let opt = Opt::parse();
init_log(opt.debug);
match opt.commands {
#[cfg(feature = "systemd")]
Commands::Install(config) => {
systemd::XunleiInstall::from(config).launch()?;
}
#[cfg(feature = "systemd")]
Commands::Uninstall => {
systemd::XunleiUninstall {}.launch()?;
}
#[cfg(feature = "launch")]
Commands::Launch(config) => {
launch::XunleiLauncher::from(config).launch()?;
}
}
Ok(())
}
fn init_log(debug: bool) {
match debug {
true => std::env::set_var("RUST_LOG", "DEBUG"),
false => std::env::set_var("RUST_LOG", "INFO"),
};
env_logger::builder()
.format(|buf, record| {
writeln!(
buf,
"{} {}: {}",
record.level(),
//Format like you want to: <-----------------
chrono::Local::now().format("%Y-%m-%d %H:%M:%S"),
record.args()
)
})
.init();
}
const PORT_RANGE: std::ops::RangeInclusive<usize> = 1024..=65535;
// port range parser
pub(crate) fn parser_port_in_range(s: &str) -> anyhow::Result<u16> {
let port: usize = s
.parse()
.map_err(|_| anyhow::anyhow!(format!("`{}` isn't a port number", s)))?;
if PORT_RANGE.contains(&port) {
return Ok(port as u16);
}
anyhow::bail!(format!(
"Port not in range {}-{}",
PORT_RANGE.start(),
PORT_RANGE.end()
))
}
pub trait Running {
fn launch(&self) -> anyhow::Result<()>;
}