lib/safe_pusher/cli.rb in safe_pusher-0.4.0 vs lib/safe_pusher/cli.rb in safe_pusher-0.4.1
- old
+ new
@@ -1,145 +1,74 @@
module SafePusher
class CLI
- SHORTCUTS = {
- 't' => 'test',
- 'l' => 'lint',
- 'p' => 'push',
- 'o' => 'open',
- 'm' => 'amend',
- 'a' => 'add',
- 'c' => 'commit',
- }.freeze
-
- private_constant :SHORTCUTS
-
def initialize(arguments:)
@arguments = arguments
end
def start
return version if arguments.first == '--version'
- unless arguments_valid?
- help
+ help if commands.include?(nil)
- return
- end
-
- arguments.each do |command|
- execute_command(command)
- end
+ commands.compact.each { |command| execute_command(command) }
end
private
attr_reader :arguments
def execute_command(command)
- if SHORTCUTS[command]
- send(SHORTCUTS[command])
- else
- send(command)
- end
- end
+ explain(command) if verbose
- def test
- puts '##########################'.yellow
- puts '## Testing new files... ##'.yellow
- puts '##########################'.yellow
- results = SafePusher::RspecRunner.new.call
+ results = SafePusher::Client
+ .const_get(services[command].capitalize)
+ .new
+ .public_send(command)
exit results unless results == 0
end
- def lint
- puts '#######################'.yellow
- puts '## Running linter... ##'.yellow
- puts '#######################'.yellow
+ def commands
+ @commands ||= arguments.map do |arg|
+ next unless arg =~ valid_commands_regexp
- results = SafePusher::ProntoRunner.new.call
-
- exit results unless results == 0
+ shortcut_to_command[arg] || arg
+ end
end
- def push
- puts '##########################'.yellow
- puts '## Pushing to Github... ##'.yellow
- puts '##########################'.yellow
-
- results = SafePusher::GithubRunner.new.push
-
- exit results unless results == 0
+ def version
+ puts SafePusher::VERSION
end
- def amend
- puts '###################################'.yellow
- puts '## Amending your last commit... ###'.yellow
- puts '###################################'.yellow
-
- results = SafePusher::GitRunner.new.amend
-
- exit results unless results == 0
+ def explain(command)
+ puts I18n.t("command.#{command}.verbose").yellow
end
- def add
- puts '######################'.yellow
- puts '## Adding files... ###'.yellow
- puts '######################'.yellow
-
- results = SafePusher::GitRunner.new.add
-
- exit results unless results == 0
+ def help
+ puts I18n.t('help')
end
- def commit
- puts '################################'.yellow
- puts '## Commiting last changes... ###'.yellow
- puts '################################'.yellow
-
- results = SafePusher::GitRunner.new.commit
-
- exit results unless results == 0
+ def valid_commands_regexp
+ @valid_commands_regexp ||= /^(?!\s*$)(?:#{available_commands})$/
end
- def open
- puts '#########################################'.yellow
- puts '## Opening a pull request on Github... ##'.yellow
- puts '#########################################'.yellow
-
- results = SafePusher::GithubRunner.new.open
-
- exit results unless results == 0
+ def available_commands
+ @available_commands ||= (
+ shortcut_to_command.keys + shortcut_to_command.values
+ ).join('|')
end
- def arguments_valid?
- arguments.join(' ') =~ valid_commands_regexp
+ def verbose
+ @verbose ||= SafePusher.configuration.verbose
end
- def valid_commands_regexp
- valid_commands = "#{SHORTCUTS.keys.join('|')}|"\
- "#{SHORTCUTS.values.join('|')}"
-
- /^(?!\s*$)(?:#{valid_commands}| )+$/
+ def services
+ @services ||= SafePusher.configuration.services
end
- def help
- puts "Usage:\n"\
- " help (h) # show this usage message\n"\
- " --version # print SafePusher version\n"\
- " ##########################################################\n"\
- " # you can use any combination of theese commands \n"\
- " ##########################################################\n"\
- " test (t) # run the test suite\n"\
- " lint (l) # run the linters\n"\
- " amend (m) # amend your last commit \n"\
- " add (a) # add changes to be committed \n"\
- " commit (c) # commit your staged changes \n"\
- " push (p) # push on distant repository\n"\
- ' open (o) # open a pull request on the distant repository'
- end
-
- def version
- puts SafePusher::VERSION
+ def shortcut_to_command
+ @shortcut_to_command ||= YAML
+ .load_file('config/commands.yml')
+ .reduce({}) { |o, (k, v)| o.update(v['shortcut'] => k) }
end
end
end