Skip to content

Commit 7468529

Browse files
authored
feat: add version command (#1460)
1 parent 81eaccc commit 7468529

File tree

6 files changed

+44
-50
lines changed

6 files changed

+44
-50
lines changed

crates/core/src/bin/freenet.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ fn main() -> anyhow::Result<()> {
5858
.build()
5959
.unwrap();
6060
rt.block_on(async move {
61-
let config = ConfigArgs::parse().build().await?;
61+
let config = ConfigArgs::parse();
62+
if config.version {
63+
println!("Freenet version: {}", config.current_version());
64+
return Ok(());
65+
}
66+
let config = config.build().await?;
6267
run(config).await
6368
})?;
6469
Ok(())

crates/core/src/client_events/websocket.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ async fn websocket_interface(
298298
Ok(None) => continue,
299299
Err(None) => {
300300
tracing::debug!("client channel closed on request");
301+
let _ = server_sink.send(Message::Close(None)).await;
301302
return Ok(())
302303
},
303304
Err(Some(err)) => {

crates/core/src/config.rs

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ pub const DEFAULT_RANDOM_PEER_CONN_THRESHOLD: usize = 7;
3737
/// Default maximum number of hops to live for any operation
3838
/// (if it applies, e.g. connect requests).
3939
pub const DEFAULT_MAX_HOPS_TO_LIVE: usize = 10;
40+
4041
pub(crate) const OPERATION_TTL: Duration = Duration::from_secs(60);
4142

43+
/// Current version of the crate.
44+
pub(crate) const PCK_VERSION: &str = env!("CARGO_PKG_VERSION");
45+
4246
// Initialize the executor once.
4347
static ASYNC_RT: Lazy<Option<Runtime>> = Lazy::new(GlobalExecutor::initialize_async_rt);
4448

@@ -51,27 +55,31 @@ const FREENET_GATEWAYS_INDEX: &str = "https://freenet.org/keys/gateways.toml";
5155
#[derive(clap::Parser, Debug)]
5256
pub struct ConfigArgs {
5357
/// Node operation mode. Default is network mode.
54-
#[clap(value_enum, env = "MODE")]
58+
#[arg(value_enum, env = "MODE")]
5559
pub mode: Option<OperationMode>,
5660

57-
#[clap(flatten)]
61+
#[command(flatten)]
5862
pub ws_api: WebsocketApiArgs,
5963

60-
#[clap(flatten)]
64+
#[command(flatten)]
6165
pub network_api: NetworkArgs,
6266

63-
#[clap(flatten)]
67+
#[command(flatten)]
6468
pub secrets: SecretArgs,
6569

66-
#[clap(long, env = "LOG_LEVEL")]
70+
#[arg(long, env = "LOG_LEVEL")]
6771
pub log_level: Option<tracing::log::LevelFilter>,
6872

69-
#[clap(flatten)]
73+
#[command(flatten)]
7074
pub config_paths: ConfigPathsArgs,
7175

7276
/// An arbitrary identifier for the node, mostly for debugging or testing purposes.
73-
#[clap(long)]
77+
#[arg(long, hide = true)]
7478
pub id: Option<String>,
79+
80+
/// Show the version of the application.
81+
#[arg(long, short)]
82+
pub version: bool,
7583
}
7684

7785
impl Default for ConfigArgs {
@@ -98,11 +106,16 @@ impl Default for ConfigArgs {
98106
log_level: Some(tracing::log::LevelFilter::Info),
99107
config_paths: Default::default(),
100108
id: None,
109+
version: false,
101110
}
102111
}
103112
}
104113

105114
impl ConfigArgs {
115+
pub fn current_version(&self) -> &str {
116+
PCK_VERSION
117+
}
118+
106119
fn read_config(dir: &PathBuf) -> std::io::Result<Option<Config>> {
107120
if !dir.exists() {
108121
return Ok(None);
@@ -610,21 +623,6 @@ pub struct ConfigPathsArgs {
610623
/// The configuration directory.
611624
#[arg(long, default_value = None, env = "CONFIG_DIR")]
612625
pub config_dir: Option<PathBuf>,
613-
/// The contracts directory.
614-
#[arg(long, default_value = None, env = "CONTRACTS_DIR")]
615-
contracts_dir: Option<PathBuf>,
616-
/// The delegates directory.
617-
#[arg(long, default_value = None, env = "DELEGATES_DIR")]
618-
delegates_dir: Option<PathBuf>,
619-
/// The secrets directory.
620-
#[arg(long, default_value = None, env = "SECRECTS_DIR")]
621-
secrets_dir: Option<PathBuf>,
622-
/// The database directory.
623-
#[arg(long, default_value = None, env = "DB_DIR")]
624-
db_dir: Option<PathBuf>,
625-
/// The event log file.
626-
#[arg(long, default_value = None, env = "EVENT_LOG")]
627-
event_log: Option<PathBuf>,
628626
/// The data directory.
629627
#[arg(long, default_value = None, env = "DATA_DIR")]
630628
pub data_dir: Option<PathBuf>,
@@ -633,11 +631,6 @@ pub struct ConfigPathsArgs {
633631
impl ConfigPathsArgs {
634632
fn merge(&mut self, other: ConfigPaths) {
635633
self.config_dir.get_or_insert(other.config_dir);
636-
self.contracts_dir.get_or_insert(other.contracts_dir);
637-
self.delegates_dir.get_or_insert(other.delegates_dir);
638-
self.secrets_dir.get_or_insert(other.secrets_dir);
639-
self.db_dir.get_or_insert(other.db_dir);
640-
self.event_log.get_or_insert(other.event_log);
641634
self.data_dir.get_or_insert(other.data_dir);
642635
}
643636

@@ -669,16 +662,10 @@ impl ConfigPathsArgs {
669662
};
670663
Ok(defaults.data_dir().to_path_buf())
671664
})?;
672-
let contracts_dir = self
673-
.contracts_dir
674-
.unwrap_or_else(|| app_data_dir.join("contracts"));
675-
let delegates_dir = self
676-
.delegates_dir
677-
.unwrap_or_else(|| app_data_dir.join("delegates"));
678-
let secrets_dir = self
679-
.secrets_dir
680-
.unwrap_or_else(|| app_data_dir.join("secrets"));
681-
let db_dir = self.db_dir.unwrap_or_else(|| app_data_dir.join("db"));
665+
let contracts_dir = app_data_dir.join("contracts");
666+
let delegates_dir = app_data_dir.join("delegates");
667+
let secrets_dir = app_data_dir.join("secrets");
668+
let db_dir = app_data_dir.join("db");
682669

683670
if !contracts_dir.exists() {
684671
fs::create_dir_all(&contracts_dir)?;
@@ -720,13 +707,13 @@ impl ConfigPathsArgs {
720707
})?;
721708

722709
Ok(ConfigPaths {
710+
config_dir,
711+
data_dir: app_data_dir,
723712
contracts_dir,
724713
delegates_dir,
725714
secrets_dir,
726715
db_dir,
727-
data_dir: app_data_dir,
728716
event_log,
729-
config_dir,
730717
})
731718
}
732719
}

crates/core/src/config/secret.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ pub struct SecretArgs {
5050
#[clap(long, value_parser, default_value=None, env = "TRANSPORT_KEYPAIR")]
5151
pub transport_keypair: Option<PathBuf>,
5252

53-
/// Path to the nonce file.
53+
/// Path to the nonce file for encrypting data.
5454
#[clap(long, value_parser, default_value=None, env = "NONCE")]
5555
pub nonce: Option<PathBuf>,
5656

57-
/// Path to the cipher file.
57+
/// Path to the cipher file for encrypting data.
5858
#[clap(long, value_parser, default_value=None, env = "CIPHER")]
5959
pub cipher: Option<PathBuf>,
6060
}

crates/core/src/transport/connection_handler.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::sync::atomic::AtomicU32;
66
use std::sync::Arc;
77
use std::time::{Duration, Instant};
88

9+
use crate::config::PCK_VERSION;
910
use crate::transport::crypto::TransportSecretKey;
1011
use crate::transport::packet_data::{AssymetricRSA, UnknownEncryption};
1112
use crate::transport::symmetric_message::OutboundConnection;
@@ -890,7 +891,7 @@ fn handle_ack_connection_error(err: Cow<'static, str>) -> TransportError {
890891
if let Some(expected) = err.split("expected version").nth(1) {
891892
TransportError::ProtocolVersionMismatch {
892893
expected: expected.trim().to_string(),
893-
actual: version_cmp::VERSION,
894+
actual: PCK_VERSION,
894895
}
895896
} else {
896897
TransportError::ConnectionEstablishmentFailure { cause: err }
@@ -938,9 +939,9 @@ struct InboundRemoteConnection {
938939
}
939940

940941
mod version_cmp {
941-
pub(super) const VERSION: &str = env!("CARGO_PKG_VERSION");
942+
use crate::config::PCK_VERSION;
942943

943-
pub(super) const PROTOC_VERSION: [u8; 8] = parse_version_with_flags(VERSION);
944+
pub(super) const PROTOC_VERSION: [u8; 8] = parse_version_with_flags(PCK_VERSION);
944945

945946
const fn parse_version_with_flags(version: &str) -> [u8; 8] {
946947
let mut major = 0u8;
@@ -1086,7 +1087,7 @@ mod test {
10861087
match handle_ack_connection_error(err_msg.into()) {
10871088
TransportError::ProtocolVersionMismatch { expected, actual } => {
10881089
assert_eq!(expected, "1.2.3");
1089-
assert_eq!(actual, version_cmp::VERSION);
1090+
assert_eq!(actual, PCK_VERSION);
10901091
}
10911092
_ => panic!("Expected ProtocolVersionMismatch error"),
10921093
}

crates/core/tests/operations.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ async fn base_node_test_config(
7171
bandwidth_limit: None,
7272
},
7373
config_paths: {
74-
let mut args = freenet::config::ConfigPathsArgs::default();
75-
args.config_dir = Some(temp_dir.path().to_path_buf());
76-
args.data_dir = Some(temp_dir.path().to_path_buf());
77-
args
74+
freenet::config::ConfigPathsArgs {
75+
config_dir: Some(temp_dir.path().to_path_buf()),
76+
data_dir: Some(temp_dir.path().to_path_buf()),
77+
}
7878
},
7979
secrets: SecretArgs {
8080
transport_keypair: Some(transport_keypair),

0 commit comments

Comments
 (0)