Class: Flydata::Cli

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/flydata/cli.rb

Instance Method Summary collapse

Methods included from Helpers

development?, env_mode, env_suffix, flydata_api_host_file, flydata_conf_file, format_menu_list, parse_command, retry_on, to_command_class, usage_text

Constructor Details

#initialize(args) ⇒ Cli

Returns a new instance of Cli.



6
7
8
# File 'lib/flydata/cli.rb', line 6

def initialize(args)
  @args = args
end

Instance Method Details

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/flydata/cli.rb', line 10

def run
  unless check_environment
    puts "Sorry, you have to run the installation command to use flydata. Please go to https://console.flydata.com/ and sign up."
    return
  end
  begin
    if @args.size > 0
      first_arg = @args.shift
      cmd, sub_cmd = parse_command(first_arg)
      begin
        cmd_cls = "Flydata::Command::#{cmd.capitalize}".constantize
      rescue
        raise "Command not found: #{cmd}"
      end
      # Command class can define options for each subcommand by defining method "slop_subcommandname"
      slop_method = sub_cmd ? "slop_#{sub_cmd}".to_sym : :slop
      options = cmd_cls.respond_to?(slop_method) ? cmd_cls.send(slop_method) : Slop.new(strict: true)
      options.parse!(@args)
      cmd_obj = cmd_cls.new(options)
      sub_cmd ? cmd_obj.send(sub_cmd,*@args) : cmd_obj.run(*@args)
    else
      raise 'no command given'
    end
  rescue => e
    #raise e
    $stderr.puts "! #{e.to_s}"
    $stderr.puts
    $stderr.puts
    $stderr.puts usage_text
    raise e if FLYDATA_DEBUG
    exit 1
  end
end