Class: BetweenMeals::Cmd

Inherits:
Object
  • Object
show all
Defined in:
lib/between_meals/cmd.rb

Direct Known Subclasses

Repo::Git::Cmd, Repo::Hg::Cmd, Repo::Svn::Cmd

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Cmd

Returns a new instance of Cmd.



23
24
25
26
27
# File 'lib/between_meals/cmd.rb', line 23

def initialize(params)
  @bin = params[:bin] || fail
  @cwd = params[:cwd] || Dir.pwd
  @logger = params[:logger] || Logger.new(STDOUT)
end

Instance Attribute Details

#binObject

Returns the value of attribute bin.



21
22
23
# File 'lib/between_meals/cmd.rb', line 21

def bin
  @bin
end

Instance Method Details

#cmd(params, cwd = nil, nofail = false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/between_meals/cmd.rb', line 29

def cmd(params, cwd = nil, nofail = false)
  cwd ||= File.expand_path(@cwd)
  cmd = "#{@bin} #{params}"
  @logger.info("Running \"#{cmd}\"")
  c = Mixlib::ShellOut.new(
    cmd,
    :cwd => cwd,
    :env => {
      # macOS needs /usr/local/bin as hg cannot be installed in /bin or
      # /usr/bin
      'PATH' => '/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin',
    },
  )
  c.run_command
  # If the user asked us not to fail, let them handle error reporting
  if c.error? && !nofail
    # Let's make sure the error goes to the logs
    @logger.error("#{@bin} failed: #{c.format_for_exception}")
    # if our logger is STDOUT, we'll double log when we throw
    # the exception, but that's OK
    c.error!
  end
  c
end