Class: Legion::TTY::Background::Scanner
- Inherits:
-
Object
- Object
- Legion::TTY::Background::Scanner
- Defined in:
- lib/legion/tty/background/scanner.rb
Overview
rubocop:disable Metrics/ClassLength
Constant Summary collapse
- MAX_DEPTH =
3- SERVICES =
{ rabbitmq: 5672, redis: 6379, memcached: 11_211, vault: 8200, postgres: 5432 }.freeze
- CONFIG_FILES =
%w[.env Gemfile package.json Dockerfile].freeze
- LANGUAGE_MAP =
{ '.rb' => 'Ruby', '.py' => 'Python', '.js' => 'JavaScript', '.ts' => 'TypeScript', '.go' => 'Go', '.java' => 'Java', '.rs' => 'Rust', '.tf' => 'Terraform', '.sh' => 'Shell' }.freeze
Instance Method Summary collapse
-
#initialize(base_dirs: nil, logger: nil) ⇒ Scanner
constructor
A new instance of Scanner.
- #run_async(queue) ⇒ Object
- #scan_all ⇒ Object
- #scan_config_files ⇒ Object
- #scan_dotfiles ⇒ Object
- #scan_git_repos ⇒ Object
- #scan_services ⇒ Object
- #scan_shell_history ⇒ Object
Constructor Details
#initialize(base_dirs: nil, logger: nil) ⇒ Scanner
Returns a new instance of Scanner.
35 36 37 38 |
# File 'lib/legion/tty/background/scanner.rb', line 35 def initialize(base_dirs: nil, logger: nil) @base_dirs = base_dirs || [File.('~')] @log = logger end |
Instance Method Details
#run_async(queue) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/legion/tty/background/scanner.rb', line 74 def run_async(queue) Thread.new do @log&.log('scanner', "starting scan of #{@base_dirs.join(', ')}") t0 = Time.now data = scan_all elapsed = ((Time.now - t0) * 1000).round @log&.log('scanner', "scan complete in #{elapsed}ms") queue.push({ type: :scan_complete, data: data }) rescue StandardError => e @log&.log('scanner', "ERROR: #{e.class}: #{e.}") queue.push({ type: :scan_error, error: e. }) end end |
#scan_all ⇒ Object
61 62 63 64 |
# File 'lib/legion/tty/background/scanner.rb', line 61 def scan_all { services: scan_services, repos: scan_git_repos, tools: scan_shell_history, configs: scan_config_files, dotfiles: scan_dotfiles } end |
#scan_config_files ⇒ Object
55 56 57 58 59 |
# File 'lib/legion/tty/background/scanner.rb', line 55 def scan_config_files @base_dirs.flat_map do |base| CONFIG_FILES.map { |name| File.join(base, name) }.select { |p| File.exist?(p) } end end |
#scan_dotfiles ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/legion/tty/background/scanner.rb', line 66 def scan_dotfiles { git: scan_gitconfig, jfrog: scan_jfrog, terraform: scan_terraform } end |
#scan_git_repos ⇒ Object
46 47 48 |
# File 'lib/legion/tty/background/scanner.rb', line 46 def scan_git_repos @base_dirs.flat_map { |base| collect_repos(base) } end |
#scan_services ⇒ Object
40 41 42 43 44 |
# File 'lib/legion/tty/background/scanner.rb', line 40 def scan_services SERVICES.each_with_object({}) do |(name, port), result| result[name] = { name: name.to_s, port: port, running: port_open?('127.0.0.1', port) } end end |
#scan_shell_history ⇒ Object
50 51 52 53 |
# File 'lib/legion/tty/background/scanner.rb', line 50 def scan_shell_history lines = read_history_lines tally_commands(lines).sort_by { |_, v| -v }.first(20).to_h end |