Page MenuHomeDevCentral

D2903.id7398.diff
No OneTemporary

D2903.id7398.diff

diff --git a/Cargo.toml b/Cargo.toml
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,9 +8,10 @@
[dependencies]
clap = { version = "4.1.1", features = ["derive"] }
+reqwest = { version = "0.11.14", features = ["json"] }
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.91"
-reqwest = { version = "0.11.14", features = ["json"] }
-openssl-sys = "0.9.80"
+serde_yaml = "0.9.19"
+tokio = { version = "1.12.0", features = ["full"] }
openssl = "0.10.45"
-tokio = { version = "1.12.0", features = ["full"] }
\ No newline at end of file
+openssl-sys = "0.9.80"
\ No newline at end of file
diff --git a/settings/notification-push.yml b/settings/notification-push.yml
new file mode 100644
--- /dev/null
+++ b/settings/notification-push.yml
@@ -0,0 +1,10 @@
+# api_endpoint: https://eo4a3aeazbbb14z.m.pipedream.net # Test Endpoint
+api_endpoint: https://notifications.notifications-test.nasqueron.org
+
+default_payload:
+ service: ""
+ project: ""
+ group: ""
+ type: ""
+ text: ""
+ link: ""
\ No newline at end of file
diff --git a/src/clap_args.rs b/src/clap_args.rs
--- a/src/clap_args.rs
+++ b/src/clap_args.rs
@@ -4,21 +4,27 @@
#[clap(author, version, about)]
pub struct ClapArgs {
/// The notification's source service (e.g. GitHub, Phabricator, Jenkins)
- pub clap_service: String,
+ #[arg(long)]
+ pub service: String,
/// The notification's target project (e.g. Wikimedia, Nasqueron, Wolfplex)
- pub clap_project: String,
+ #[arg(long)]
+ pub project: String,
/// The notification's target group (e.g. Tasacora, Operations)
- pub clap_group: String,
+ #[arg(long)]
+ pub group: String,
/// The notification's type (e.g. "commits", "task")
- pub clap_type: String,
+ #[arg(long, name = "type")]
+ pub _type: String,
/// The notification's text
- pub clap_text: Option<String>,
+ #[arg(long)]
+ pub text: Option<String>,
/// The notification's URL, to be used as the main link for widgets
- pub clap_link: Option<String>,
+ #[arg(long)]
+ pub link: Option<String>,
}
\ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,12 +1,16 @@
mod clap_args;
use clap_args::ClapArgs;
+
use clap::Parser;
-use serde::{Serialize};
use reqwest;
+use serde::{Deserialize, Serialize};
+use serde_yaml::{self};
+
use std::process::exit;
+use std::path::Path;
-#[derive(Serialize)]
+#[derive(Debug, Serialize, Deserialize)]
struct Notification {
service: String,
project: String,
@@ -17,29 +21,54 @@
link: String,
}
+#[derive(Debug, Serialize, Deserialize)]
+struct Config {
+ default_payload: Notification,
+ api_endpoint: String,
+}
+
#[tokio::main]
async fn main() {
- // Environment variables
- let api_endpoint = "https://eo4a3aeazbbb14z.m.pipedream.net";
+ // Checks and set yaml file location
+ let locations = vec![
+ "settings/notification-push.yml",
+ "$HOME/.config/notification-push.yml",
+ "/usr/local/etc/notification-push.yml",
+ "/etc/notification-push.yml",
+ ];
+ let mut yaml_file = "";
+
+ for location in locations {
+ if Path::new(location).exists() {
+ yaml_file = location;
+ break;
+ }
+ }
+
+ // Open/read yaml file
+ let f = std::fs::File::open(yaml_file).expect("Could not open file.");
+ let yaml_config: Config = serde_yaml::from_reader(f).expect("Could not read values.");
// ========================================================
// 1. Validating user's arguments while parsing (clap)
let _args:ClapArgs = ClapArgs::parse();
// 2. Converting user's arguments to ready-to-post JSON (serde)
+ let (args_text, args_link) = (_args.text.unwrap_or("".to_string()), _args.link.unwrap_or("".to_string()));
+
let populated_notification = Notification {
- service: _args.clap_service,
- project: _args.clap_project,
- group: _args.clap_group,
- _type: _args.clap_type,
- text: _args.clap_text.unwrap_or("".to_string()),
- link: _args.clap_link.unwrap_or("".to_string()),
+ service: if _args.service != "" { _args.service } else { yaml_config.default_payload.service },
+ project: if _args.project != "" { _args.project } else { yaml_config.default_payload.project },
+ group: if _args.group != "" { _args.group } else { yaml_config.default_payload.group },
+ _type: if _args._type != "" { _args._type } else { yaml_config.default_payload._type },
+ text: if args_text != "" { args_text } else { yaml_config.default_payload.text },
+ link: if args_link != "" { args_link } else { yaml_config.default_payload.link },
};
// 3. Sending JSON as HTTP request (reqwest)
let client = reqwest::Client::new();
- let res = client.post(api_endpoint)
+ let res = client.post(&yaml_config.api_endpoint)
.json(&populated_notification)
.send()
.await;

File Metadata

Mime Type
text/plain
Expires
Tue, Oct 1, 04:37 (20 h, 21 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2167727
Default Alt Text
D2903.id7398.diff (4 KB)

Event Timeline