Sha256: 61a5f14a458e04fb34ef51bb5a28e516571771c8e29feb1f63272d439d818f70

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

require 'yaml'

module SeedGenerator
  def self.add_container(container_file, m, long_name, docsite, readme_anchor)
    container_file.write("\n") unless container_file.size.zero?
    container_file.write("sc = SecurityContainer.where(name: '#{long_name}').first_or_initialize\n")
    container_file.write("sc.prog_args = '#{m['prog_args']}'\n")
    container_file.write("sc.default_config = #{symbolized_config(m['default_config'])}\n") if m.key?('default_config')
    container_file.write("sc.category = :#{m['category'].to_sym}\n")
    container_file.write("sc.test_types = #{m['test_types']}\n")
    container_file.write("sc.common_service_type = CommonServiceType.find_by(name: '#{m['common_service_type']}')\n") if m.key?('common_service_type')
    container_file.write("sc.configurable = #{m['configurable']}\n") if m.key?('configurable')
    container_file.write("sc.multi_host = #{m['multi_host']}\n") if m.key?('multi_host')
    container_file.write("sc.help_url = '#{docsite}##{readme_anchor}'\n")
    container_file.write("sc.save!\n")
  end

  def self.symbolized_config(config)
    '{ ' + config.reduce([]) do |a, p|
      a << "#{p.first}: '#{p.last}'"
    end.join(', ') + ' }'
  end

  def self.readme_anchor(readme)
    title = readme.readline
    # ## My Test Name123
    # Trim off ## portion of title, replace spaces with -, and downcase
    title.match(/\s*##\s+(.+)/)[1].strip.gsub(/[ .]/, '-').downcase
  end

  def self.process_manifests(seed_file, docsite)
    File.open(seed_file, 'w') do |container_file|
      Dir.glob('./**/manifest.yml').each do |p|
        manifest = YAML.load(File.new(p, 'r').read)
        readme_anchor = readme_anchor(File.new(p.gsub(/manifest\.yml/, 'README.md')))
        long_name = "#{manifest['registry']}/#{manifest['name']}:#{manifest['version']}"
        add_container(container_file, manifest, long_name, docsite, readme_anchor)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
norad_cli-0.2.2 lib/norad_cli/support/api_security_container_seed_script.rb
norad_cli-0.2.1 lib/norad_cli/support/api_security_container_seed_script.rb
norad_cli-0.2.0 lib/norad_cli/support/api_security_container_seed_script.rb
norad_cli-0.1.24 lib/norad_cli/support/api_security_container_seed_script.rb