Module: SugarJar::Util

Included in:
Commands, RepoConfig
Defined in:
lib/sugarjar/util.rb

Overview

Some common methods needed by other classes

Instance Method Summary collapse

Instance Method Details

#hub(*args) ⇒ Object



34
35
36
37
38
# File 'lib/sugarjar/util.rb', line 34

def hub(*args)
  s = hub_nofail(*args)
  s.error!
  s
end

#hub_nofail(*args) ⇒ Object



29
30
31
32
# File 'lib/sugarjar/util.rb', line 29

def hub_nofail(*args)
  SugarJar::Log.trace("Running: hub #{args.join(' ')}")
  Mixlib::ShellOut.new([which('hub')] + args).run_command
end

#in_repoObject



40
41
42
43
# File 'lib/sugarjar/util.rb', line 40

def in_repo
  s = hub_nofail('rev-parse', '--is-inside-work-tree')
  !s.error? && s.stdout.strip == 'true'
end

#repo_rootObject



45
46
47
# File 'lib/sugarjar/util.rb', line 45

def repo_root
  hub('rev-parse', '--show-toplevel').stdout.strip
end

#which(cmd) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/sugarjar/util.rb', line 21

def which(cmd)
  path = which_nofail(cmd)
  return path if path

  SugarJar::Log.fatal("Could not find #{cmd} in your path")
  exit(1)
end

#which_nofail(cmd) ⇒ Object

Finds the first entry in the path for a binary and checks to make sure it’s not us (i.e. we may be linked to as ‘git’ or ‘hub’, but when we are calling that, we don’t want ourselves.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/sugarjar/util.rb', line 9

def which_nofail(cmd)
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |dir|
    p = File.join(dir, cmd)
    # if it exists, and it is executable and is not us...
    if File.exist?(p) && File.executable?(p) &&
       File.basename(File.realpath(p)) != 'sj'
      return p
    end
  end
  false
end