# encoding: UTF-8 # frozen_string_literal: true require 'nrser/refinements/types' using NRSER::Types # Namespace # ======================================================================= module Locd module CLI module Command # Definitions # ======================================================================= class Agent < Base desc 'truncate-logs', "Truncate agent log file(s)." include_shared t[ groups: t.has_any( :pattern, :multi ) ] option :restart, desc: "Restart the agent after truncation", type: :boolean, default: true def truncate_logs find_multi!( pattern ).each do |agent| log_paths = [agent.out_path, agent.err_path].compact.uniq unless log_paths.empty? restart = options[:restart] && agent.running? agent.stop if restart log_paths.each do |log_path| begin log_path.open( 'w' ) { |f| f.truncate 0 } rescue Exception => error logger.error "Failed to truncate #{ log_path }", error else logger.info "Truncated", 'file' => log_path.to_s, 'agent.label' => agent.label end end # each log_path agent.start if restart end # unless log_paths.empty? end # each agent end # #truncate_logs end # /Namespace # ======================================================================= end # module Command end # module CLI end # module Locd