Class: Toolshed::Commands::CreateBranch

Inherits:
Object
  • Object
show all
Defined in:
lib/toolshed/commands/create_branch.rb

Overview

Create a branch from a given set of parameters

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cli_optionsObject

rubocop:disable Metrics/MethodLength



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/toolshed/commands/create_branch.rb', line 7

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

Instance Method Details

#execute(_args, options = {}) ⇒ Object

rubocop:disable Metrics/MethodLength



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/toolshed/commands/create_branch.rb', line 21

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)

  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

#read_user_input_branch_from(message, options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/toolshed/commands/create_branch.rb', line 51

def read_user_input_branch_from(message, options)
  return options[:branch_from] if options.key?(:branch_from)

  # if branch-name was supplied then default to master if not supplied
  return Toolshed::Git::DEFAULT_BRANCH_FROM if options.key?(:branch_name)

  puts message
  value = $stdin.gets.chomp

  while value.empty?
    puts 'Branch from cannot be empty'
    puts message
    value = $stdin.gets.chomp
  end

  value
end

#read_user_input_branch_name(message, options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/toolshed/commands/create_branch.rb', line 36

def read_user_input_branch_name(message, options)
  return options[:branch_name] if options.key?(:branch_name)

  puts message
  value = $stdin.gets.chomp

  while value.empty?
    puts 'Branch name cannot be empty'
    puts message
    value = $stdin.gets.chomp
  end

  value
end