-
Notifications
You must be signed in to change notification settings - Fork 49
cuprated --dry-run option #461
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
Open
IznaDev
wants to merge
1
commit into
Cuprate:main
Choose a base branch
from
IznaDev:dry-run
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ monerod | |
| books/*/book | ||
| fast_sync_hashes.bin | ||
| /books/user/Cuprated.toml | ||
| cuprate.toml | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,11 +2,13 @@ | |
| use std::{ | ||
| fs::{read_to_string, File}, | ||
| io, | ||
| net::{IpAddr, TcpListener}, | ||
| path::Path, | ||
| str::FromStr, | ||
| time::Duration, | ||
| }; | ||
|
|
||
| use args::Args; | ||
| use clap::Parser; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
|
|
@@ -61,7 +63,7 @@ const HEADER: &str = r"## ____ _ | |
|
|
||
| /// Reads the args & config file, returning a [`Config`]. | ||
| pub fn read_config_and_args() -> Config { | ||
| let args = args::Args::parse(); | ||
| let args = Args::parse(); | ||
| args.do_quick_requests(); | ||
|
|
||
| let config: Config = if let Some(config_file) = &args.config_file { | ||
|
|
@@ -260,6 +262,62 @@ impl Config { | |
| pub fn block_downloader_config(&self) -> BlockDownloaderConfig { | ||
| self.p2p.block_downloader.clone().into() | ||
| } | ||
|
|
||
| fn check_port(ip: IpAddr, port: u16) -> Result<(), String> { | ||
| match TcpListener::bind((ip, port)) { | ||
| Ok(_) => Ok(()), | ||
| Err(e) => Err(format!("Fail to bind {ip}:{port} : {e}")), | ||
| } | ||
| } | ||
|
|
||
| pub fn dry_run_check(args: &Args) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think to reduce duplicated code it would be better to call this function in |
||
| let config: Self = if let Some(config_file) = &args.config_file { | ||
| // If a config file was set in the args try to read it and exit if we can't. | ||
| match Self::read_from_path(config_file) { | ||
| Ok(config) => config, | ||
| Err(e) => { | ||
| eprintln_red(&format!("Failed to read config from file: {e}")); | ||
| std::process::exit(1); | ||
| } | ||
| } | ||
| } else { | ||
| // First attempt to read the config file from the current directory. | ||
| std::env::current_dir() | ||
| .map(|path| path.join(DEFAULT_CONFIG_FILE_NAME)) | ||
| .map_err(Into::into) | ||
| .and_then(Self::read_from_path) | ||
| .inspect_err(|e| tracing::debug!("Failed to read config from current dir: {e}")) | ||
| // otherwise try the main config directory. | ||
| .or_else(|_| { | ||
| let file = CUPRATE_CONFIG_DIR.join(DEFAULT_CONFIG_FILE_NAME); | ||
| Self::read_from_path(file) | ||
| }) | ||
| .inspect_err(|e| { | ||
| tracing::debug!("Failed to read config from config dir: {e}"); | ||
| if !args.skip_config_warning { | ||
| eprintln_red(DEFAULT_CONFIG_WARNING); | ||
| std::thread::sleep(DEFAULT_CONFIG_STARTUP_DELAY); | ||
| } | ||
| }) | ||
| .unwrap_or_default() | ||
| }; | ||
| println!("Config file loaded."); | ||
|
|
||
| //If the ports are available | ||
| let ip = config.p2p.clear_net.listen_on; | ||
| let ports = vec![config.p2p.clear_net.general.p2p_port]; | ||
| for port in ports { | ||
| match Self::check_port(ip, port) { | ||
| Ok(()) => println!("Port {ip}:{port} available."), | ||
| Err(e) => { | ||
| eprintln_red(&format!("Config file error: {e}")); | ||
| std::process::exit(1); | ||
| } | ||
| } | ||
| } | ||
| println!("Dry run done."); | ||
| std::process::exit(0); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
@@ -275,4 +333,39 @@ mod test { | |
|
|
||
| assert_eq!(conf, Config::default()); | ||
| } | ||
|
|
||
| use std::io::Write; | ||
| use std::net::{IpAddr, TcpListener}; | ||
| use std::path::Path; | ||
| use tempfile::NamedTempFile; | ||
| use toml::to_string; | ||
|
|
||
| #[test] | ||
|
|
||
| fn test_check_port() { | ||
| let port = 51000; | ||
| let ip = IpAddr::from([127, 0, 0, 1]); | ||
| let available_port = Config::check_port(ip, port); | ||
| assert!(available_port.is_ok()); | ||
| let _listen = TcpListener::bind((ip, port)).expect("fail to bind to the port for test"); | ||
| let unavailable_port = Config::check_port(ip, port); | ||
| assert!(unavailable_port.is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_read_from_path() { | ||
| let config = Config::default(); | ||
| let config_str = to_string(&config).unwrap(); | ||
| let mut file = NamedTempFile::new().unwrap(); | ||
| writeln!(file, "{config_str}").unwrap(); | ||
| let path = file.path().to_path_buf(); | ||
|
|
||
| // Test reading from the file | ||
| let read_config = Config::read_from_path(path).unwrap(); | ||
| assert_eq!(read_config, config); | ||
|
|
||
| // Test reading from a non-existent file | ||
| let non_existent_path = Path::new("non_existent_file.toml"); | ||
| assert!(Config::read_from_path(non_existent_path).is_err()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: because the is a mod and not an external crate it should be grouped with the mod imports around line 40