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
# File 'lib/cli/runner.rb', line 15

def initialize(args)
  @args = args

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

  @configs  = Configs.new
  @commands = @configs.commands
  @suites   = Suites.new
  @uuid     = UUIDTools::UUID.random_create
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

#suitesObject

Returns the value of attribute suites.



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

def suites
  @suites
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



98
99
100
101
# File 'lib/cli/runner.rb', line 98

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

#get_command_handler(command, args) ⇒ Object



103
104
105
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
# File 'lib/cli/runner.rb', line 103

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



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

def parse_global_options
  @options = {}
  opts = @option_parser

  opts.on("-v", "--version", "Show version") do
    @options[:version] = true
    say("tac %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



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

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



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

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



74
75
76
# File 'lib/cli/runner.rb', line 74

def usage
  @option_parser.to_s
end