From 31b810f8176e3e16556ff97f95a2611bdacf3f1d Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Wed, 29 May 2024 22:46:50 +0300
Subject: [PATCH] feat: Support CLI
Signed-off-by: Awiteb
---
src/cli_parser.rs | 108 ++++++++++++++++++++++++++++++++++++++++++++++
src/main.rs | 7 +++
2 files changed, 115 insertions(+)
create mode 100644 src/cli_parser.rs
diff --git a/src/cli_parser.rs b/src/cli_parser.rs
new file mode 100644
index 0000000..3ddb9c5
--- /dev/null
+++ b/src/cli_parser.rs
@@ -0,0 +1,108 @@
+// A simple API to ping telegram bots and returns if it's online or not.
+// Copyright (C) 2023-2024 Awiteb
+//
+// 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 .
+
+use std::{env, path::PathBuf};
+
+use crate::ServerError;
+
+pub(crate) const HELP_MESSAGE: &str = r#"Copyright (C) 2023-2024 Awiteb
+License GNU AGPL-3.0-or-later
+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 The config toml file
+ -V, --version Print version
+ --help Print this message
+
+Please report bugs to ."#;
+
+/// 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 {
+ 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
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 40cfa7f..e31f34d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -22,6 +22,7 @@ use lazy_static::lazy_static;
use salvo::{conn::TcpListener, Listener};
mod api;
+mod cli_parser;
mod errors;
mod superbot;
mod traits;
@@ -60,6 +61,12 @@ async fn try_main() -> ServerResult<()> {
pretty_env_logger::init();
dotenv::dotenv().ok();
log::info!("Starting the API");
+ let cli_args = cli_parser::Args::parse()?;
+
+ if cli_args.stop() {
+ return Ok(());
+ }
+
let bots: Vec = fs::read_to_string("bots.txt")?
.lines()