Sha256: 7a1a17c8f75df440ab58e59eaa3c1874913422e4e6cc197e8149deb6e77616fc

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

require 'json'

require_relative 'inspec_result'

# DeployRubygem - deploy a gem using rake
# Containing a class
module DeployRubygem
  # Using Inspec to deploy and manage Inspec
  class InspecTestor
    attr_reader :inspec_path, :input_file, :waiver_file

    def initialize(inspec_path, input_file = nil, waiver_file = nil)
      @inspec_path = inspec_path
      @input_file = input_file
      @waiver_file = waiver_file
    end

    def inspec_name
      File.basename(inspec_path)
    end

    def apply
      puts "ActuaL Dir #{Dir.pwd}"
      puts "inspec_name = #{inspec_name}"
      puts "input_file = #{input_file}"
      puts "waiver_file = #{waiver_file}"

      check
      system("inspec exec #{inspec_path} #{inspec_options.join(' ')}")
    end

    def update
      system("rm -rf #{inspec_path}/vendor")
      system("rm #{inspec_path}/inspec.lock")
      system("inspec vendor #{inspec_path} ")
    end

    def save_as_html(html_file)
      check
      system("inspec exec #{inspec_path} #{inspec_options.join(' ')} --reporter html:#{html_file}")
    end

    def test_json
      @test_json ||= InspecTestorResult.new(as_json)
    end

    private

    def chef_license
      '--chef-licens accept'
    end

    def inspec_options
      cmd_opt = [chef_license]

      unless input_file.nil?
        cmd_opt << '--input-file'
        cmd_opt << input_file
      end

      unless waiver_file.nil?
        cmd_opt << '--waiver-file'
        cmd_opt << waiver_file
      end
      puts "Using inspec options #{cmd_opt}"
      cmd_opt
    end

    def check
      update
      system("inspec check #{inspec_path}")
    end

    def as_json
      check
      request_str = `inspec exec #{inspec_path} #{inspec_options.join(' ')} --reporter json`
      JSON.parse(request_str)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
deploy_rubygem-0.60.23 lib/deploy_rubygem/inspectestor.rb
deploy_rubygem-0.60.22 lib/deploy_rubygem/inspectestor.rb