Co-authored-by: aider (openai/andrew/openrouter/anthropic/claude-sonnet-4) <aider@aider.chat>
58 lines
1.3 KiB
Rust
58 lines
1.3 KiB
Rust
use anyhow::Result;
|
|
use clap::Command;
|
|
use std::io::{Read, Write};
|
|
|
|
use crate::config;
|
|
use crate::services::item_service::ItemService;
|
|
|
|
fn validate_save_args(cmd: &mut Command, ids: &Vec<i64>) {
|
|
if !ids.is_empty() {
|
|
cmd.error(
|
|
clap::error::ErrorKind::InvalidValue,
|
|
"ID given, you cannot supply IDs when using --save",
|
|
)
|
|
.exit();
|
|
}
|
|
}
|
|
|
|
// Tee reader that writes to a writer as it is read
|
|
struct TeeReader<R: Read, W: Write> {
|
|
reader: R,
|
|
writer: W,
|
|
}
|
|
|
|
impl<R: Read, W: Write> Read for TeeReader<R, W> {
|
|
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
|
let n = self.reader.read(buf)?;
|
|
if n > 0 {
|
|
self.writer.write_all(&buf[..n])?;
|
|
}
|
|
Ok(n)
|
|
}
|
|
}
|
|
|
|
pub fn mode_save(
|
|
cmd: &mut Command,
|
|
settings: &config::Settings,
|
|
ids: &mut Vec<i64>,
|
|
tags: &mut Vec<String>,
|
|
conn: &mut rusqlite::Connection,
|
|
data_path: std::path::PathBuf,
|
|
) -> Result<(), anyhow::Error> {
|
|
validate_save_args(cmd, ids);
|
|
|
|
let item_service = ItemService::new(data_path);
|
|
|
|
let stdin = std::io::stdin();
|
|
let stdout = std::io::stdout();
|
|
|
|
let tee_reader = TeeReader {
|
|
reader: stdin.lock(),
|
|
writer: stdout.lock(),
|
|
};
|
|
|
|
let _item_id = item_service.save_item(tee_reader, cmd, settings, tags, conn)?;
|
|
|
|
Ok(())
|
|
}
|