Module: SugarJar::Util

Defined in:
lib/sugarjar/util.rb

Class Method Summary collapse

Class Method Details

.forge_nofail(cli, *args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sugarjar/util.rb', line 69

def self.forge_nofail(cli, *args)
  SugarJar::Log.trace("Running: #{cli} #{args.join(' ')}")
  bin = which(cli)
  s = Mixlib::ShellOut.new([bin] + args).run_command
  if s.error? && s.stderr.include?("#{cli} auth")
    SugarJar::Log.info(
      'glab was run but no gitlab token exists. Will run ' +
      '"glab auth login" to force\ngh to authenticate...',
    )
    unless system(bin, '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

.ghcliObject



53
54
55
56
57
# File 'lib/sugarjar/util.rb', line 53

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

.ghcli_nofailObject



49
50
51
# File 'lib/sugarjar/util.rb', line 49

def self.ghcli_nofail(*)
  forge_nofail('gh', *)
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

.glcliObject



63
64
65
66
67
# File 'lib/sugarjar/util.rb', line 63

def self.glcli(*)
  s = glcli_nofail(*)
  s.error!
  s
end

.glcli_nofailObject



59
60
61
# File 'lib/sugarjar/util.rb', line 59

def self.glcli_nofail(*)
  forge_nofail('glab', *)
end

.in_repo?Boolean

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/sugarjar/util.rb', line 90

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

.repo_rootObject



95
96
97
# File 'lib/sugarjar/util.rb', line 95

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