Sha256: 317413220f0d969b0eb9cd0776319285f170f690913bde4775e3283d878638ae

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

module ConfCtl
  class NixCopy
    # @param target [String]
    # @param store_path [String]
    def initialize(target, store_path)
      @target = target
      @store_path = store_path
      @total = nil
      @progress = 0
    end

    # @yieldparam progress [Integer]
    # @yieldparam total [Integer]
    # @yieldparam path [String]
    def run!(&block)
      cmd = SystemCommand.new

      line_buf = StdLineBuffer.new do |_out, err|
        parse_line(err, &block) if err && block
      end

      ret = cmd.run!(
        'nix-copy-closure',
        '--to', "root@#{target}",
        store_path,
        &line_buf.feed_block
      )

      line_buf.flush
      ret
    end

    protected

    attr_reader :target, :store_path

    def parse_line(line)
      if @total.nil? && /^copying (\d+) paths/ =~ line
        @total = ::Regexp.last_match(1).to_i
        return
      end

      return unless /^copying path '([^']+)/ =~ line

      @progress += 1
      yield(@progress, @total, ::Regexp.last_match(1))
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
confctl-2.0.0 lib/confctl/nix_copy.rb
confctl-1.0.0 lib/confctl/nix_copy.rb