Module: Retag::Utils

Included in:
Image, Repo, Service
Defined in:
lib/retag/utils.rb

Instance Method Summary collapse

Instance Method Details

#cmd!(cmd, capture: false, logger: $logger) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/retag/utils.rb', line 7

def cmd!(cmd, capture: false, logger: $logger)
  $cmdnum ||= 0
  $cmdnum += 1
  tag = $cmdnum.to_s.rjust(3, '0')
  output = ''

  logger.tagged("CMD#{tag}".colorize(:light_black)) { logger.debug cmd.colorize(:light_black) }

  code = Open3.popen3(cmd) do |stdin, stdout, stderr, thr|
    stdin.close_write
    files = [stdout, stderr]

    until files.empty?
      ready = IO.select(files)
      readable = ready[0]

      readable.each do |f|
        begin
          line = f.readline
          if f == stderr
            logger.tagged("ERR#{tag}") { logger.error(line.strip) }
          else
            output += line
            logger.tagged("OUT#{tag}") { logger.info(line.strip) } unless capture
          end
        rescue EOFError => e
          files.delete f
        end
      end
    end

    thr.value
  end

  raise "Execution failed #{code}: #{cmd}" unless code.success?

  output
end

#dockerauth(hostname) ⇒ Object



46
47
48
49
50
51
# File 'lib/retag/utils.rb', line 46

def dockerauth(hostname)
  json = ENV['DOCKER_AUTH_CONFIG'].presence || File.read(File.expand_path('~/.docker/config.json'))
  auth = JSON.parse(json).dig('auths', hostname, 'auth')

  Base64.decode64(auth) if auth
end