#![forbid(unsafe_code)]
#![deny(missing_docs)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::redundant_field_names)]
use tracing::debug;
use users::UsersCache;
#[cfg(feature = "auth")]
use std::path::PathBuf;
mod cli;
mod errors;
mod exporter;
mod file;
mod httpd;
mod racctrctl;
mod rctlstate;
mod user;
#[macro_use]
mod macros;
#[cfg(feature = "bcrypt_cmd")]
mod bcrypt;
#[cfg(feature = "rc_script")]
mod rcscript;
use errors::ExporterError;
use exporter::Exporter;
use file::{
FileExporter,
FileExporterOutput,
};
#[cfg(feature = "auth")]
use httpd::auth::BasicAuthConfig;
#[tokio::main]
async fn main() -> Result<(), ExporterError> {
tracing_subscriber::fmt::init();
let matches = cli::parse_args();
#[cfg(feature = "rc_script")]
if matches.get_flag("RC_SCRIPT") {
rcscript::output();
::std::process::exit(0);
}
#[cfg(feature = "bcrypt_cmd")]
if let Some(subcmd) = matches.subcommand_matches("bcrypt") {
bcrypt::generate_from(subcmd)?;
::std::process::exit(0);
}
user::is_running_as_root(&mut UsersCache::new())?;
racctrctl::is_available()?;
if let Some(output_path) = matches.get_one::<FileExporterOutput>("OUTPUT_FILE_PATH") {
debug!("output.file-path: {}", output_path);
let exporter = FileExporter::new(output_path.clone());
return exporter.export();
}
let bind_address = matches.get_one::<String>("WEB_LISTEN_ADDRESS")
.ok_or_else(|| {
ExporterError::ArgNotSet("web.listen-address".to_owned())
})?.clone();
debug!("web.listen-address: {}", bind_address);
let telemetry_path = matches.get_one::<String>("WEB_TELEMETRY_PATH")
.ok_or_else(|| {
ExporterError::ArgNotSet("web.telemetry-path".to_owned())
})?.clone();
debug!("web.telemetry-path: {}", telemetry_path);
#[allow(unused_mut)]
let mut server = httpd::Server::new()
.bind_address(bind_address)
.telemetry_path(telemetry_path);
#[cfg(feature = "auth")]
if let Some(path) = matches.get_one::<PathBuf>("WEB_AUTH_CONFIG") {
let config = BasicAuthConfig::from_yaml(path)?;
server = server.auth_config(config);
}
let exporter = Exporter::new();
server.run(exporter).await?;
Ok(())
}