Module: SugarJar::Util

Defined in:
lib/sugarjar/util.rb

Class Method Summary collapse

Class Method Details

.ghcliObject



70
71
72
73
74
# File 'lib/sugarjar/util.rb', line 70

def self.ghcli(*)
  s = ghcli_nofail(*)
  s.error!
  s
end

.ghcli_nofail(*args) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sugarjar/util.rb', line 49

def self.ghcli_nofail(*args)
  SugarJar::Log.trace("Running: gh #{args.join(' ')}")
  gh = which('gh')
  s = Mixlib::ShellOut.new([gh] + args).run_command
  if s.error? && s.stderr.include?('gh auth')
    SugarJar::Log.info(
      'gh was run but no github token exists. Will run "gh auth login" ' +
      "to force\ngh to authenticate...",
    )
    unless system(gh, 'auth', 'login', '-p', 'ssh')
      SugarJar::Log.fatal(
        'That failed, I will bail out. Hub needs to get a github ' +
        'token. Try running "gh auth login" (will list info about ' +
        'your account) and try this again when that works.',
      )
      exit(1)
    end
  end
  s
end

.git(color: true) ⇒ Object



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

def self.git(*, color: true)
  s = git_nofail(*, :color => color)
  s.error!
  s
end

.git_nofail(*args, color: true) ⇒ Object



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

def self.git_nofail(*args, color: true)
  if %w{diff log grep branch}.include?(args[0]) &&
     args.none? { |x| x.include?('color') }
    args << (color ? '--color' : '--no-color')
  end
  SugarJar::Log.trace("Running: git #{args.join(' ')}")
  Mixlib::ShellOut.new([which('git')] + args).run_command
end

.in_repo?Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/sugarjar/util.rb', line 76

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

.repo_rootObject



81
82
83
# File 'lib/sugarjar/util.rb', line 81

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

.which(cmd) ⇒ Object

a mixin to hold stuff that Commands and RepoConfig both use



8
9
10
11
12
13
14
# File 'lib/sugarjar/util.rb', line 8

def self.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. Warn if it is us as that won’t work in 2.x



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sugarjar/util.rb', line 18

def self.which_nofail(cmd)
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |dir|
    p = File.join(dir, cmd)
    next unless File.exist?(p) && File.executable?(p)

    if File.basename(File.realpath(p)) == 'sj'
      SugarJar::Log.error(
        "'#{cmd}' is linked to 'sj' which is no longer supported.",
      )
      next
    end
    return p
  end
  false
end