🏡 index : ~doyle/titanirc.git

author Jordan Doyle <jordan@doyle.la> 2023-01-07 22:53:09.0 +00:00:00
committer Jordan Doyle <jordan@doyle.la> 2023-01-07 22:53:09.0 +00:00:00
commit
f43a9c0034fe8d9b8dda490eae6b8139251f73ba [patch]
tree
df97b791e9065905d37e1c357c8a45765921e0dc
parent
8dd9439b9430053b6666949dc9dba9383b405957
download
f43a9c0034fe8d9b8dda490eae6b8139251f73ba.tar.gz

Add CLI parsing using clap



Diff

 Cargo.lock    | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 Cargo.toml    |  2 +-
 src/client.rs |  2 +-
 src/config.rs |  9 +++++++++
 src/main.rs   | 18 ++++++++++++++++++
 5 files changed, 78 insertions(+), 3 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index bf50ed2..b2389c9 100644
--- a/Cargo.lock
+++ a/Cargo.lock
@@ -131,6 +131,7 @@
checksum = "a7db700bc935f9e43e88d00b0850dae18a63773cfbec6d8e070fccf7fef89a39"
dependencies = [
 "bitflags",
 "clap_derive",
 "clap_lex",
 "is-terminal",
 "once_cell",
@@ -139,6 +140,19 @@
]

[[package]]
name = "clap_derive"
version = "4.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0177313f9f02afc995627906bbd8967e2be069f5261954222dac78290c2b9014"
dependencies = [
 "heck",
 "proc-macro-error",
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "clap_lex"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -405,6 +419,12 @@
 "pin-utils",
 "slab",
]

[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"

[[package]]
name = "hermit-abi"
@@ -661,6 +681,30 @@
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"

[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
 "proc-macro-error-attr",
 "proc-macro2",
 "quote",
 "syn",
 "version_check",
]

[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
 "proc-macro2",
 "quote",
 "version_check",
]

[[package]]
name = "proc-macro2"
@@ -1038,6 +1082,12 @@
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"

[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"

[[package]]
name = "wasi"
diff --git a/Cargo.toml b/Cargo.toml
index 749a597..625ecc4 100644
--- a/Cargo.toml
+++ a/Cargo.toml
@@ -10,7 +10,7 @@
actix-rt = "2.7"
anyhow = "1.0"
chrono = "0.4"
clap = { version = "4.0", features = ["cargo"] }
clap = { version = "4.0", features = ["cargo", "derive", "std", "suggestions", "color"] }
futures = "0.3"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
diff --git a/src/client.rs b/src/client.rs
index 07e17b7..20451da 100644
--- a/src/client.rs
+++ a/src/client.rs
@@ -6,7 +6,7 @@
};
use clap::{crate_name, crate_version};
use futures::FutureExt;
use irc_proto::{error::ProtocolError, ChannelExt, Command, Message, Response, Prefix};
use irc_proto::{error::ProtocolError, ChannelExt, Command, Message, Prefix, Response};
use tokio::time::Instant;
use tracing::{debug, error, info_span, instrument, warn, Instrument, Span};

diff --git a/src/config.rs b/src/config.rs
new file mode 100644
index 0000000..042fdef 100644
--- /dev/null
+++ a/src/config.rs
@@ -1,0 +1,9 @@
use clap::Parser;

#[derive(Parser)]
#[clap(version = clap::crate_version!(), author = clap::crate_authors!())]
pub struct Args {
    /// Turn debugging information on

    #[clap(short, long, action = clap::ArgAction::Count)]
    pub verbose: u8,
}
diff --git a/src/main.rs b/src/main.rs
index 269868c..cc9072c 100644
--- a/src/main.rs
+++ a/src/main.rs
@@ -5,16 +5,18 @@

use actix::{io::FramedWrite, Actor, Addr, AsyncContext};
use actix_rt::System;
use clap::Parser;
use irc_proto::IrcCodec;
use tokio::{net::TcpListener, time::Instant};
use tokio_util::codec::FramedRead;
use tracing::{error, info, info_span, Instrument};
use tracing_subscriber::EnvFilter;

use crate::{client::Client, messages::UserConnected, server::Server};
use crate::{client::Client, config::Args, messages::UserConnected, server::Server};

pub mod channel;
pub mod client;
pub mod config;
pub mod connection;
pub mod messages;
pub mod server;
@@ -23,6 +25,20 @@

#[actix_rt::main]
async fn main() -> anyhow::Result<()> {
    // parse CLI arguments
    let opts: Args = Args::parse();

    // overrides the RUST_LOG variable to our own value based on the
    // amount of `-v`s that were passed when calling the service
    std::env::set_var(
        "RUST_LOG",
        match opts.verbose {
            1 => "debug",
            2 => "trace",
            _ => "info",
        },
    );

    let subscriber = tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::from_default_env())
        .pretty();