Class: AllInRuby::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/all_in_ruby/cli.rb

Constant Summary collapse

COMMANDS =
%w[install uninstall status].freeze
STATUS_LABELS =
{
  created: "created",
  updated: "instructions added",
  already_installed: "already installed, left untouched",
  removed: "instructions removed",
  not_installed: "not installed",
  installed: "installed",
  manual: "no global rules file; use Cursor Settings > Rules"
}.freeze
DRY_RUN_LABELS =
STATUS_LABELS.merge(
  created: "would be created",
  updated: "instructions would be added",
  already_installed: "would already be covered, no change",
  removed: "instructions would be removed",
  not_installed: "not installed, nothing to remove"
).freeze

Instance Method Summary collapse

Constructor Details

#initialize(stdout: $stdout, stderr: $stderr, installer: Installer.new) ⇒ CLI

Returns a new instance of CLI.



27
28
29
30
31
# File 'lib/all_in_ruby/cli.rb', line 27

def initialize(stdout: $stdout, stderr: $stderr, installer: Installer.new)
  @stdout = stdout
  @stderr = stderr
  @installer = installer
end

Instance Method Details

#run(argv) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/all_in_ruby/cli.rb', line 33

def run(argv)
  options = { agents: [], scopes: [], dry_run: false, backup: true }
  parser = build_parser(options)
  parser.parse!(argv)

  command = argv.shift
  unless COMMANDS.include?(command)
    @stderr.puts parser
    return 1
  end

  agents = options[:agents].empty? ? Installer::AGENTS : options[:agents]
  scopes = scopes_for(command, options[:scopes])
  dry_run = options[:dry_run] && command != "status"

  scope_results = results_by_scope(command, agents:, scopes:, dry_run:, backup: options[:backup])
  print_statuses(scope_results, dry_run ? DRY_RUN_LABELS : STATUS_LABELS)

  results = scope_results.flat_map { |_scope, scope_result| scope_result }
  print_diffs(results)
  print_backups(results, dry_run)
  print_manual_instruction(command, results)
  0
rescue OptionParser::ParseError => e
  @stderr.puts e.message
  1
end