class GitCommands::CLI

Constants

VALID_COMMANDS

Public Class Methods

new(command_name:, args: ARGV, out: STDOUT, computer_klass: Computer) click to toggle source
# File lib/git_commands/cli.rb, line 12
def initialize(command_name:, args: ARGV, out: STDOUT, computer_klass: Computer)
  @command_name = check_command_name(command_name)
  @computer_klass = computer_klass
  @args = args
  @out = out 
  @repo = nil
  @branches = nil
end

Public Instance Methods

call() click to toggle source
# File lib/git_commands/cli.rb, line 21
def call
  parser.parse!(@args)
  @origin ||= Branch::ORIGIN
  @default ||= Branch::DEFAULT
  computer = @computer_klass.new(repo: @repo, branches: @branches, origin: @origin, default: @default)
  computer.send(@command_name)
rescue Repository::PathError, Computer::GitError, AbortError, Repository::InvalidError => e
  error(e.message)  
  exit
end

Private Instance Methods

check_command_name(name) click to toggle source
# File lib/git_commands/cli.rb, line 32
        def check_command_name(name)
  return name if VALID_COMMANDS.include?(name)
  fail UnknownCommandError, "#{name} is not a supported command"
end
parser() click to toggle source
# File lib/git_commands/cli.rb, line 37
        def parser
  OptionParser.new do |opts|
    opts.banner = "Usage: #{@command_name} --repo=~/greatest_hits --origin=upstream --default=production --branches=feature/love_me_tender,fetaure/teddybear"

    opts.on("-rREPO", "--repo=REPO", "The path to the existing GIT repository") do |repo|
      @repo = File.expand_path(repo)
    end

    opts.on("-oORIGIN", "--origin=ORIGIN", "Specify the remote alias (origin)") do |origin|
      @origin = origin
    end

    opts.on("-dDEFAULT", "--default=DEFAULT", "Specify the default branch (master)") do |default|
      @default = default
    end

    opts.on("-bBRANCHES", "--branches=BRANCHES", "Specify branches as: 1. a comma-separated list of names 2. the path to a file containing names on each line 3. via pattern matching") do |branches|
      @branches = branches
    end

    opts.on("-h", "--help", "Prints this help") do
      @out.puts opts
      exit
    end
  end
end