Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cli on tests #102

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ shellexpand = "3.1"
tokio = { version = "1", features = ["full"] }
tokio-serial = "5.4.4"
tokio-util = { version = "0.7", features = [ "codec", "net" ] }
once_cell = "1.20"
url = { version = "2.5.2", features = ["serde"] }
uuid = { version = "1", features = ["v5", "v4", "serde"] }
mime_guess = "2.0.5"
Expand Down
81 changes: 43 additions & 38 deletions src/lib/cli.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
use std::sync::Arc;

use clap::Parser;
use lazy_static::lazy_static;
use once_cell::sync::OnceCell;
use tracing::*;

use crate::drivers;

static MANAGER: OnceCell<Manager> = OnceCell::new();

struct Manager {
clap_matches: Args,
}

#[derive(Debug, Parser)]
#[command(
version = env!("CARGO_PKG_VERSION"),
author = env!("CARGO_PKG_AUTHORS"),
about = env!("CARGO_PKG_DESCRIPTION")
)]
struct Args {
pub struct Args {
/// Space-separated list of endpoints.
/// At least one endpoint is required.
/// Possible endpoints types are:
Expand Down Expand Up @@ -131,49 +137,44 @@ fn endpoints_parser(entry: &str) -> Result<Arc<dyn drivers::Driver>, String> {
drivers::create_driver_from_entry(entry)
}

struct Manager {
clap_matches: Args,
}

lazy_static! {
static ref MANAGER: Arc<Manager> = Arc::new(Manager::new());
}

impl Manager {
fn new() -> Self {
Self {
clap_matches: Args::parse(),
}
}
/// Constructs our manager, Should be done inside main
#[instrument(level = "debug")]
pub fn init() {
init_with(Args::parse());
}

/// Constructs our manager, Should be done inside main

#[instrument(level = "debug")]
pub fn init() {
MANAGER.as_ref();
pub fn init_with(args: Args) {
MANAGER.get_or_init(|| Manager { clap_matches: args });
}

/// Checks if the verbosity parameter was used
/// Local acessor to the parsed Args
fn args() -> &'static Args {
&MANAGER.get().unwrap().clap_matches
}

/// Checks if the verbosity parameter was used
#[instrument(level = "debug")]
pub fn is_verbose() -> bool {
MANAGER.clap_matches.verbose
args().verbose
}

#[instrument(level = "debug")]
pub fn is_tracing() -> bool {
MANAGER.clap_matches.enable_tracing_level_log_file
MANAGER
.get()
.unwrap()
.clap_matches
.enable_tracing_level_log_file
}

/// Our log path

#[instrument(level = "debug")]
pub fn log_path() -> String {
let log_path =
MANAGER.clap_matches.log_path.clone().expect(
"Clap arg \"log-path\" should always be \"Some(_)\" because of the default value.",
);
let log_path = args()
.log_path
.clone()
.expect("Clap arg \"log-path\" should always be \"Some(_)\" because of the default value.");

shellexpand::full(&log_path)
.expect("Failed to expand path")
Expand All @@ -182,7 +183,7 @@ pub fn log_path() -> String {

pub fn endpoints() -> Vec<Arc<dyn drivers::Driver>> {
let default_endpoints = Arc::new(crate::drivers::rest::Rest::builder("Default").build());
let mut endpoints = MANAGER.clap_matches.endpoints.clone();
let mut endpoints = args().endpoints.clone();
endpoints.push(default_endpoints);
endpoints
}
Expand All @@ -192,15 +193,15 @@ pub fn command_line_string() -> String {
std::env::args().collect::<Vec<String>>().join(" ")
}

// Return a clone of current Args struct
/// Returns a pretty string of the current Args struct
#[instrument(level = "debug")]
pub fn command_line() -> String {
format!("{:#?}", MANAGER.clap_matches)
format!("{:#?}", args())
}

#[instrument(level = "debug")]
pub fn udp_server_timeout() -> Option<tokio::time::Duration> {
let seconds = MANAGER.clap_matches.udp_server_timeout;
let seconds = args().udp_server_timeout;

if seconds < 0 {
return None;
Expand All @@ -211,32 +212,36 @@ pub fn udp_server_timeout() -> Option<tokio::time::Duration> {

#[instrument(level = "debug")]
pub fn web_server() -> std::net::SocketAddrV4 {
MANAGER.clap_matches.web_server
args().web_server
}

#[instrument(level = "debug")]
pub fn mavlink_system_id() -> u8 {
MANAGER.clap_matches.mavlink_system_id
args().mavlink_system_id
}

#[instrument(level = "debug")]
pub fn mavlink_component_id() -> u8 {
MANAGER.clap_matches.mavlink_component_id
args().mavlink_component_id
}

#[instrument(level = "debug")]
pub fn mavlink_heartbeat_frequency() -> f32 {
MANAGER.clap_matches.mavlink_heartbeat_frequency
MANAGER
.get()
.unwrap()
.clap_matches
.mavlink_heartbeat_frequency
}

#[instrument(level = "debug")]
pub fn send_initial_heartbeats() -> bool {
MANAGER.clap_matches.send_initial_heartbeats
args().send_initial_heartbeats
}

#[instrument(level = "debug")]
pub fn mavlink_version() -> u8 {
MANAGER.clap_matches.mavlink_version
args().mavlink_version
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/lib/drivers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ mod tests {
vec![]
}

fn create_endpoint_from_url(&self, url: &url::Url) -> Option<Arc<dyn Driver>> {
fn create_endpoint_from_url(&self, _url: &url::Url) -> Option<Arc<dyn Driver>> {
None
}
}
Expand Down