Class: Rgit::Cli

Inherits:
Object
  • Object
show all
Includes:
Rgit
Defined in:
lib/rgit/cli.rb

Constant Summary collapse

SUB_COMMANDS =
%w(add-root remove-root show-roots checkout pull fetch status version).freeze

Constants included from Rgit

VERSION

Class Method Summary collapse

Class Method Details

.parse(args, rgit = Rgit.new(Configuration.exist? ? Configuration.load : Configuration.create)) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rgit/cli.rb', line 9

def self.parse(args, rgit = Rgit.new(Configuration.exist? ? Configuration.load : Configuration.create))
  global_opts = Trollop.options(args) do
    banner 'rgit is a utility for managing multiple git repositories'
    opt :verbose, 'Run verbosely', short: '-v'
    stop_on SUB_COMMANDS
  end

  rgit.verbose = global_opts[:verbose]

  cmd = args.shift
  case cmd
    when 'add-root'
      path = args.size == 1 ? args[0] : Dir.pwd
      rgit.add_root(path)
    when 'remove-root'
      path = args.size == 1 ? args[0] : Dir.pwd
      rgit.remove_root(path)
    when 'checkout'
      branch = args[0] if args.size == 1
      unless branch
        puts 'ERROR: checkout subcommand expects a branch name'.red
        return
      end
      rgit.checkout branch
    when 'show-root'
      rgit.print_roots
    when 'pull'
      rgit.pull
    when 'fetch'
      rgit.fetch
    when 'status'
      rgit.status
    when 'version'
      puts "rgit #{VERSION}"
    else
      puts "ERROR: unknown subcommand #{cmd}".red
      return
  end
end