Sha256: de66d72a8dfd6128a115f7bbfa06c96a6615f56dab4969713652d5b9f946b439

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module UppityRobot
  module CLI
    module Commands
      module Monitors
        # UppityRobot::CLI::Commands::Monitors::List lists monitors
        class List < Dry::CLI::Command
          desc "List Monitors"

          option :csv, type: :string, desc: "Path to additionally save results as csv"
          option :filter, type: :string, default: "{}", desc: "Filter string for monitors"
          option :search, type: :string, desc: "keyword within url or friendly_name"

          example [
            "--search aspace",
            '--filter \'{"friendly_name": "^aspace-"}\'',
            '--search aspace --filter \'{"status": 0}\''
          ]

          def call(csv: nil, filter: "{}", search: nil, **)
            filter = JSON.parse(filter)
            filtered = {stat: "ok", total: 0, monitors: []}
            params = search ? {search: search} : {}
            total = 0

            UppityRobot::Client.new(:getMonitors, params).filter(filter).each do |m|
              filtered[:monitors] << m
              total += 1
            end

            filtered[:total] = total
            write_csv(csv, filtered[:monitors])
            puts filtered.to_json
          rescue JSON::ParserError => e
            puts JSON.generate({stat: "fail", error: "JSON parser #{e.message}"})
          end

          def write_csv(file, data)
            return unless file

            CSV.open(File.expand_path(file), "wb", encoding: "UTF-8") do |csv|
              csv << data.first.keys
              data.each { |hash| csv << hash.values }
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
uppityrobot-0.4.1 lib/uppityrobot/cli/commands/monitors/list.rb