# # # = Jeeves Store CLI # # == Jeeves # # Author:: Robert Sharp # Copyright:: Copyright (c) 2014 Robert Sharp # License:: Open Software Licence v3.0 # # This software is licensed for use under the Open Software Licence v. 3.0 # The terms of this licence can be found at http://www.opensource.org/licenses/osl-3.0.php # and in the file copyright.txt. Under the terms of this licence, all derivative works # must themselves be licensed under the Open Software Licence v. 3.0 # # # require 'optplus/nested' class StoreCLI < Optplus::NestedParser include Jeni::IO usage "store command" description "manage Jeeves file stores" def before_actions @jeeves_config = @_parent.jeeves_config @logger = @_parent.logger end describe :status, 'show status of Jeeves file stores' help :status, 'display basic information about the file stores', 'currently available for Jeeves.' def status parts = Array.new devices = Array.new @jeeves_config[:add_partition].each_pair do |path, params| part = Jeeves::Partition.new(path, params) if part.ready? then if devices.include?(part.device) then part.duplicate = true else devices << part.device end end parts << part.to_a end Jeeves.tabulate(%w{* 4c 4c 4c 8r 8r}, %w{Path Mnt Rdy Dup Total Free}, parts ) end describe :list, 'list pre-allocated files in the Jeeves store' help :list, 'list all of the pre-allocated files', 'that Jeeves currently knows about. These may', 'include files that were not released and those', 'that have not been recorded yet.' def list jestore = Jeeves::Store.new(@logger, @jeeves_config) if jestore.files <= 0 then puts "There are no files to show" else entries = Array.new colours = Array.new compare_time = Time.now jestore.each_file do |path, params| entries << [path, Jeeves.human_size(params[:space]), params[:date].strftime("%Y-%b-%d %H:%M")] if params[:date] < compare_time then if FileTest.exists?(path) then colours << :yellow else colours << :red end else colours << :green end end Jeeves.tabulate(%w{* *r *c}, %w{Path Space Date}, entries, colours) end end describe :clean, 'clean old records from the list of files' help :clean, 'deletes any records in the Jeeves file store', 'for files whose allocation date has now expired' def clean jestore = Jeeves::Store.new(@logger, @jeeves_config) if get_option :delete then # going to get rid of all file lists if ask('Delete all file lists?', :no, 'yn') == :yes then jestore.each_partition do |part| if FileTest.exists?(part.file_list) then puts "Deleting #{part.file_list}" FileUtils.rm_f(part.file_list) end end else puts "OK, no files have been deleted" end return end # jeeves store clear if jestore.files == 0 then puts "The file list is empty, nothing to clean".yellow return end if get_option :clean_all then if ask('Clean all files from the list?', :no, 'yn') != :no then jestore.clean(true) puts "The file list is now empty".green end else if jestore.stale_files == 0 then puts "There are no files to clean!".yellow return end if ask('Clean files from the list?', :no, 'yn') != :no then jestore.clean puts "Cleaned file list".green end end end describe :init, 'initialise a partition for use by Jeeves' help :init, 'This will set up a partition to be used by Jeeves' def init new_store = next_argument_or_error("need to provide a path to initialise") unless FileTest.exists?(new_store) exit_on_error "path #{new_store} does not exist" end part = Jeeves::Partition.new(new_store) if get_option :delete then part.delete_key return end force = get_option :force_init puts "Forcing a new key!".red.bold if force key = part.create_key(force) puts "" puts "# add this line to your config file" puts "add_store :path=>'#{part.path}', :key=>'#{key}'" rescue Jeeves::InvalidPartitionKey => err exit_on_error err end end