feat: add pack --list subcommand to Ya CLI (#1110)

Co-authored-by: 三咲雅 · Misaki Masa <sxyazi@gmail.com>
This commit is contained in:
Filipe Paniguel 2024-06-01 13:30:14 -03:00 committed by GitHub
parent 95e960a64a
commit add801f28e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 0 deletions

View File

@ -91,6 +91,9 @@ pub(super) struct CommandPack {
/// Install all packages.
#[arg(short = 'i', long)]
pub(super) install: bool,
/// List all packages.
#[arg(short = 'l', long)]
pub(super) list: bool,
/// Upgrade all packages.
#[arg(short = 'u', long)]
pub(super) upgrade: bool,

View File

@ -36,6 +36,9 @@ async fn main() -> anyhow::Result<()> {
if cmd.install {
package::Package::install_from_config("plugin", false).await?;
package::Package::install_from_config("flavor", false).await?;
} else if cmd.list {
package::Package::list_from_config("plugin").await?;
package::Package::list_from_config("flavor").await?;
} else if cmd.upgrade {
package::Package::install_from_config("plugin", true).await?;
package::Package::install_from_config("flavor", true).await?;

View File

@ -66,6 +66,28 @@ impl Package {
fs::write(path, doc.to_string()).await.context("Failed to write package.toml")
}
pub(crate) async fn list_from_config(section: &str) -> Result<()> {
let path = Xdg::config_dir().join("package.toml");
let Ok(s) = fs::read_to_string(&path).await else {
return Ok(());
};
let doc = s.parse::<DocumentMut>().context("Failed to parse package.toml")?;
let Some(deps) = doc.get(section).and_then(|d| d.get("deps")) else {
return Ok(());
};
let deps = deps.as_array().context("`deps` must be an array")?;
println!("{section}s:");
for dep in deps {
if let Some(Value::String(use_)) = dep.as_inline_table().and_then(|t| t.get("use")) {
println!("\t{}", use_.value());
}
}
Ok(())
}
fn ensure_config(s: &str) -> Result<DocumentMut> {
let mut doc = s.parse::<DocumentMut>().context("Failed to parse package.toml")?;