Skip to content

Commit

Permalink
feat: build DfxInterface config with ExtensionManager
Browse files Browse the repository at this point in the history
This is so extension commands can load dfx.json if it references a canister with a type defined by an extension.

See https://dfinity.atlassian.net/browse/SDK-1832
  • Loading branch information
ericswanson-dfinity committed Nov 7, 2024
1 parent 7d36bdd commit 91ea56a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/dfx-core/src/error/interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::error::extension::NewExtensionManagerError;
use std::path::PathBuf;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum NewExtensionManagerFromCachePathError {
#[error("no filename in cache path '{0}'")]
NoCachePathFilename(PathBuf),

#[error("filename in cache path '{0}' is not valid UTF-8")]
CachePathFilenameNotUtf8(PathBuf),

#[error("cannot parse version from cache path filename")]
ParseVersion(#[from] semver::Error),

#[error(transparent)]
NewExtensionManager(#[from] NewExtensionManagerError),
}
1 change: 1 addition & 0 deletions src/dfx-core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod fs;
pub mod get_current_exe;
pub mod get_user_home;
pub mod identity;
pub mod interface;
pub mod keyring;
pub mod load_dfx_config;
pub mod load_networks_config;
Expand Down
45 changes: 43 additions & 2 deletions src/dfx-core/src/interface/builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use crate::error::extension::NewExtensionManagerError;
use crate::error::interface::NewExtensionManagerFromCachePathError;
use crate::extension::manager::ExtensionManager;
use crate::{
config::model::{
dfinity::{Config, NetworksConfig},
Expand All @@ -16,6 +19,8 @@ use crate::{
};
use ic_agent::{agent::route_provider::RoundRobinRouteProvider, Agent, Identity};
use reqwest::Client;
use semver::Version;
use std::path::Path;
use std::sync::Arc;

#[derive(PartialEq)]
Expand All @@ -42,14 +47,17 @@ pub struct DfxInterfaceBuilder {
/// There is no need to set this for the local network, where the root key is fetched by default.
/// This would typically be set for a testnet, or an alias for the local network.
force_fetch_root_key_insecure_non_mainnet_only: bool,

extension_manager: Option<ExtensionManager>,
}

impl DfxInterfaceBuilder {
pub(crate) fn new() -> Self {
pub fn new() -> Self {
Self {
identity: IdentityPicker::Selected,
network: NetworkPicker::Local,
force_fetch_root_key_insecure_non_mainnet_only: false,
extension_manager: None,
}
}

Expand Down Expand Up @@ -77,6 +85,39 @@ impl DfxInterfaceBuilder {
self.with_network(NetworkPicker::Named(name.to_string()))
}

pub fn with_extension_manager(
self,
version: Version,
) -> Result<Self, NewExtensionManagerError> {
let extension_manager = Some(ExtensionManager::new(&version)?);
Ok(Self {
extension_manager,
..self
})
}

pub fn with_extension_manager_from_cache_path(
self,
cache_path: &Path,
) -> Result<Self, NewExtensionManagerFromCachePathError> {
let version = {
let version = cache_path
.file_name()
.ok_or(NewExtensionManagerFromCachePathError::NoCachePathFilename(
cache_path.to_path_buf(),
))?
.to_str()
.ok_or(
NewExtensionManagerFromCachePathError::CachePathFilenameNotUtf8(
cache_path.to_path_buf(),
),
)?;
Version::parse(version)?
};

Ok(self.with_extension_manager(version)?)
}

pub fn with_force_fetch_root_key_insecure_non_mainnet_only(self) -> Self {
Self {
force_fetch_root_key_insecure_non_mainnet_only: true,
Expand All @@ -88,7 +129,7 @@ impl DfxInterfaceBuilder {
let fetch_root_key = self.network == NetworkPicker::Local
|| self.force_fetch_root_key_insecure_non_mainnet_only;
let networks_config = NetworksConfig::new()?;
let config = Config::from_current_dir(None)?.map(Arc::new);
let config = Config::from_current_dir(self.extension_manager.as_ref())?.map(Arc::new);
let network_descriptor = self.build_network_descriptor(config.clone(), &networks_config)?;
let identity = self.build_identity()?;
let agent = self.build_agent(identity.clone(), &network_descriptor)?;
Expand Down

0 comments on commit 91ea56a

Please sign in to comment.