Class: Denmark

Inherits:
Object
  • Object
show all
Defined in:
lib/denmark.rb,
lib/denmark/version.rb

Defined Under Namespace

Classes: Plugins, Repository

Constant Summary collapse

VERSION =
'0.0.3'

Class Method Summary collapse

Class Method Details

.config(*args) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/denmark.rb', line 17

def self.config(*args)
  if args.empty?
    @config
  else
    @config.dig(*args)
  end
end

.config=(arg) ⇒ Object



13
14
15
16
# File 'lib/denmark.rb', line 13

def self.config=(arg)
  raise "Requires a Hash to set config, not a #{arg.class}" unless arg.is_a? Hash
  @config = arg
end

.evaluate(slug, options) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/denmark.rb', line 31

def self.evaluate(slug, options)
  @options = options
  slug = resolve_slug(slug)

  begin
    mod = PuppetForge::Module.find(slug)
  rescue Faraday::BadRequestError, Faraday::ResourceNotFound
    raise "The module `#{slug}` was not found on the Puppet Forge."
  end

  repo = Denmark::Repository.new(mod.homepage_url)
  data = Denmark::Plugins.new(options).run(mod, repo)

  case options[:format]
  when 'json'
    puts JSON.pretty_generate(data)
  when 'human'
    generate_report(data)
  else
    raise 'unknown format'
  end
end

.generate_report(data) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/denmark.rb', line 71

def self.generate_report(data)
  if data.empty?
    puts "Congrats, no smells discovered"
  else
    puts
    [:red, :orange, :yellow, :green].each do |severity|
      alerts = data.select {|i| i[:severity] == severity}
      next unless alerts.size > 0

      puts "[#{severity.upcase}] alerts:".color_name(severity)
      alerts.each do |alert|
        puts "  #{alert[:message]}"
        puts "    > #{alert[:explanation]}" if @options[:detail]
      end
      puts
    end
  end
end

.list(options) ⇒ Object



26
27
28
29
# File 'lib/denmark.rb', line 26

def self.list(options)
  puts
  puts Denmark::Plugins.new(options).list
end

.resolve_slug(path) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/denmark.rb', line 54

def self.resolve_slug(path)
  begin
    if path.nil?
      path = JSON.parse(File.read('metadata.json'))['name']
    elsif File.directory?(path)
      path = JSON.parse(File.read("#{path}/metadata.json"))['name']
    elsif path.end_with?('metadata.json')
      path = JSON.parse(File.read(path))['name']
    end
  rescue Errno::ENOENT => e
    raise "Cannot load metadata from '#{path}'. Pass this tool the name of a module, or the local path to a module."
  end

  # if we get this far, assume it's the name of a module and normalize it
  path.sub('/', '-')
end