Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
adamspofford-dfinity committed Jan 7, 2025
1 parent c03e41a commit 5c1cc97
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/dfx-core/src/config/model/local_server_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use time::OffsetDateTime;

use super::replica_config::CachedReplicaConfig;

#[derive(Deserialize, Serialize, Debug)]
pub struct NetworkMetadata {
pub created: OffsetDateTime,
Expand Down Expand Up @@ -394,6 +396,12 @@ impl LocalServerDescriptor {
None => Ok(None),
}
}

pub fn is_pocketic(&self) -> Result<bool, StructuredFileError> {
Ok(self
.effective_config()?
.is_some_and(|cfg| matches!(cfg.config, CachedReplicaConfig::PocketIc { .. })))
}
}

/// Reads a port number from a file.
Expand Down
16 changes: 11 additions & 5 deletions src/dfx/src/commands/info/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod pocketic_config_port;
mod replica_port;
mod webserver_port;
use crate::commands::info::{replica_port::get_replica_port, webserver_port::get_webserver_port};
Expand All @@ -10,21 +11,25 @@ use crate::Environment;
use anyhow::{bail, Context};
use clap::{Parser, Subcommand};
use dfx_core::config::model::dfinity::NetworksConfig;
use pocketic_config_port::get_pocketic_config_port;

#[derive(Subcommand, Clone, Debug)]
enum InfoType {
/// Show the URL of the Candid UI canister
CandidUiUrl,
/// Show the headers that gets applied to assets in .ic-assets.json5 if "security_policy" is "standard" or "hardened".
SecurityPolicy,
/// Show the port of the local replica
/// Show the port of the local IC API/webserver
#[command(alias = "webserver-port")]
ReplicaPort,
/// Show the revision of the replica shipped with this dfx binary
ReplicaRev,
/// Show the port of the webserver
WebserverPort,
/// Show the path to network configuration file
NetworksJsonPath,
/// Show the port the native replica is using, if it is running
NativeReplicaPort,
/// Show the port that PocketIC is using, if it is running
PocketicConfigPort,
}

#[derive(Parser)]
Expand Down Expand Up @@ -53,9 +58,10 @@ pub fn exec(env: &dyn Environment, opts: InfoOpts) -> DfxResult {
InfoType::SecurityPolicy => {
ic_asset::security_policy::SecurityPolicy::Standard.to_json5_str()
}
InfoType::ReplicaPort => get_replica_port(env)?,
InfoType::NativeReplicaPort => get_replica_port(env)?,
InfoType::PocketicConfigPort => get_pocketic_config_port(env)?,
InfoType::ReplicaRev => info::replica_rev().to_string(),
InfoType::WebserverPort => get_webserver_port(env)?,
InfoType::ReplicaPort => get_webserver_port(env)?,
InfoType::NetworksJsonPath => NetworksConfig::new()?
.get_path()
.to_str()
Expand Down
25 changes: 25 additions & 0 deletions src/dfx/src/commands/info/pocketic_config_port.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use anyhow::{bail, ensure};
use dfx_core::network::provider::{create_network_descriptor, LocalBindDetermination};

use crate::lib::{environment::Environment, error::DfxResult};

pub(crate) fn get_pocketic_config_port(env: &dyn Environment) -> DfxResult<String> {
let network_descriptor = create_network_descriptor(
env.get_config()?,
env.get_networks_config(),
None,
None,
LocalBindDetermination::AsConfigured,
)?;
let local = network_descriptor.local_server_descriptor()?;
ensure!(
local.is_pocketic()?,
"The running server is a native replica, not PocketIC"
);
let logger = None;
if let Some(port) = local.get_running_pocketic_port(logger)? {
Ok(format!("{}", port))
} else {
bail!("No PocketIC port found");
}
}
13 changes: 7 additions & 6 deletions src/dfx/src/commands/info/replica_port.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::lib::error::DfxResult;
use crate::Environment;
use anyhow::bail;
use anyhow::{bail, ensure};
use dfx_core::network::provider::{create_network_descriptor, LocalBindDetermination};

pub(crate) fn get_replica_port(env: &dyn Environment) -> DfxResult<String> {
Expand All @@ -11,12 +11,13 @@ pub(crate) fn get_replica_port(env: &dyn Environment) -> DfxResult<String> {
None,
LocalBindDetermination::AsConfigured,
)?;

let local = network_descriptor.local_server_descriptor()?;
ensure!(
!local.is_pocketic()?,
"The running server is PocketIC, not a native replica"
);
let logger = None;
if let Some(port) = network_descriptor
.local_server_descriptor()?
.get_running_replica_port(logger)?
{
if let Some(port) = local.get_running_replica_port(logger)? {
Ok(format!("{}", port))
} else {
bail!("No replica port found");
Expand Down

0 comments on commit 5c1cc97

Please sign in to comment.