-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ability to list remote available extensions (#3896)
* Split the extension list function into a separate file. * Add support for listing remote extensions. * Format files. * Add 'ListRemoteExtensionsError' error type. * Address the review comments, mainly keep the behavior consistent with other commands. * Add e2e tests for listing available extensions. * Address review comments. * Update changelog.
- Loading branch information
1 parent
77164f9
commit 8072c82
Showing
7 changed files
with
135 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use super::ExtensionManager; | ||
use crate::error::extension::{ListAvailableExtensionsError, ListInstalledExtensionsError}; | ||
use crate::extension::catalog::ExtensionCatalog; | ||
use crate::extension::installed::InstalledExtensionList; | ||
use crate::extension::ExtensionName; | ||
use std::vec; | ||
use url::Url; | ||
|
||
pub type AvailableExtensionList = Vec<ExtensionName>; | ||
|
||
impl ExtensionManager { | ||
pub fn list_installed_extensions( | ||
&self, | ||
) -> Result<InstalledExtensionList, ListInstalledExtensionsError> { | ||
if !self.dir.exists() { | ||
return Ok(vec![]); | ||
} | ||
let dir_content = crate::fs::read_dir(&self.dir)?; | ||
|
||
let extensions = dir_content | ||
.filter_map(|v| { | ||
let dir_entry = v.ok()?; | ||
if dir_entry.file_type().map_or(false, |e| e.is_dir()) | ||
&& !dir_entry.file_name().to_str()?.starts_with(".tmp") | ||
{ | ||
let name = dir_entry.file_name().to_string_lossy().to_string(); | ||
Some(name) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect(); | ||
Ok(extensions) | ||
} | ||
|
||
pub async fn list_available_extensions( | ||
&self, | ||
catalog_url: Option<&Url>, | ||
) -> Result<AvailableExtensionList, ListAvailableExtensionsError> { | ||
let catalog = ExtensionCatalog::fetch(catalog_url) | ||
.await | ||
.map_err(ListAvailableExtensionsError::FetchCatalog)?; | ||
let extensions: Vec<String> = catalog.0.into_keys().collect(); | ||
|
||
Ok(extensions) | ||
} | ||
} |
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,22 +1,63 @@ | ||
use crate::lib::environment::Environment; | ||
use crate::lib::error::DfxResult; | ||
use clap::Parser; | ||
use std::io::Write; | ||
use tokio::runtime::Runtime; | ||
use url::Url; | ||
|
||
pub fn exec(env: &dyn Environment) -> DfxResult<()> { | ||
#[derive(Parser)] | ||
pub struct ListOpts { | ||
/// Specifies to list the available remote extensions. | ||
#[arg(long)] | ||
available: bool, | ||
/// Specifies the URL of the catalog to use to find the extension. | ||
#[clap(long)] | ||
catalog_url: Option<Url>, | ||
} | ||
|
||
pub fn exec(env: &dyn Environment, opts: ListOpts) -> DfxResult<()> { | ||
let mgr = env.get_extension_manager(); | ||
let extensions = mgr.list_installed_extensions()?; | ||
|
||
if opts.available || opts.catalog_url.is_some() { | ||
let runtime = Runtime::new().expect("Unable to create a runtime"); | ||
let extensions = runtime.block_on(async { | ||
mgr.list_available_extensions(opts.catalog_url.as_ref()) | ||
.await | ||
})?; | ||
|
||
display_extension_list( | ||
&extensions, | ||
"No extensions available.", | ||
"Available extensions:", | ||
) | ||
} else { | ||
let extensions = mgr.list_installed_extensions()?; | ||
|
||
display_extension_list( | ||
&extensions, | ||
"No extensions installed.", | ||
"Installed extensions:", | ||
) | ||
} | ||
} | ||
|
||
fn display_extension_list( | ||
extensions: &Vec<String>, | ||
empty_msg: &str, | ||
header_msg: &str, | ||
) -> DfxResult<()> { | ||
if extensions.is_empty() { | ||
eprintln!("No extensions installed."); | ||
eprintln!("{}", empty_msg); | ||
return Ok(()); | ||
} | ||
|
||
eprintln!("Installed extensions:"); | ||
eprintln!("{}", header_msg); | ||
for extension in extensions { | ||
eprint!(" "); | ||
std::io::stderr().flush()?; | ||
println!("{}", extension); | ||
std::io::stdout().flush()?; | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains 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