Class: Jiragit::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/jiragit/cli.rb

Constant Summary collapse

COMMANDS =
[
  :install,
  :uninstall,
  :jira,
  :branch,
  :jira_branch,
  :browse,
  :remote,
  :local,
  :configure,
  :configuration
]
FLAGS =
[
  :silent
]

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Cli

Returns a new instance of Cli.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jiragit/cli.rb', line 22

def initialize(args)
  exit_with_help and return if args.empty?
  @args = args
  command = args[0].to_sym
  @params = args[1..-1]
  exit_with_help and return unless COMMANDS.include?(command)

  @config = Jiragit::Configuration.new("#{`echo ~`.chomp}/.jiragit")

  begin
    self.send(command)
  rescue => error
    $stderr.puts error.message
  end
end

Instance Method Details

#branchObject



75
76
77
78
79
80
# File 'lib/jiragit/cli.rb', line 75

def branch
  branch = @params[0] || Jiragit::Git.current_branch
  puts "Listing all relations for branch #{branch}"
  js = JiraStore.new("#{Jiragit::Git.repository_root}/.git/jiragit/jira_store")
  puts js.relations(branch: branch).to_a
end

#browseObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/jiragit/cli.rb', line 88

def browse
  type = :branch
  type, object = classify(*@params) unless @params.empty?
  unless object
    object = case type
    when :jira
      related(:jira, {branch: Jiragit::Git.current_branch}).first
    when :branch
      Jiragit::Git.current_branch
    when :commit
      related(:commit, {branch: Jiragit::Git.current_branch}).first
    end
  end
  self.send("browse_#{type}", object)
end

#configurationObject



131
132
133
134
135
136
# File 'lib/jiragit/cli.rb', line 131

def configuration
  puts "Configuration settings"
  @config.each do |key,value|
    puts "#{key} = #{value}"
  end
end

#configureObject



122
123
124
125
126
127
128
129
# File 'lib/jiragit/cli.rb', line 122

def configure
  if @params.size!=2 || @params.first.empty? || @params.last.empty?
    warn "Please provide parameter and value"
    return
  end
  @config[@params.first.to_sym] = @params.last
  puts "Setting #{@params.first.to_sym} to #{@params.last}"
end

#exit_with_helpObject



38
39
40
41
42
43
44
45
46
# File 'lib/jiragit/cli.rb', line 38

def exit_with_help
  puts "jira {command}"
  puts
  puts "Commands"
  puts "--------"
  COMMANDS.each do |command|
    puts command
  end
end

#installObject



48
49
50
51
52
53
54
55
56
# File 'lib/jiragit/cli.rb', line 48

def install
  puts "Installing into #{Jiragit::Git.repository_root}" unless silent?
  gem_hook_paths.each do |hook|
    `cp #{hook} #{Jiragit::Git.repository_root}/.git/hooks/`
  end
  gem_hook_files.each do |hook|
    `chmod a+x #{Jiragit::Git.repository_root}/.git/hooks/#{hook}`
  end
end

#jiraObject



65
66
67
68
69
70
71
72
73
# File 'lib/jiragit/cli.rb', line 65

def jira
  if @params.empty? || @params[0].empty?
    warn "Please provide a jira"
    return
  end
  puts "Listing all relations for jira #{@params[0]}"
  js = JiraStore.new("#{Jiragit::Git.repository_root}/.git/jiragit/jira_store")
  puts js.relations(jira: @params[0]).to_a
end

#jira_branchObject



82
83
84
85
86
# File 'lib/jiragit/cli.rb', line 82

def jira_branch
  puts "Relating jira #{@params[0]} and branch #{@params[1]}"
  js = JiraStore.new("#{Jiragit::Git.repository_root}/.git/jiragit/jira_store")
  js.relate(jira: @params[0], branch: @params[1])
end

#localObject



113
114
115
116
117
118
119
120
# File 'lib/jiragit/cli.rb', line 113

def local
  # local branches
  type = (@params[0] || 'branches').to_sym
  case type
  when :branches
    list_local_branches
  end
end

#remoteObject



104
105
106
107
108
109
110
111
# File 'lib/jiragit/cli.rb', line 104

def remote
  # remote branches
  type = (@params[0] || 'branches').to_sym
  case type
  when :branches
    list_remote_branches
  end
end

#uninstallObject



58
59
60
61
62
63
# File 'lib/jiragit/cli.rb', line 58

def uninstall
  puts "Uninstalling from #{Jiragit::Git.repository_root}"
  gem_hook_files.each do |hook|
    `rm #{Jiragit::Git.repository_root}/.git/hooks/#{hook}`
  end
end