organize/organize.rb
2016-02-16 19:47:18 +01:00

46 lines
1.1 KiB
Ruby

#!/usr/bin/env ruby
require 'json'
require 'fileutils'
organizer = JSON.parse(File.read(File.expand_path('~/.config/organize/organizer.json')))
organizer.each do |match|
puts "[+] Match: "+match["match"]
keep_answer = false
answer = nil
Dir.glob(match["match"]).each do |file|
file = File.realdirpath(file)
puts " [-] File matched "+file
if match.has_key?("ask_confirm") and match["ask_confirm"]
if !keep_answer
print "Really y,n ! "
answer = gets.chomp
end
keep_answer = true if answer.match /(y|n)!/
case answer
when /y!?/
when /n!?/
next
end
end
match["action"] = "none" unless match.has_key?("action") # no action if action not exists
case match["action"]
when "exec"
if match.has_key?("exec")
system match["exec"]+' "'+File.expand_path(file)+'"'
end
when "move"
if match.has_key?("to")
FileUtils.mv(file, File.expand_path(match["to"]))
end
when "remove"
FileUtils.rm(file)
when "none"
else
end
end
end