feat: Support CLI
All checks were successful
Write changelog / write-changelog (push) Successful in 4s
Rust CI / Rust CI (push) Successful in 2m53s

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-05-29 22:46:50 +03:00
parent b7b1f7e415
commit 31b810f817
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
2 changed files with 115 additions and 0 deletions

108
src/cli_parser.rs Normal file
View file

@ -0,0 +1,108 @@
// A simple API to ping telegram bots and returns if it's online or not.
// Copyright (C) 2023-2024 Awiteb <a@4rs.nl>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/agpl-3.0>.
use std::{env, path::PathBuf};
use crate::ServerError;
pub(crate) const HELP_MESSAGE: &str = r#"Copyright (C) 2023-2024 Awiteb <a@4rs.nl>
License GNU AGPL-3.0-or-later <https://gnu.org/licenses/agpl-3.0>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Git repository: https://git.4rs.nl/awiteb/telepingbot
A simple API to ping telegram bots and returns if it's online or not
Usage: telepingbot [OPTIONS]
Options:
-c, --config <CONFIG_FILE> The config toml file
-V, --version Print version
--help Print this message
Please report bugs to <https://git.4rs.nl/awiteb/telepingbot/issues>."#;
/// The server accepted args
#[derive(Debug)]
pub(crate) struct Args {
/// The config file location, default to `config.toml`
pub config_file: PathBuf,
/// The version flag, the user want the server version
pub version: bool,
/// The help flag, the user want to show the help message
pub help: bool,
}
impl Default for Args {
fn default() -> Self {
Self {
config_file: "config.toml".into(),
version: false,
help: false,
}
}
}
impl Args {
/// Parse the cli input
pub(crate) fn parse() -> crate::ServerResult<Self> {
let mut user_args = env::args_os().skip(1);
let mut args = Self::default();
while let Some(arg) = user_args.next() {
if let Some(str_arg) = arg.to_str() {
match str_arg {
"-V" | "--version" => args.version = true,
"--help" => args.help = true,
"-c" | "--config" => {
args.config_file = user_args
.next()
.ok_or_else(|| {
ServerError::CliParse(
"The `-c`/`--config` does't have the config path".to_owned(),
)
})?
.into()
}
unknowun => {
return Err(ServerError::CliParse(format!("Unknowun arg `{unknowun}`")));
}
}
} else {
return Err(ServerError::CliParse(
"Invalid argument string provided".to_owned(),
));
}
}
Ok(args)
}
/// Returns true if the server should stop
pub(crate) fn stop(&self) -> bool {
const VERSION: &str = env!("CARGO_PKG_VERSION");
if self.help {
println!("{HELP_MESSAGE}");
} else if self.version {
println!("telepingbot v{VERSION}");
} else {
return false;
}
true
}
}

View file

@ -22,6 +22,7 @@ use lazy_static::lazy_static;
use salvo::{conn::TcpListener, Listener}; use salvo::{conn::TcpListener, Listener};
mod api; mod api;
mod cli_parser;
mod errors; mod errors;
mod superbot; mod superbot;
mod traits; mod traits;
@ -60,6 +61,12 @@ async fn try_main() -> ServerResult<()> {
pretty_env_logger::init(); pretty_env_logger::init();
dotenv::dotenv().ok(); dotenv::dotenv().ok();
log::info!("Starting the API"); log::info!("Starting the API");
let cli_args = cli_parser::Args::parse()?;
if cli_args.stop() {
return Ok(());
}
let bots: Vec<String> = fs::read_to_string("bots.txt")? let bots: Vec<String> = fs::read_to_string("bots.txt")?
.lines() .lines()