lib/lib.rs
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
mod cli;
mod config;
mod destinations;
mod errors;
mod shooters;
use clap::Parser;
use cli::{Cli, Command};
use destinations::Destinations;
use errors::MusketError;
use shooters::{bluesky_shooter, linkedin_shooter, mastodon_shooter, turso_shooter};
use tracing::{debug, level_filters::LevelFilter};
use tracing_subscriber::EnvFilter;
/// Runs the main logic of the application.
///
/// # Errors
///
/// This function will return an error if any of the commands fail.
#[allow(clippy::too_many_lines)]
pub async fn run() -> Result<Vec<String>, MusketError> {
let mut success_messages: Vec<String> = vec![];
let cli = Cli::parse();
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy(),
)
.without_time()
.init();
match cli.cmd {
Command::Init { force } => {
debug!("Inside run function. Command::Init");
let overwrite = force.unwrap_or(false);
if config::configuration_exists()? && !overwrite {
return Err(MusketError::Cli {
message: "The configuration file already exists. If you want to overwrite it, please run the musket init command with the -f, --force option.".to_string(),
});
}
match config::configure() {
Ok(_) => {
success_messages.push(format!("The configuration file has been created here: \"{}\". \nTo start using Musket, please complete the configuration file with your data.",
config::get_configuration_path()
.unwrap_or_default()
.to_string_lossy()));
}
Err(e) => return Err(e.into()),
}
}
Command::Fire {
url,
destination,
tags,
commentary,
language,
} => {
debug!("Inside run function. Command::Fire");
if !config::configuration_exists()? {
return Err(MusketError::Cli {
message: format!("The configuration file does not exist. If you want to send \"{url}\" to {} destination, please first run the musket init command and next fill the configuration file.", destination.unwrap_or_default().iter().map(std::string::ToString::to_string).collect::<Vec<String>>().join(", ")),
});
}
if destination.is_none() {
return Err(MusketError::Cli {
message: format!("The url \"{url}\" cannot be sent to a non-existing destination. Set, at least, one valid destination."),
});
}
let cfg = config::configure()?;
let tags = tags.unwrap_or_default();
let destinations = destination.unwrap_or_default();
for target in destinations {
match target {
Destinations::All => {
success_messages.push(
bluesky_shooter(
&cfg,
&url,
tags.clone(),
commentary.as_ref(),
language.as_ref(),
)
.await?,
);
success_messages.push(
linkedin_shooter(
&cfg,
&url,
tags.clone(),
commentary.as_ref(),
language.as_ref(),
)
.await?,
);
success_messages.push(
mastodon_shooter(
&cfg,
&url,
tags.clone(),
commentary.as_ref(),
language.as_ref(),
)
.await?,
);
success_messages.push(turso_shooter(&cfg, &url, tags.clone(), None).await?);
}
Destinations::Bluesky => {
success_messages.push(
bluesky_shooter(
&cfg,
&url,
tags.clone(),
commentary.as_ref(),
language.as_ref(),
)
.await?,
);
}
Destinations::LinkedIn => {
success_messages.push(
linkedin_shooter(
&cfg,
&url,
tags.clone(),
commentary.as_ref(),
language.as_ref(),
)
.await?,
);
}
Destinations::Mastodon => {
success_messages.push(
mastodon_shooter(
&cfg,
&url,
tags.clone(),
commentary.as_ref(),
language.as_ref(),
)
.await?,
);
}
Destinations::Turso => {
success_messages.push(turso_shooter(&cfg, &url, tags.clone(), None).await?);
}
}
}
}
}
Ok(success_messages)
}