Sha256: 08740e39364215c7e24188f4591da56d031078a84d4b84cb895a0a93ac2cb729

Contents?: true

Size: 1.28 KB

Versions: 6

Compression:

Stored size: 1.28 KB

Contents

require 'slop'

module Flydata
  class Cli
    include Helpers
    def initialize(args)
      @args = args
    end

    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)
          cmd_cls = "Flydata::Command::#{cmd.capitalize}".constantize
          # 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
        puts "! #{e.to_s}"
        puts
        puts
        print_usage
        raise e if FLYDATA_DEBUG
      end
    end

    private
    def check_environment
      FileTest.file?(flydata_api_host_file)
    end
    def parse_command(cmd)
      cmd.split(':')
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
flydata-0.2.1 lib/flydata/cli.rb
flydata-0.2.0 lib/flydata/cli.rb
flydata-0.1.15 lib/flydata/cli.rb
flydata-0.1.13 lib/flydata/cli.rb
flydata-0.1.12 lib/flydata/cli.rb
flydata-0.1.11 lib/flydata/cli.rb