Class: CTT::Cli::Runner

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Runner

Returns a new instance of Runner.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cli/runner.rb', line 15

def initialize(args)
  @args = args

  banner = "Usage: orc [<options>] <command> [<args>]"
  @option_parser = OptionParser.new(banner)

  @configs  = Configs.new
  @commands = @configs.commands
  @suites   = Suites.new
  @uuid     = UUIDTools::UUID.random_create
  @log      = Logger.new(File.join(ENV["HOME"], ".orc/orc.log"), 'weekly')
  @url      = ENV['ORC_RESULTS_SERVER_URL'] ?
      format_url(ENV['ORC_RESULTS_SERVER_URL']) : RESULTS_SERVER_URL
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



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

def command
  @command
end

#commandsObject (readonly)

Returns the value of attribute commands.



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

def commands
  @commands
end

#configsObject

Returns the value of attribute configs.



7
8
9
# File 'lib/cli/runner.rb', line 7

def configs
  @configs
end

#logObject (readonly)

Returns the value of attribute log.



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

def log
  @log
end

#suitesObject

Returns the value of attribute suites.



7
8
9
# File 'lib/cli/runner.rb', line 7

def suites
  @suites
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

#uuidObject (readonly)

Returns the value of attribute uuid.



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

def uuid
  @uuid
end

Class Method Details

.run(args) ⇒ Object

Parameters:

  • args (Array)


11
12
13
# File 'lib/cli/runner.rb', line 11

def self.run(args)
  new(args).run
end

Instance Method Details

#execute(command, args) ⇒ Object



101
102
103
104
# File 'lib/cli/runner.rb', line 101

def execute(command, args)
  handler = get_command_handler(command, args)
  handler.run
end

#format_url(url) ⇒ Object



137
138
139
140
141
142
# File 'lib/cli/runner.rb', line 137

def format_url(url)
  unless url =~ /^http/
    url = "http://#{url}"
  end
  url
end

#get_command_handler(command, args) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cli/runner.rb', line 106

def get_command_handler(command, args)
  # handle runtime commands
  #
  @configs.suites["suites"].keys.each do |s|
    if command =~ /#{s}/
      #pieces = command.split(" ")
      #pieces.insert(0, "") if pieces.size == 1
      #action, suite = pieces
      return Command::TestSuite.new(command, args, self)
    end
  end

  # handle user defined alias
  #
  # TODO

  # handle static commands
  #
  handler =
      case command
        when "help"
          Command::Help.new(args, self)
        when "add suite", "delete suite", "suites"
          Command::SuitesConfig.new(command, args, self)
        when "tests", "rerun"
          Command::MultipleTests.new(command, args, self)
        else
          nil
      end
end

#parse_global_optionsObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cli/runner.rb', line 55

def parse_global_options
  @options = {}
  opts = @option_parser

  opts.on("-v", "--version", "Show version") do
    @options[:version] = true
    say("orc %s" % [CTT::Cli::VERSION])
    exit(0)
  end

  opts.on("-h", "--help", "Show help message") do
    @options[:help] = true
  end

  begin
    @args = @option_parser.order!(@args)
  rescue
    say("invalid command. abort!", :red)
    exit(1)
  end
end

#runObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cli/runner.rb', line 30

def run
  #puts "the args: #{@args}"
  parse_global_options

  if @args.empty? && @options.empty?
    say(usage)
    exit(0)
  end

  if @options[:help]
    Command::Help.new(@args, self).run
    exit(0)
  end

  find, command, args = search_commands
  unless find
    say("invalid command. abort!", :red)
    exit(1)
  end

  @command = command
  execute(command, args)

end

#search_commandsObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cli/runner.rb', line 81

def search_commands
  cmds = @commands.keys

  find = nil
  longest_cmd = ""
  args = []
  size = @args.size
  size.times do |index|

    longest_cmd = @args[0..(size - index - 1)].join(" ")
    find = cmds.index(longest_cmd)
    if find
      args = @args[(size - index)..-1]
      break
    end

  end
  [find, longest_cmd, args]
end

#usageObject



77
78
79
# File 'lib/cli/runner.rb', line 77

def usage
  @option_parser.to_s
end