Get fields in item cmd

This commit is contained in:
2026-03-21 10:19:02 -07:00
parent 44466f16cf
commit 7caf2553bc

View File

@@ -20,6 +20,13 @@ pub struct ItemCommand {
#[arg(long, short = 'p')] #[arg(long, short = 'p')]
path: Option<String>, path: Option<String>,
/// If present, print the schema fields instead of item data
#[arg(long)]
fields: bool,
#[arg(long, short = 'x')]
exclude: Vec<String>,
/// Path to dataset config /// Path to dataset config
#[arg(long, short = 'c', default_value = "./pile.toml")] #[arg(long, short = 'c', default_value = "./pile.toml")]
config: PathBuf, config: PathBuf,
@@ -42,24 +49,57 @@ impl CliCmd for ItemCommand {
let state = ExtractState { ignore_mime: false }; let state = ExtractState { ignore_mime: false };
let item = ds.get(&source, &self.key).await.ok_or_else(|| {
anyhow::anyhow!("{:?} not found in source {:?}", self.key, self.source)
})?;
let pv = PileValue::Item(item);
if self.fields {
let mut map = serde_json::Map::new();
for (name, spec) in &ds.config.schema {
if self.exclude.contains(&name.to_string()) {
continue;
}
let mut value = None;
for path in &spec.path {
let v = pv
.query(&state, path)
.await
.with_context(|| format!("while extracting field {name}"))?;
if let Some(v) = v {
let j = v
.to_json(&state)
.await
.with_context(|| format!("while extracting field {name}"))?;
value = Some(j);
break;
}
}
map.insert(name.to_string(), value.unwrap_or(serde_json::Value::Null));
}
let json = serde_json::to_string_pretty(&serde_json::Value::Object(map)).unwrap();
println!("{json}");
return Ok(0);
}
let json = if let Some(path_str) = self.path { let json = if let Some(path_str) = self.path {
let path: ObjectPath = path_str let path: ObjectPath = path_str
.parse() .parse()
.with_context(|| format!("invalid path {path_str:?}"))?; .with_context(|| format!("invalid path {path_str:?}"))?;
ds.get_field(&state, &source, &self.key, &path) let v = pv
.query(&state, &path)
.await .await
.with_context(|| format!("while extracting {}", self.key))? .with_context(|| format!("while extracting {}", self.key))?
.ok_or_else(|| { .ok_or_else(|| {
anyhow::anyhow!("{:?} not found in source {:?}", self.key, self.source) anyhow::anyhow!("{:?} not found in source {:?}", self.key, self.source)
})? })?;
v.to_json(&state)
.await
.with_context(|| format!("while extracting {}", self.key))?
} else { } else {
let item = ds.get(&source, &self.key).await.ok_or_else(|| { pv.to_json(&state)
anyhow::anyhow!("{:?} not found in source {:?}", self.key, self.source)
})?;
let item = PileValue::Item(item);
item.to_json(&state)
.await .await
.with_context(|| format!("while extracting {}", self.key))? .with_context(|| format!("while extracting {}", self.key))?
}; };