require 'flydata/command/base' require 'flydata/command/login' module Flydata module Command class Setlogdel < Base def run Flydata::Command::Login.new.run unless flydata.credentials.authenticated? # Choose target data_entry data_entry = choose_data_entry return unless data_entry # Confirm and update set_log_deletion data_entry if confirm_setting data_entry register_crontab end private def choose_data_entry data_entries = retrieve_data_entries if data_entries.size < 1 puts("There are no registered entries.") puts("You need to create a data entry with flydata setup command.") return nil end choices = data_entries.map {|e| file_attr = File.writable?(e['log_path']) ? "" : "(read only)" "#{e['display_name']}\t#{e['log_path']}\t -- Log deletion:#{boolToOnOff e['log_deletion']} #{file_attr}" } choices << "Cancel" choice = nil say('Please select your log path for setting log deletion.') say(" (About log deletion - http://docs.hapyrus.com/faq/how-log-deletion-works/)") newline choose do |menu| menu.index = :letter menu.index_suffix = ") " menu.prompt = "Select item to change log deletion setting: " menu.choices(*choices) {|item| choice = item.split(' ')[0]} end newline data_entry = data_entries.select{|e| e['display_name'] == choice}.first return nil unless data_entry if not data_entry['log_deletion'] and not File.writable?(data_entry['log_path']) say("Failed to change setting...") say(" This path is readonly for current user.") say(" Change user or permission, if you want to set log deletion option.") return nil end data_entry end def confirm_setting(data_entry) cur_flag = data_entry['log_deletion'] loop do say "Target log path: #{data_entry['log_path']}" say "Now changing log deletion setting: #{boolToOnOff cur_flag} -> #{boolToOnOff !cur_flag}" ans = ask "Are you sure? (yes/no): " if ans.size > 0 case ans[0].downcase when 'y'; return true when 'n'; return false end end say " ! Please answer y[es] or n[o]" newline end end def boolToOnOff(bool) bool ? 'ON' : 'OFF' end def set_log_deletion(data_entry) param = { :data_entry => { 'log_deletion' => !data_entry['log_deletion'] } } url = "/data_entries/#{data_entry['id']}" ret = flydata.put(url, param) raise 'Failed to update the log deletion setting.' unless ret['success'] puts "Update succeeded!" end end end end