From 18a4d27a9e9821c217fc22601b4363c36c31d079 Mon Sep 17 00:00:00 2001 From: Chris Schlaeger Date: Tue, 22 Jul 2014 21:06:57 +0200 Subject: 'delete' command implemented. This command allows to remove activities from the DB. These will not be imported again. --- lib/postrunner/ActivitiesDB.rb | 27 +++++++++++++++++++-------- lib/postrunner/Main.rb | 7 +++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/postrunner/ActivitiesDB.rb b/lib/postrunner/ActivitiesDB.rb index c8785fb..17e1760 100644 --- a/lib/postrunner/ActivitiesDB.rb +++ b/lib/postrunner/ActivitiesDB.rb @@ -42,6 +42,11 @@ module PostRunner return false end + if File.exists(File.join(@fit_dir, base_fit_file)) + Log.debug "Activity #{fit_file} has been deleted before" + return false + end + begin fit_activity = Fit4Ruby.read(fit_file) rescue Fit4Ruby::Error @@ -65,6 +70,12 @@ module PostRunner true end + def delete(fit_file) + base_fit_file = File.basename(fit_file) + index = @activities.index { |a| a.fit_file == base_fit_file } + @activities.delete_at(index) + sync + end def rename(fit_file, name) base_fit_file = File.basename(fit_file) @@ -129,16 +140,16 @@ module PostRunner end def create_directories - Log.info "Creating data directory #{@db_dir}" - begin - Dir.mkdir(@db_dir) - rescue - Log.fatal "Cannot create data directory #{@db_dir}: #{$!}" - end + create_directory(@db_dir, 'data') + create_directory(@fit_dir, 'fit') + end + + def create_directory(dir, name) + Log.info "Creating #{name} directory #{dir}" begin - Dir.mkdir(@fit_dir) + Dir.mkdir(dir) rescue - Log.fatal "Cannot create fit directory #{@fit_dir}: #{$!}" + Log.fatal "Cannot create #{name} directory #{dir}: #{$!}" end end diff --git a/lib/postrunner/Main.rb b/lib/postrunner/Main.rb index 7fb09f5..5187011 100644 --- a/lib/postrunner/Main.rb +++ b/lib/postrunner/Main.rb @@ -98,6 +98,9 @@ dump | import | Import the provided FIT file(s) into the postrunner database. +delete + Delete the activity from the archive. + list List all FIT files stored in the data base. @@ -118,6 +121,8 @@ EOT case (cmd = args.shift) when 'check' process_files_or_activities(args, :check) + when 'delete' + process_activities(args, :delete) when 'dump' @filter = Fit4Ruby::FitFilter.new unless @filter process_files_or_activities(args, :dump) @@ -188,6 +193,8 @@ EOT def process_file(file, command) case command + when :delete + @activities.delete(file) when :import @activities.add(file) when :rename -- cgit v1.2.3