lib/toolshed/commands/create_branch.rb in toolshed-1.0.2 vs lib/toolshed/commands/create_branch.rb in toolshed-1.0.3
- old
+ new
@@ -1,60 +1,65 @@
+require 'toolshed/git'
+
module Toolshed
module Commands
+ # Create a branch from a given set of parameters
class CreateBranch
- def execute(args, options = {})
- begin
- branch_name = read_user_input_branch_name("Branch name:", options)
- branch_from = read_user_input_branch_from("Branch from:", options)
+ def self.cli_options # rubocop:disable Metrics/MethodLength
+ {
+ banner: 'Usage: create_branch [options]',
+ options: {
+ branch_name: {
+ short_on: '-b'
+ },
+ branch_from: {
+ short_on: '-f'
+ }
+ }
+ }
+ end
- puts "Branch name: #{branch_name}"
- puts "Branching from: #{branch_from}"
+ def execute(_args, options = {}) # rubocop:disable Metrics/MethodLength
+ branch_name = read_user_input_branch_name('Branch name:', options)
+ branch_from = read_user_input_branch_from('Branch from:', options)
- git = Toolshed::Git::Base.new({
- from_remote_branch_name: branch_from,
- to_remote_branch_name: branch_name,
- })
- git.create_branch
-
- puts "Branch #{branch_name} has been created"
- return
- rescue Veto::InvalidEntity => e
- puts "Unable to create branch due to the following errors"
- e.message.each do |key, value|
- puts "#{key}: #{value}"
- end
- return
+ branch = Toolshed::Git::Branch.new(from_remote_branch_name: branch_from, to_remote_branch_name: branch_name) # rubocop:disable Metrics/LineLength
+ branch.create
+ Toolshed.die
+ rescue Veto::InvalidEntity => e
+ Toolshed.logger.fatal 'Unable to create branch due to the following errors' # rubocop:disable Metrics/LineLength
+ e.message.each do |key, value|
+ Toolshed.logger.fatal "#{key}: #{value}"
end
+ Toolshed.die
end
def read_user_input_branch_name(message, options)
- return options[:branch_name] if (options.has_key?(:branch_name))
+ return options[:branch_name] if options.key?(:branch_name)
puts message
value = $stdin.gets.chomp
- until (!value.empty?)
- puts "Branch name cannot be empty"
+ while value.empty?
+ puts 'Branch name cannot be empty'
puts message
value = $stdin.gets.chomp
end
value
end
def read_user_input_branch_from(message, options)
- return options[:branch_from] if (options.has_key?(:branch_from))
+ return options[:branch_from] if options.key?(:branch_from)
# if branch-name was supplied then default to master if not supplied
- if (options.has_key?(:branch_name))
- return Toolshed::Git::DEFAULT_BRANCH_FROM
- end
+ return Toolshed::Git::DEFAULT_BRANCH_FROM if options.key?(:branch_name)
puts message
value = $stdin.gets.chomp
- until (!value.empty?)
- puts "Branch from cannot be empty"
+ while value.empty?
+ puts 'Branch from cannot be empty'
puts message
value = $stdin.gets.chomp
end
value