feat: enforce numeric IDs for --info command
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
53
src/main.rs
53
src/main.rs
@@ -57,23 +57,48 @@ fn main() -> Result<(), Error> {
|
|||||||
let ids = &mut Vec::new();
|
let ids = &mut Vec::new();
|
||||||
let tags = &mut Vec::new();
|
let tags = &mut Vec::new();
|
||||||
|
|
||||||
for v in args.ids_or_tags.iter() {
|
// For --info mode, treat all arguments as IDs
|
||||||
debug!("MAIN: Parsed value: {:?}", v);
|
if args.mode.info {
|
||||||
match v.clone() {
|
for v in args.ids_or_tags.iter() {
|
||||||
NumberOrString::Number(num) => {
|
debug!("MAIN: Parsed value: {:?}", v);
|
||||||
debug!("MAIN: Adding to ids: {}", num);
|
match v.clone() {
|
||||||
ids.push(num)
|
NumberOrString::Number(num) => {
|
||||||
},
|
debug!("MAIN: Adding to ids: {}", num);
|
||||||
NumberOrString::Str(str) => {
|
ids.push(num)
|
||||||
debug!("MAIN: Adding to tags: {}", str);
|
},
|
||||||
tags.push(str)
|
NumberOrString::Str(str) => {
|
||||||
},
|
// Try to parse as number for --info
|
||||||
|
if let Ok(num) = str.parse::<i64>() {
|
||||||
|
debug!("MAIN: Adding parsed string to ids: {}", num);
|
||||||
|
ids.push(num)
|
||||||
|
} else {
|
||||||
|
cmd.error(
|
||||||
|
ErrorKind::InvalidValue,
|
||||||
|
format!("--info requires numeric IDs, found: '{}'", str)
|
||||||
|
).exit();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Original logic for other modes
|
||||||
|
for v in args.ids_or_tags.iter() {
|
||||||
|
debug!("MAIN: Parsed value: {:?}", v);
|
||||||
|
match v.clone() {
|
||||||
|
NumberOrString::Number(num) => {
|
||||||
|
debug!("MAIN: Adding to ids: {}", num);
|
||||||
|
ids.push(num)
|
||||||
|
},
|
||||||
|
NumberOrString::Str(str) => {
|
||||||
|
debug!("MAIN: Adding to tags: {}", str);
|
||||||
|
tags.push(str)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tags.sort();
|
||||||
|
tags.dedup();
|
||||||
}
|
}
|
||||||
|
|
||||||
tags.sort();
|
|
||||||
tags.dedup();
|
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
enum KeepModes {
|
enum KeepModes {
|
||||||
Unknown,
|
Unknown,
|
||||||
|
|||||||
@@ -20,41 +20,15 @@ pub fn mode_info(
|
|||||||
cmd: &mut Command,
|
cmd: &mut Command,
|
||||||
settings: &config::Settings,
|
settings: &config::Settings,
|
||||||
ids: &mut Vec<i64>,
|
ids: &mut Vec<i64>,
|
||||||
tags: &mut Vec<String>,
|
_tags: &mut Vec<String>,
|
||||||
conn: &mut rusqlite::Connection,
|
conn: &mut rusqlite::Connection,
|
||||||
data_path: PathBuf,
|
data_path: PathBuf,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// Check if any tags can be parsed as numbers and treat them as IDs for --info
|
// For --info, we only use IDs, tags should be empty
|
||||||
let mut numeric_tags = Vec::new();
|
if ids.is_empty() {
|
||||||
let mut non_numeric_tags = Vec::new();
|
cmd.error(ErrorKind::InvalidValue, "No ID provided for --info").exit();
|
||||||
|
|
||||||
for tag in tags.iter() {
|
|
||||||
if let Ok(num) = tag.parse::<i64>() {
|
|
||||||
numeric_tags.push(num);
|
|
||||||
} else {
|
|
||||||
non_numeric_tags.push(tag.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we have numeric tags and no IDs, use them as IDs
|
|
||||||
if ids.is_empty() && !numeric_tags.is_empty() {
|
|
||||||
// For --info, we can only handle one ID
|
|
||||||
if numeric_tags.len() > 1 {
|
|
||||||
cmd.error(ErrorKind::InvalidValue, "More than one ID given, you must supply exactly one ID or atleast one tag when using --info").exit();
|
|
||||||
}
|
|
||||||
// Move the numeric tag to ids
|
|
||||||
ids.push(numeric_tags[0]);
|
|
||||||
// Clear the tags since we're now using an ID
|
|
||||||
tags.clear();
|
|
||||||
} else {
|
|
||||||
// Use the original logic
|
|
||||||
*tags = non_numeric_tags;
|
|
||||||
}
|
|
||||||
|
|
||||||
if !ids.is_empty() && !tags.is_empty() {
|
|
||||||
cmd.error(ErrorKind::InvalidValue, "Both ID and tags given, you must supply exactly one ID or atleast one tag when using --info").exit();
|
|
||||||
} else if ids.len() > 1 {
|
} else if ids.len() > 1 {
|
||||||
cmd.error(ErrorKind::InvalidValue, "More than one ID given, you must supply exactly one ID or atleast one tag when using --info").exit();
|
cmd.error(ErrorKind::InvalidValue, "More than one ID given, you must supply exactly one ID when using --info").exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut meta: std::collections::HashMap<String, String> = std::collections::HashMap::new();
|
let mut meta: std::collections::HashMap<String, String> = std::collections::HashMap::new();
|
||||||
@@ -67,8 +41,9 @@ pub fn mode_info(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let item_service = ItemService::new(data_path.clone());
|
let item_service = ItemService::new(data_path.clone());
|
||||||
|
// Use empty tags vector since --info only works with IDs
|
||||||
let item_with_meta = item_service
|
let item_with_meta = item_service
|
||||||
.find_item(conn, ids, tags, &meta)
|
.find_item(conn, ids, &Vec::new(), &meta)
|
||||||
.map_err(|e| anyhow!("Unable to find matching item in database: {}", e))?;
|
.map_err(|e| anyhow!("Unable to find matching item in database: {}", e))?;
|
||||||
|
|
||||||
show_item(item_with_meta, settings, data_path)
|
show_item(item_with_meta, settings, data_path)
|
||||||
|
|||||||
Reference in New Issue
Block a user