aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2022-11-19 18:48:12 -0800
committerFranck Cuny <franck@fcuny.net>2022-11-19 19:01:01 -0800
commitd78070d130c32227a2788591d3efd321da702658 (patch)
tree55e34a15a8ff3b53714c55974bd738e48a5bbcb9 /src/main.rs
parentmeta: ignore artifact created by `nix build` (diff)
downloadsendsms-d78070d130c32227a2788591d3efd321da702658.tar.gz
feat: a CLI to send sms via twilio's API
I want to get an SMS when some of my machines are rebooted. This tool let me do just that. The CLI takes as argument the path to a configuration file that contains credentials to the API. It also has a number of sub commands, and each sub command is associated with a specific action. For each action, a section in the configuration file is expected. The two actions currently supported are: - `reboot` - `hello` `hello` is a simple one used for testing, it sends "hello world". `reboot` is used to create a message that contains the host's name and the IP address of the network interface specified in the configuration file.
Diffstat (limited to '')
-rwxr-xr-x[-rw-r--r--]src/main.rs51
1 files changed, 50 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs
index e7a11a9..f6e4a37 100644..100755
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,52 @@
+mod client;
+mod config;
+mod message;
+
+use clap::{crate_version, Parser};
+use std::path::PathBuf;
+use std::process::exit;
+
+#[derive(Parser, Debug)]
+#[clap(author, version, about, long_about = None)]
+#[clap(version = crate_version!())]
+struct Args {
+ #[clap(short, long, value_parser)]
+ config: PathBuf,
+
+ #[clap(subcommand)]
+ subcmd: message::MessageKind,
+}
+
+const TWILIO_BASE_URL: &str = "https://api.twilio.com";
+
fn main() {
- println!("Hello, world!");
+ let args = Args::parse();
+
+ let config: config::Config = match config::Config::load_from_file(&args.config) {
+ Ok(config) => config,
+ Err(e) => {
+ eprintln!("unable to load data from {}: {}", args.config.display(), e);
+ exit(1);
+ }
+ };
+
+ let msg = message::Message::builder()
+ .from(config.from.to_owned())
+ .to(config.to.to_owned())
+ .with_action(args.subcmd, &config)
+ .build();
+
+ let client = client::TwilioClient::new(
+ &config.account_sid,
+ &config.auth_token,
+ reqwest::Url::parse(TWILIO_BASE_URL).unwrap(),
+ );
+
+ match client.send(msg) {
+ Ok(m) => println!("message sent successfully: status is {:?}", m.status),
+ Err(error) => {
+ eprintln!("failed to send the message: {}", error);
+ exit(1);
+ }
+ }
}