Class: Gitbulk::Util
- Inherits:
-
Object
- Object
- Gitbulk::Util
- Defined in:
- lib/gitbulk/utils.rb
Class Method Summary collapse
- .cmd!(cmd, capture: false, logger: $logger) ⇒ Object
- .logger ⇒ Object
- .setup ⇒ Object
- .within_repo(git, logger: $logger) ⇒ Object
Class Method Details
.cmd!(cmd, capture: false, logger: $logger) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/gitbulk/utils.rb', line 52 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)) do logger.debug cmd.colorize(:light_black) end 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 |
.logger ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/gitbulk/utils.rb', line 11 def logger $logger ||= ActiveSupport::TaggedLogging.new(Logger.new(STDOUT)).tap do |logger| logger.level = ENV.fetch('LOG_LEVEL', Logger::INFO) formatter = Logger::Formatter.new formatter.extend ActiveSupport::TaggedLogging::Formatter logger.formatter = formatter end end |
.setup ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/gitbulk/utils.rb', line 20 def setup STDOUT.sync = true STDERR.sync = true String.class_eval do def to_bool return true if self == true || self =~ (/(true|t|yes|y|1)$/i) return false if self == false || self.blank? || self =~ (/(false|f|no|n|0)$/i) raise ArgumentError.new("invalid value for Boolean: \"#{self}\"") end end end |
.within_repo(git, logger: $logger) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/gitbulk/utils.rb', line 34 def within_repo(git, logger: $logger) _host, name = git.split(':') logger.tagged(name) do if File.exist?(name) Dir.chdir(name) do |_dir| cmd!('git fetch --prune', logger: logger) yield(name) end else cmd!("git clone --mirror #{git} #{name}", logger: logger) Dir.chdir(name) do |_dir| yield(name) end end end end |