Class: Fp::CLI

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

Overview

Main CLI entry point - parses global flags and dispatches to commands

Constant Summary collapse

COMMANDS =
{
  'projects' => Commands::Projects,
  'surfaces' => Commands::Surfaces,
  'list' => Commands::List,
  'show' => Commands::Show,
  'propose' => Commands::Propose,
  'report' => Commands::Report,
  'matrix' => Commands::Matrix,
  'profile' => Commands::Profile,
  'config' => Commands::ConfigCmd,
  'setup' => Commands::Setup,
  'version' => Commands::Version,
  'help' => Commands::Help
}.freeze

Class Method Summary collapse

Class Method Details

.run(argv) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
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
60
61
# File 'lib/fp/cli.rb', line 22

def run(argv)
  args = argv.dup
  global_opts = extract_global_options(args)

  command_name = args.shift || 'help'
  command_class = COMMANDS[command_name]

  unless command_class
    output = Output.new(json: global_opts[:json])
    output.error("Unknown command: #{command_name}")
    puts "\nRun 'fp help' for usage."
    exit 1
  end

  # Profile/config commands don't need auth
  needs_auth = !%w[profile config setup version help].include?(command_name)

  if needs_auth
    config = Config.new(profile: global_opts[:profile], api_url: global_opts[:api_url])
    unless config.valid?
      output = Output.new(json: global_opts[:json])
      output.error(config.validation_error.strip)
      exit 1
    end
    client = Client.new(config)
  else
    config = Config.new(profile: global_opts[:profile], api_url: global_opts[:api_url])
  end

  output = Output.new(json: global_opts[:json])
  command = command_class.new(client: client, output: output, config: config)
  command.run(args)
rescue Interrupt
  warn "\nAborted."
  exit 130
rescue StandardError => e
  output = Output.new(json: global_opts&.dig(:json))
  output.error(e.message)
  exit 1
end