Sha256: a21a9313095898c996d488b48a99d40d92c6da9df3146f5921624e8a3184e8fb

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

require 'open3'
require 'renuo_bin_check/result'

module RenuoBinCheck
  class ServantThread
    attr_reader :script_config
    def initialize(script_config)
      @script_config = script_config
      script_files = @script_config.script_files
      @cacher = Cacher.new(@script_config.script_name, script_files) if script_files
    end

    def run
      @script_config.script_files ? run_with_cache : run_command
    end

    private

    def run_with_cache
      @cacher.cache(run_command) unless @cacher.exists?
      @cacher.result
    end

    def run_command
      standard_output, error_output, process = Open3.capture3(@script_config.script_command)
      @result = Result.new(standard_output, error_output, process.exitstatus)
      override_output
      @script_config.reversed_exit? ? reverse_result : @result
    end

    def override_output
      standard_output = @script_config.script_standard_output ||= @result.standard_output
      error_output = @script_config.script_error_output ||= @result.error_output
      @result = Result.new(standard_output + @script_config.appended_standard_output,
                           error_output + @script_config.appended_error_output,
                           @result.exit_code)
    end

    def reverse_result
      Result.new(@result.error_output, @result.standard_output, @result.exit_code == 0 ? 1 : 0)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
renuo-bin-check-0.2.1 lib/renuo_bin_check/servant_thread.rb
renuo-bin-check-0.2.0 lib/renuo_bin_check/servant_thread.rb