Module: Rtt::CmdLineParser

Included in:
Rtt
Defined in:
lib/rtt/cmd_line_parser.rb

Constant Summary collapse

COMMAND_MAPPING =
{
 :project => SetProjectCommand,
 :client => SetClientCommand,
 :report => ReportCommand,
 :stop => StopCommand,
 :start => StartCommand,
 :rename => RenameCommand,
 :list => QueryCommand,
 :pause => PauseCommand,
 :resume => StartCommand,
 :user => SetUserCommand,
 :delete => DeleteCommand,
 :configure => ConfigureCommand,
 :export => ExportCommand,
 :import => ImportCommand
}

Instance Method Summary collapse

Instance Method Details

#capture(arguments) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rtt/cmd_line_parser.rb', line 72

def capture(arguments)
  unless arguments.length == 0
    operation = arguments.shift.to_sym
    if COMMAND_MAPPING.keys.include?(operation)
      klazz = COMMAND_MAPPING[operation]
      if arguments.length >= klazz::NUMBER_OF_PARAM_REQUIRED
        command = klazz.new
        first_argument = arguments.shift
        if /^--.*$/.match(first_argument)
          command.name = nil
          command.optional = [first_argument]
        else
          command.name = first_argument
          command.optional = arguments if arguments.present?
        end
        Array(command)
      end
    elsif operation.present?
      command = StartCommand.new
      command.name = operation
      command.optional = arguments if arguments.present?
      Array(command)
    end
  else
    puts_usage
  end
end

#env_filtersObject



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rtt/cmd_line_parser.rb', line 100

def env_filters
  [ 'date', 'nickname', 'from', 'to', 'client', 'project' ].inject({}) do |filters, key|
    if env_variable(key).present?
      value = if key == 'date' || key == 'from' || key == 'to'
        Date.parse(env_variable(key))
      else
        env_variable(key)
      end
      filters[key.to_sym] = value
    end
    filters
  end
end

#env_variable(key) ⇒ Object



114
115
116
# File 'lib/rtt/cmd_line_parser.rb', line 114

def env_variable(key)
  ENV[key] || ENV[key.upcase] || ENV[key.downcase]
end

#execute(cmds) ⇒ Object



118
119
120
121
122
123
# File 'lib/rtt/cmd_line_parser.rb', line 118

def execute(cmds)
  cmds.each { |cmd| execute_single(cmd) }
  say "Operation(s) succeded."
rescue => e
  handle_error(e)
end

#execute_single(cmd) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rtt/cmd_line_parser.rb', line 125

def execute_single(cmd)
  case cmd
    when SetProjectCommand
      client = cmd.optional.shift if cmd.optional.present? && (/^--.*$/.match(cmd.optional.first)).blank?
      set_project(cmd.name, client)
    when SetClientCommand
      set_client(cmd.name)
    when StartCommand
      start(cmd.name)
    when RenameCommand
      rename(cmd.name)
    when PauseCommand
      if current_task.present?
          pause
      else
        raise TaskNotStartedError
      end
    when StopCommand
      stop
    when ReportCommand
      options = env_filters.merge!(:pdf => cmd.name)
      report(options)
    when QueryCommand
      list(env_filters)
    when SetUserCommand
      set_user(cmd.name)
    when ImportCommand
      import(cmd.name)
    when ExportCommand
      export(cmd.name)
    when DeleteCommand
      name = cmd.name
      options = name.present? ? env_filters.merge!(:name => name) : env_filters
      delete(options)
    when ConfigureCommand
      case cmd.name.downcase
        when 'task'
          name = cmd.next_optional
          update_task(name, env_filters)
        when 'project'
          name = cmd.next_optional
          client = cmd.next_optional
          set_project(name, client, true)
        when 'client'
          name = cmd.next_optional
          set_client(name, true)
        when 'user'
          name = cmd.next_optional
          set_user(name, true)
        else
          raise CommandNotFoundError
      end
    else
      raise CommandNotFoundError
  end
end

#handle_error(e) ⇒ Object



182
183
184
185
186
187
188
189
# File 'lib/rtt/cmd_line_parser.rb', line 182

def handle_error(e)
  case e
    when CommandNotFoundError
      return puts_usage
    when TaskNotStartedError
      say "There is no active task. Pause is not valid at this point."
  end
end

#puts_usageObject



191
192
193
194
195
196
197
198
199
# File 'lib/rtt/cmd_line_parser.rb', line 191

def puts_usage
  say('')
  File.open(File.join( File.dirname(__FILE__), '..', '..', "USAGE.txt")) do |file|
    while content = file.gets
      say content
    end
  end
  say('')
end