Update file history in more CLI actions

This commit is contained in:
timvisee 2018-04-22 16:38:55 +02:00
parent e7d3064ad7
commit fa96930645
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2
5 changed files with 57 additions and 4 deletions

View file

@ -1,4 +1,5 @@
use clap::ArgMatches;
use failure::Fail;
use ffsend_api::action::delete::{
Error as DeleteError,
Delete as ApiDelete,
@ -15,7 +16,8 @@ use cmd::matcher::{
main::MainMatcher,
};
use error::ActionError;
use util::{ensure_owner_token, print_success};
use history::History;
use util::{ensure_owner_token, print_error, print_success};
/// A file delete action.
pub struct Delete<'a> {
@ -52,6 +54,17 @@ impl<'a> Delete<'a> {
// Send the file deletion request
ApiDelete::new(&file, None).invoke(&client)?;
// Remove the file from the history manager
// TODO: also remove if it was expired
if let Err(err) = History::load_remove_save(
matcher_main.history(),
&file,
) {
print_error(err.context(
"Failed to remove file from local history, ignoring",
));
}
// Print a success message
print_success("File deleted");

View file

@ -1,4 +1,5 @@
use clap::ArgMatches;
use failure::Fail;
use ffsend_api::action::exists::{
Error as ExistsError,
Exists as ApiExists,
@ -12,8 +13,11 @@ use ffsend_api::reqwest::Client;
use cmd::matcher::{
Matcher,
exists::ExistsMatcher,
main::MainMatcher,
};
use error::ActionError;
use history::History;
use util::print_error;
/// A file exists action.
pub struct Exists<'a> {
@ -33,6 +37,7 @@ impl<'a> Exists<'a> {
pub fn invoke(&self) -> Result<(), ActionError> {
// Create the command matchers
let matcher_exists = ExistsMatcher::with(self.cmd_matches).unwrap();
let matcher_main = MainMatcher::with(self.cmd_matches).unwrap();
// Get the share URL
let url = matcher_exists.url();
@ -54,6 +59,19 @@ impl<'a> Exists<'a> {
println!("Password: {:?}", exists_response.has_password());
}
// Remove the file from the history manager if it doesn't exist
// TODO: add if it does exist
if !exists {
if let Err(err) = History::load_remove_save(
matcher_main.history(),
&file,
) {
print_error(err.context(
"Failed to remove file from local history, ignoring",
));
}
}
Ok(())
}
}

View file

@ -20,6 +20,7 @@ use cmd::matcher::{
info::InfoMatcher,
main::MainMatcher,
};
use history::History;
use util::{ensure_owner_token, ensure_password, print_error};
/// A file info action.
@ -58,6 +59,16 @@ impl<'a> Info<'a> {
// Check whether the file exists
let exists = ApiExists::new(&file).invoke(&client)?;
if !exists.exists() {
// Remove the file from the history manager if it doesn't exist
if let Err(err) = History::load_remove_save(
matcher_main.history(),
&file,
) {
print_error(err.context(
"Failed to remove file from local history, ignoring",
));
}
return Err(Error::Expired);
}

View file

@ -130,7 +130,9 @@ impl<'a> Upload<'a> {
matcher_main.history(),
file.clone(),
) {
print_error(err.context("Failed to add file to history, ignoring"));
print_error(err.context(
"Failed to add file to local history, ignoring",
));
}
// Open the URL in the browser

View file

@ -111,14 +111,23 @@ impl History {
/// Load the history from the given path, add the given file, and save it
/// again.
/// If there is not history file at the given path, a new empty one will
/// be created.
/// If there is no file at the given path, new history will be created.
pub fn load_add_save(path: PathBuf, file: RemoteFile) -> Result<(), Error> {
let mut history = Self::load_or_new(path)?;
history.add(file);
history.save().map_err(|err| err.into())
}
/// Load the history from the given path, remove the given file by it's
/// ID, and save it again.
/// True is returned if any file was removed.
pub fn load_remove_save(path: PathBuf, file: &RemoteFile) -> Result<bool, Error> {
let mut history = Self::load_or_new(path)?;
let removed = history.remove(file);
history.save()?;
Ok(removed)
}
/// Add the given remote file to the history.
pub fn add(&mut self, file: RemoteFile) {
self.files.push(file);