aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: f6e4a37b8b66b358cd4751302783f0dd882943ba (plain) (blame)
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
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() {
    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);
        }
    }
}