Class: Legion::CLI::MonitorCommand

Inherits:
Thor
  • Object
show all
Defined in:
lib/legion/cli/knowledge_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/legion/cli/knowledge_command.rb', line 9

def self.exit_on_failure?
  true
end

Instance Method Details

#add(path) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/legion/cli/knowledge_command.rb', line 19

def add(path)
  out = formatter
  exts = options[:extensions]&.split(',')&.map(&:strip)
  result = api_post('/api/knowledge/monitors', path: path, extensions: exts, label: options[:label])

  if options[:json]
    out.json(result)
  elsif result[:success]
    out.success("Monitor added: #{path}")
  else
    out.warn("Failed to add monitor: #{result[:error]}")
  end
end

#listObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/legion/cli/knowledge_command.rb', line 34

def list
  out = formatter
  monitors = api_get('/api/knowledge/monitors')

  if options[:json]
    out.json(monitors)
  elsif monitors.nil? || monitors.empty?
    out.warn('No monitors registered')
  else
    out.header('Knowledge Monitors')
    monitors.each do |m|
      label = m[:label] ? " [#{m[:label]}]" : ''
      exts  = m[:extensions]&.join(', ')
      puts "  #{m[:path]}#{label}"
      puts "    Extensions: #{exts}" if exts && !exts.empty?
    end
  end
end

#remove(identifier) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/cli/knowledge_command.rb', line 55

def remove(identifier)
  out = formatter
  result = api_delete("/api/knowledge/monitors?identifier=#{URI.encode_www_form_component(identifier)}")

  if options[:json]
    out.json(result)
  elsif result[:success]
    out.success("Monitor removed: #{identifier}")
  else
    out.warn("Failed to remove monitor: #{result[:error]}")
  end
end

#statusObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/legion/cli/knowledge_command.rb', line 69

def status
  out = formatter
  result = api_get('/api/knowledge/monitors/status')

  if options[:json]
    out.json(result)
  else
    out.header('Monitor Status')
    out.detail({
                 'Total monitors' => result[:total_monitors].to_s,
                 'Total files'    => result[:total_files].to_s
               })
  end
end