Skip to content

Commit a92a715

Browse files
feat: add dedicated package metadata get function
This is primarily useful for r2modman, which requires additional information about the package in order to display it correctly within the UI.
1 parent 617aeb1 commit a92a715

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/package/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ use crate::TCLI_HOME;
2424

2525
use self::index::PackageIndex;
2626

27+
#[derive(Serialize, Deserialize, Debug)]
28+
pub struct PackageMetadata {
29+
#[serde(flatten)]
30+
manifest: PackageManifestV1,
31+
reference: String,
32+
icon: PathBuf,
33+
}
34+
2735
#[derive(Serialize, Deserialize, Debug, Clone)]
2836
pub enum PackageSource {
2937
Remote(String),
@@ -134,6 +142,24 @@ impl Package {
134142
}
135143
}
136144

145+
/// Get the metadata associated with this package. This will return None
146+
/// the package does not exist locally.
147+
pub async fn get_metadata(&self) -> Result<Option<PackageMetadata>, Error> {
148+
let Some(package_dir) = self.get_path().await else {
149+
return Ok(None);
150+
};
151+
let manifest = {
152+
let str = fs::read_to_string(package_dir.join("manifest.json")).await?;
153+
serde_json::from_str::<PackageManifestV1>(&str)?
154+
};
155+
let icon = package_dir.join("icon.png");
156+
let reference = package_dir.file_name().unwrap().to_string_lossy().to_string();
157+
158+
Ok(Some(PackageMetadata {
159+
manifest,
160+
reference,
161+
icon,
162+
}))
137163
}
138164

139165
pub async fn download(&self, reporter: &dyn ProgressBarTrait) -> Result<PathBuf, Error> {

0 commit comments

Comments
 (0)