class RmOld::App

Public Instance Methods

main() click to toggle source
# File lib/rm_old.rb, line 6
def main
        opts = Trollop::options do
                version "rm_old --age=n [--test] <files> (c) 2016 @reednj"
                opt :age, "delete any files older than this (ex. 1d, 36h)", :type => :string
                opt :test, "print the list of matched files, but don't delete"
        end

        if opts[:age].nil?
                # the only thing we really need specified is the age. We can use
                # defaults for everything else
                Trollop::educate
        end

        max_age = opts[:age].to_duration
        matched = ARGV.select {|f| File.file?(f) && File.mtime(f).age > max_age }

        puts 'Listing files only, will not delete' if opts[:test]
        matched.each do |f|
                if !opts[:test]
                        File.delete(f)
                else
                        puts f
                end
        end
end