Get fields in item cmd
This commit is contained in:
@@ -20,6 +20,13 @@ pub struct ItemCommand {
|
||||
#[arg(long, short = 'p')]
|
||||
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
|
||||
#[arg(long, short = 'c', default_value = "./pile.toml")]
|
||||
config: PathBuf,
|
||||
@@ -42,24 +49,57 @@ impl CliCmd for ItemCommand {
|
||||
|
||||
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 path: ObjectPath = path_str
|
||||
.parse()
|
||||
.with_context(|| format!("invalid path {path_str:?}"))?;
|
||||
|
||||
ds.get_field(&state, &source, &self.key, &path)
|
||||
let v = pv
|
||||
.query(&state, &path)
|
||||
.await
|
||||
.with_context(|| format!("while extracting {}", self.key))?
|
||||
.ok_or_else(|| {
|
||||
anyhow::anyhow!("{:?} not found in source {:?}", self.key, self.source)
|
||||
})?
|
||||
} else {
|
||||
let item = ds.get(&source, &self.key).await.ok_or_else(|| {
|
||||
anyhow::anyhow!("{:?} not found in source {:?}", self.key, self.source)
|
||||
})?;
|
||||
|
||||
let item = PileValue::Item(item);
|
||||
item.to_json(&state)
|
||||
v.to_json(&state)
|
||||
.await
|
||||
.with_context(|| format!("while extracting {}", self.key))?
|
||||
} else {
|
||||
pv.to_json(&state)
|
||||
.await
|
||||
.with_context(|| format!("while extracting {}", self.key))?
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user