Class: HeapScope::CLI

Inherits:
Object
  • Object
show all
Includes:
Commands::Capture, Commands::Diffing, Commands::Meta, Commands::Reporting, Support
Defined in:
lib/heapscope/cli.rb,
lib/heapscope/cli/help.rb,
lib/heapscope/cli/color.rb,
lib/heapscope/cli/support.rb,
lib/heapscope/cli/completion.rb,
lib/heapscope/cli/commands/meta.rb,
lib/heapscope/cli/commands/capture.rb,
lib/heapscope/cli/commands/diffing.rb,
lib/heapscope/cli/commands/reporting.rb

Overview

Polished, expansive CLI for HeapScope diagnostics (local-only).

Defined Under Namespace

Modules: Color, Commands, Completion, Help, Support

Constant Summary collapse

EXIT_OK =
0
EXIT_REGRESSION =
1
EXIT_INVALID =
2
EXIT_CAPABILITY =
3
COMMAND_MAP =
{
  "snapshot" => :cmd_snapshot,
  "inspect" => :cmd_inspect,
  "diff" => :cmd_diff,
  "monitor" => :cmd_monitor,
  "watch" => :cmd_watch,
  "trends" => :cmd_trends,
  "report" => :cmd_report,
  "compare" => :cmd_compare,
  "baseline" => :cmd_baseline,
  "sessions" => :cmd_sessions,
  "history" => :cmd_history,
  "validate" => :cmd_validate,
  "suggest" => :cmd_suggest,
  "ignore-suggest" => :cmd_ignore_suggest,
  "pack" => :cmd_pack,
  "export" => :cmd_export,
  "probe" => :cmd_probe,
  "measure" => :cmd_measure,
  "retention" => :cmd_retention,
  "findings" => :cmd_findings,
  "scorecard" => :cmd_scorecard,
  "table" => :cmd_table,
  "explain" => :cmd_explain,
  "open" => :cmd_open,
  "html" => :cmd_html,
  "self-test" => :cmd_self_test,
  "env" => :cmd_env,
  "completion" => :cmd_completion,
  "man" => :cmd_man,
  "codes" => :cmd_codes,
  "doctor" => :cmd_doctor,
  "overhead" => :cmd_overhead,
  "config" => :cmd_config,
  "about" => :cmd_about,
  "capabilities" => :cmd_capabilities
}.freeze

Constants included from Support

Support::SEVERITY_RANK

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



72
73
74
75
# File 'lib/heapscope/cli.rb', line 72

def initialize
  @json_mode = false
  @color_enabled = Color.enabled?
end

Class Method Details

.run(argv = ARGV) ⇒ Object



68
69
70
# File 'lib/heapscope/cli.rb', line 68

def self.run(argv = ARGV)
  new.run(argv)
end

Instance Method Details

#run(argv) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/heapscope/cli.rb', line 77

def run(argv)
  argv = argv.map(&:to_s)
  parse_globals!(argv)

  command = argv.shift
  case command
  when "version", "-v", "--version"
    puts "heapscope #{VERSION}"
    EXIT_OK
  when "help", "-h", "--help", nil
    topic = argv.shift
    if topic && Help::COMMANDS.key?(topic)
      puts Help.command_help(topic)
      EXIT_OK
    else
      print_help
      command.nil? ? EXIT_INVALID : EXIT_OK
    end
  else
    method = COMMAND_MAP[command]
    unless method
      say_err Color.err("Unknown command: #{command}", enabled: color?)
      print_help
      return EXIT_INVALID
    end
    send(method, argv)
  end
rescue CapabilityError => e
  say_err "Capability unavailable: #{e.message}"
  EXIT_CAPABILITY
rescue InvalidReportError, SnapshotError, ConfigurationError, ArgumentError => e
  say_err "Invalid input: #{e.message}"
  EXIT_INVALID
rescue BudgetExceededError => e
  say_err e.message
  EXIT_REGRESSION
end