Skip to content

Commit

Permalink
Add update document from modified text file
Browse files Browse the repository at this point in the history
This update adds support for modifying documents. This is done by using
the normal 'get' subcommand and redirecting the output to a file. After
modifying this file, specify it with the 'update' subcommand.
  • Loading branch information
ryanfrishkorn committed Aug 20, 2023
1 parent 0a6a793 commit a29db54
Show file tree
Hide file tree
Showing 3 changed files with 294 additions and 43 deletions.
79 changes: 58 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,17 @@ fn main() -> Result<(), Box<dyn Error>> {
.about("Stem word from stdin")
.arg_required_else_help(false)
.arg(Arg::new("words")),
)
.subcommand(
Command::new("update")
.about("Update document from modified file")
.arg_required_else_help(true)
.arg(
Arg::new("file")
.help("edited document file")
.num_args(1)
.action(ArgAction::Append),
),
);

let matches = cmd.get_matches();
Expand Down Expand Up @@ -445,28 +456,35 @@ fn main() -> Result<(), Box<dyn Error>> {
true => print!("{}", s.text),
// formatted output
false => {
println!(
"uuid: {}\nname: {}\ntimestamp: {}\n----",
s.uuid, s.name, s.timestamp
);

// add a newline if not already present
match s.text.chars().last() {
Some(v) if v == '\n' => println!("{}----", s.text),
_ => println!("{}\n----", s.text),
}

// show attachments
s.collect_attachments(&conn)?;
if !s.attachments.is_empty() {
println!("attachments:");

println!("{:<36} {:>10} name", "uuid", "bytes");
for a in &s.attachments {
println!("{} {:>10} {}", a.uuid, a.size, a.name);
}
}
}
s.print();
} /*
false => {
println!(
"uuid: {}\nname: {}\ntimestamp: {}\n----",
s.uuid,
s.name,
s.timestamp.to_rfc3339()
);
// add a newline if not already present
match s.text.chars().last() {
Some(v) if v == '\n' => println!("{}----", s.text),
_ => println!("{}\n----", s.text),
}
// show attachments
s.collect_attachments(&conn)?;
if !s.attachments.is_empty() {
println!("attachments:");
println!("{:<36} {:>10} name", "uuid", "bytes");
for a in &s.attachments {
println!("{} {:>10} {}", a.uuid, a.size, a.name);
}
}
}
*/
}
}

Expand Down Expand Up @@ -755,6 +773,25 @@ fn main() -> Result<(), Box<dyn Error>> {
println!("{:?}", stems);
}

// UPDATE
if let Some(("update", sub_matches)) = matches.subcommand() {
if let Some(file) = sub_matches.get_one::<String>("file") {
let s = snip::from_file(file)?;
s.update(&conn)?;
let mut s = snip::get_from_uuid(&conn, &s.uuid)?;
// re-index due to changed content
s.index(&conn)?;
eprintln!("update successful");

// collect attachments before printing so they are included in output
s.collect_attachments(&conn)?;
s.print();
} else {
eprintln!("update failed");
std::process::exit(1);
}
}

Ok(())
}

Expand Down
Loading

0 comments on commit a29db54

Please sign in to comment.