cuprated: add startup delay if config is missing ()

* add startup delay if config is missing

* clean up

* fix typos

* fix imports + add small comment

* fmt
This commit is contained in:
Boog900 2025-03-06 03:52:03 +00:00 committed by GitHub
parent f554be73b9
commit ba708a78d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 38 additions and 6 deletions

1
Cargo.lock generated
View file

@ -1094,6 +1094,7 @@ dependencies = [
"hex-literal",
"indexmap",
"monero-serai",
"nu-ansi-term",
"paste",
"pin-project",
"rand",

View file

@ -122,6 +122,7 @@ hex = { version = "0.4", default-features = false }
hex-literal = { version = "0.4", default-features = false }
indexmap = { version = "2", default-features = false }
monero-serai = { git = "https://github.com/Cuprate/serai.git", rev = "e6fdef6", default-features = false }
nu-ansi-term = { version = "0.46", default-features = false }
paste = { version = "1", default-features = false }
pin-project = { version = "1", default-features = false }
randomx-rs = { git = "https://github.com/Cuprate/randomx-rs.git", rev = "e09955c", default-features = false }

View file

@ -57,6 +57,7 @@ hex = { workspace = true }
hex-literal = { workspace = true }
indexmap = { workspace = true }
monero-serai = { workspace = true }
nu-ansi-term = { workspace = true }
paste = { workspace = true }
pin-project = { workspace = true }
randomx-rs = { workspace = true }

View file

@ -17,6 +17,11 @@ use cuprate_helper::{
use cuprate_p2p::block_downloader::BlockDownloaderConfig;
use cuprate_p2p_core::{ClearNet, ClearNetServerCfg};
use crate::{
constants::{DEFAULT_CONFIG_STARTUP_DELAY, DEFAULT_CONFIG_WARNING},
logging::eprintln_red,
};
mod args;
mod fs;
mod p2p;
@ -42,7 +47,7 @@ pub fn read_config_and_args() -> Config {
match Config::read_from_path(config_file) {
Ok(config) => config,
Err(e) => {
eprintln!("Failed to read config from file: {e}");
eprintln_red(&format!("Failed to read config from file: {e}"));
std::process::exit(1);
}
}
@ -60,7 +65,10 @@ pub fn read_config_and_args() -> Config {
})
.inspect_err(|e| {
tracing::debug!("Failed to read config from config dir: {e}");
eprintln!("Failed to find/read config file, using default config.");
if !args.skip_config_warning {
eprintln_red(DEFAULT_CONFIG_WARNING);
std::thread::sleep(DEFAULT_CONFIG_STARTUP_DELAY);
}
})
.unwrap_or_default()
};
@ -101,13 +109,14 @@ impl Config {
let file_text = read_to_string(file.as_ref())?;
Ok(toml::from_str(&file_text)
.inspect(|_| eprintln!("Using config at: {}", file.as_ref().to_string_lossy()))
.inspect(|_| println!("Using config at: {}", file.as_ref().to_string_lossy()))
.inspect_err(|e| {
eprintln!("{e}");
eprintln!(
eprintln_red(&format!(
"Failed to parse config file at: {}",
file.as_ref().to_string_lossy()
);
));
eprintln_red(&format!("{e}"));
std::process::exit(1);
})?)
}

View file

@ -32,6 +32,10 @@ pub struct Args {
#[arg(long)]
pub generate_config: bool,
/// Stops the missing config warning and startup delay if a config file is missing.
#[arg(long)]
pub skip_config_warning: bool,
/// Print misc version information in JSON.
#[arg(short, long)]
pub version: bool,

View file

@ -1,4 +1,5 @@
//! General constants used throughout `cuprated`.
use std::time::Duration;
use const_format::formatcp;
@ -23,6 +24,16 @@ pub const VERSION_BUILD: &str = formatcp!("{VERSION}-{}", cuprate_constants::bui
pub const PANIC_CRITICAL_SERVICE_ERROR: &str =
"A service critical to Cuprate's function returned an unexpected error.";
pub const DEFAULT_CONFIG_WARNING: &str = formatcp!(
"WARNING: no config file found, using default config.\
\nThe default config may not be optimal for your setup, see the user book here: https://user.cuprate.org/.\
\nPausing startup for {} seconds. \
\nUse the `--skip-config-warning` arg to skip this delay if you really want to use the default.",
DEFAULT_CONFIG_STARTUP_DELAY.as_secs()
);
pub const DEFAULT_CONFIG_STARTUP_DELAY: Duration = Duration::from_secs(15);
pub const EXAMPLE_CONFIG: &str = include_str!("../config/Cuprated.toml");
#[cfg(test)]

View file

@ -140,3 +140,8 @@ pub fn modify_stdout_output(f: impl FnOnce(&mut CupratedTracingFilter)) {
pub fn modify_file_output(f: impl FnOnce(&mut CupratedTracingFilter)) {
FILE_WRITER_FILTER_HANDLE.get().unwrap().modify(f).unwrap();
}
/// Prints some text using [`eprintln`], with [`nu_ansi_term::Color::Red`] applied.
pub fn eprintln_red(s: &str) {
eprintln!("{}", nu_ansi_term::Color::Red.bold().paint(s));
}