feat: add --ids-only flag to --list mode for scripting
Outputs one ID per line with no header. Errors if used with any mode other than --list. Works with both local and client (remote) list.
This commit is contained in:
@@ -237,6 +237,10 @@ pub struct OptionsArgs {
|
|||||||
#[arg(help("Display file sizes with units"))]
|
#[arg(help("Display file sizes with units"))]
|
||||||
pub human_readable: bool,
|
pub human_readable: bool,
|
||||||
|
|
||||||
|
#[arg(long)]
|
||||||
|
#[arg(help("Only output item IDs (for scripting)"))]
|
||||||
|
pub ids_only: bool,
|
||||||
|
|
||||||
#[arg(short, long, action = clap::ArgAction::Count, conflicts_with("quiet"))]
|
#[arg(short, long, action = clap::ArgAction::Count, conflicts_with("quiet"))]
|
||||||
#[arg(help("Increase message verbosity, can be given more than once"))]
|
#[arg(help("Increase message verbosity, can be given more than once"))]
|
||||||
pub verbose: u8,
|
pub verbose: u8,
|
||||||
|
|||||||
@@ -191,6 +191,8 @@ pub struct Settings {
|
|||||||
pub table_config: TableConfig,
|
pub table_config: TableConfig,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub human_readable: bool,
|
pub human_readable: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub ids_only: bool,
|
||||||
pub output_format: Option<String>,
|
pub output_format: Option<String>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub quiet: bool,
|
pub quiet: bool,
|
||||||
@@ -277,6 +279,10 @@ impl Settings {
|
|||||||
config_builder = config_builder.set_override("human_readable", true)?;
|
config_builder = config_builder.set_override("human_readable", true)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if args.options.ids_only {
|
||||||
|
config_builder = config_builder.set_override("ids_only", true)?;
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(output_format) = &args.options.output_format {
|
if let Some(output_format) = &args.options.output_format {
|
||||||
config_builder =
|
config_builder =
|
||||||
config_builder.set_override("output_format", output_format.as_str())?;
|
config_builder.set_override("output_format", output_format.as_str())?;
|
||||||
|
|||||||
@@ -196,6 +196,15 @@ fn main() -> Result<(), Error> {
|
|||||||
.exit();
|
.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate ids-only usage
|
||||||
|
if settings.ids_only && mode != KeepModes::List {
|
||||||
|
cmd.error(
|
||||||
|
ErrorKind::InvalidValue,
|
||||||
|
"--ids-only can only be used with --list mode",
|
||||||
|
)
|
||||||
|
.exit();
|
||||||
|
}
|
||||||
|
|
||||||
debug!("MAIN: args: {args:?}");
|
debug!("MAIN: args: {args:?}");
|
||||||
debug!("MAIN: ids: {ids:?}");
|
debug!("MAIN: ids: {ids:?}");
|
||||||
debug!("MAIN: tags: {tags:?}");
|
debug!("MAIN: tags: {tags:?}");
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::client::KeepClient;
|
use crate::client::KeepClient;
|
||||||
use crate::modes::common::{
|
use crate::modes::common::{
|
||||||
ColumnType, OutputFormat, format_size, render_list_table_with_format, settings_output_format,
|
format_size, render_list_table_with_format, settings_output_format, ColumnType, OutputFormat,
|
||||||
};
|
};
|
||||||
use clap::Command;
|
use clap::Command;
|
||||||
use log::debug;
|
use log::debug;
|
||||||
@@ -21,6 +21,13 @@ pub fn mode(
|
|||||||
.collect();
|
.collect();
|
||||||
let items = client.list_items(tags, "newest", 0, 100, &meta_filter)?;
|
let items = client.list_items(tags, "newest", 0, 100, &meta_filter)?;
|
||||||
|
|
||||||
|
if settings.ids_only {
|
||||||
|
for item in &items {
|
||||||
|
println!("{}", item.id);
|
||||||
|
}
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
let output_format = settings_output_format(settings);
|
let output_format = settings_output_format(settings);
|
||||||
|
|
||||||
match output_format {
|
match output_format {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
/// including table, JSON, and YAML.
|
/// including table, JSON, and YAML.
|
||||||
use crate::config;
|
use crate::config;
|
||||||
use crate::modes::common::ColumnType;
|
use crate::modes::common::ColumnType;
|
||||||
use crate::modes::common::{OutputFormat, apply_color, apply_table_attribute, format_size};
|
use crate::modes::common::{apply_color, apply_table_attribute, format_size, OutputFormat};
|
||||||
use crate::services::item_service::ItemService;
|
use crate::services::item_service::ItemService;
|
||||||
use crate::services::types::ItemWithMeta;
|
use crate::services::types::ItemWithMeta;
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
@@ -104,6 +104,15 @@ pub fn mode_list(
|
|||||||
.collect();
|
.collect();
|
||||||
let items_with_meta = item_service.list_items(conn, tags, &meta_filter)?;
|
let items_with_meta = item_service.list_items(conn, tags, &meta_filter)?;
|
||||||
|
|
||||||
|
if settings.ids_only {
|
||||||
|
for item_with_meta in &items_with_meta {
|
||||||
|
if let Some(id) = item_with_meta.item.id {
|
||||||
|
println!("{id}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
let output_format = crate::modes::common::settings_output_format(settings);
|
let output_format = crate::modes::common::settings_output_format(settings);
|
||||||
|
|
||||||
if output_format != OutputFormat::Table {
|
if output_format != OutputFormat::Table {
|
||||||
|
|||||||
Reference in New Issue
Block a user