class SystemdServiceCheck::CLI

CLI

Constants

COLS

rubocop:disable Metrics/LineLength

Public Instance Methods

check(*envs) click to toggle source
# File lib/systemd_service_check/cli.rb, line 35
def check(*envs) # rubocop:disable Metrics/AbcSize:
  raise InvalidFormatOptionError unless format_option_validate

  @ssc = Base.new(envs, options[:yaml], options[:role])
  @ssc.run
  disp
rescue InvalidFormatOptionError => e
  puts "<#{e}>",
       "  [#{options[:format]}] is invalid value."
  puts "  #{e.backtrace_locations.first}"
end
version() click to toggle source
# File lib/systemd_service_check/cli.rb, line 49
def version
  puts VERSION
end

Private Instance Methods

color_state(obj, method, arg) click to toggle source
# File lib/systemd_service_check/cli.rb, line 113
def color_state(obj, method, arg)
  green_or_red = obj.send(method, arg) ? :green : :red
  set_color(obj, green_or_red)
end
decorate_ansi_color(services) click to toggle source
# File lib/systemd_service_check/cli.rb, line 100
def decorate_ansi_color(services)
  services.map do |s|
    s.class.new(
      s.service_name,
      color_state(s.load_state,      :==, "loaded"),
      color_state(s.active_state,    :==, "active"),
      color_state(s.sub_state,       :!=, "dead"),
      color_state(s.unit_file_state, :==, "enabled"),
      s.type
    )
  end
end
disp() click to toggle source
# File lib/systemd_service_check/cli.rb, line 59
def disp
  case options[:format]
  when 't', 'table'         then disp_table
  when 'j', 'json'          then disp_json
  when 'a', 'awesome_print' then disp_ap
  end
end
disp_ap() click to toggle source
# File lib/systemd_service_check/cli.rb, line 71
def disp_ap
  ap @ssc.results
end
disp_json() click to toggle source
# File lib/systemd_service_check/cli.rb, line 67
def disp_json
  puts @ssc.to_json
end
disp_table() click to toggle source

rubocop:enable Metrics/LineLength

# File lib/systemd_service_check/cli.rb, line 79
def disp_table # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  service_name_width =
    @ssc.results.map(&:services).flatten.map(&:service_name).map(&:size).max

  @ssc.results.each do |result|
    services = decorate_ansi_color(result.services)
    data = services.map { |s| result.server.to_h.merge(s.to_h) }

    tp(data, COLS, service_name: { width: service_name_width })
    puts
  end

  # ref: https://github.com/erikhuda/thor/blob/master/lib/thor/shell.rb#L14
  # rubocop:disable Style/GuardClause
  if RbConfig::CONFIG["host_os"] =~ /mswin|mingw/ && !ENV["ANSICON"]
    puts "-- For colors on windows, please setup `ANSICON`.",
         "-- ANSICON: https://github.com/adoxa/ansicon"
  end
  # rubocop:enable Style/GuardClause
end
format_option_validate() click to toggle source
# File lib/systemd_service_check/cli.rb, line 55
def format_option_validate
  %w[t table j json a awesome_print].include? options[:format]
end