#![allow(clippy::arithmetic_side_effects)]
pub use solana_test_validator as test_validator;
use {
console::style,
fd_lock::{RwLock, RwLockWriteGuard},
indicatif::{ProgressDrawTarget, ProgressStyle},
std::{
borrow::Cow,
fmt::Display,
fs::{File, OpenOptions},
path::Path,
process::exit,
time::Duration,
},
};
pub mod admin_rpc_service;
pub mod bootstrap;
pub mod cli;
pub mod commands;
pub mod dashboard;
pub fn format_name_value(name: &str, value: &str) -> String {
format!("{} {}", style(name).bold(), value)
}
pub fn println_name_value(name: &str, value: &str) {
println!("{}", format_name_value(name, value));
}
pub fn new_spinner_progress_bar() -> ProgressBar {
let progress_bar = indicatif::ProgressBar::new(42);
progress_bar.set_draw_target(ProgressDrawTarget::stdout());
progress_bar.set_style(
ProgressStyle::default_spinner()
.template("{spinner:.green} {wide_msg}")
.expect("ProgresStyle::template direct input to be correct"),
);
progress_bar.enable_steady_tick(Duration::from_millis(100));
ProgressBar {
progress_bar,
is_term: console::Term::stdout().is_term(),
}
}
pub struct ProgressBar {
progress_bar: indicatif::ProgressBar,
is_term: bool,
}
impl ProgressBar {
pub fn set_message<T: Into<Cow<'static, str>> + Display>(&self, msg: T) {
if self.is_term {
self.progress_bar.set_message(msg);
} else {
println!("{msg}");
}
}
pub fn println<I: AsRef<str>>(&self, msg: I) {
self.progress_bar.println(msg);
}
pub fn abandon_with_message<T: Into<Cow<'static, str>> + Display>(&self, msg: T) {
if self.is_term {
self.progress_bar.abandon_with_message(msg);
} else {
println!("{msg}");
}
}
}
pub fn ledger_lockfile(ledger_path: &Path) -> RwLock<File> {
let lockfile = ledger_path.join("ledger.lock");
fd_lock::RwLock::new(
OpenOptions::new()
.write(true)
.create(true)
.truncate(false)
.open(lockfile)
.unwrap(),
)
}
pub fn lock_ledger<'lock>(
ledger_path: &Path,
ledger_lockfile: &'lock mut RwLock<File>,
) -> RwLockWriteGuard<'lock, File> {
ledger_lockfile.try_write().unwrap_or_else(|_| {
println!(
"Error: Unable to lock {} directory. Check if another validator is running",
ledger_path.display()
);
exit(1);
})
}