Class: Sfn::Command::Lint

Inherits:
Sfn::Command show all
Includes:
Sfn::CommandModule::Base, Sfn::CommandModule::Template
Defined in:
lib/sfn/command/lint.rb

Overview

Lint command

Constant Summary

Constants included from Sfn::CommandModule::Template

Sfn::CommandModule::Template::DEFAULT_PROVIDER_NAME, Sfn::CommandModule::Template::MAX_PARAMETER_ATTEMPTS, Sfn::CommandModule::Template::TEMPLATE_IGNORE_DIRECTORIES

Constants inherited from Sfn::Command

CONFIG_BASE_NAME, VALID_CONFIG_EXTENSIONS

Instance Method Summary collapse

Methods included from Sfn::CommandModule::Template

included

Methods included from Sfn::CommandModule::Base

included

Methods inherited from Sfn::Command

#config, #initialize

Methods included from Sfn::CommandModule::Callbacks

#api_action!, #callbacks_for, #run_callbacks_for

Constructor Details

This class inherits a constructor from Sfn::Command

Instance Method Details

#execute!Object

Perform linting



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sfn/command/lint.rb', line 11

def execute!
  print_only_original = config[:print_only]
  config[:print_only] = true
  file = load_template_file
  ui.info "#{ui.color("Template Linting (#{provider.connection.provider}): ", :bold)} #{config[:file].sub(Dir.pwd, "").sub(%r{^/}, "")}"
  config[:print_only] = print_only_original

  raw_template = parameter_scrub!(template_content(file))

  if config[:print_only]
    ui.puts raw_template
  else
    result = lint_template(raw_template)
    if result == true
      ui.info ui.color("  -> VALID", :green, :bold)
    else
      ui.info ui.color("  -> INVALID", :red, :bold)
      result.each do |failure|
        ui.error "Result Set: #{ui.color(failure[:rule_set].name, :red, :bold)}"
        failure[:failures].each do |f_msg|
          ui.fatal f_msg
        end
      end
      raise "Linting failure"
    end
  end
end

#lint_template(template) ⇒ TrueClass, Array<Smash[:rule_set, :failures]>

Apply linting to given template

Parameters:

  • template (Hash)

Returns:

  • (TrueClass, Array<Smash[:rule_set, :failures]>)


43
44
45
46
47
48
49
50
51
# File 'lib/sfn/command/lint.rb', line 43

def lint_template(template)
  results = rule_sets.map do |set|
    result = set.apply(template)
    unless result == true
      Smash.new(:rule_set => set, :failures => result)
    end
  end.compact
  results.empty? ? true : results
end

#rule_setsArray<Sfn::Lint::RuleSet>

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sfn/command/lint.rb', line 54

def rule_sets
  sets = [config[:lint_directory]].flatten.compact.map do |directory|
    if File.directory?(directory)
      files = Dir.glob(File.join(directory, "**", "**", "*.rb"))
      files.map do |path|
        begin
          Sfn::Lint.class_eval(
            IO.read(path), path, 1
          )
        rescue
          ui.warn "Failed to load detected file: #{path}"
          nil
        end
      end
    end
  end.flatten.compact.find_all { |rs| rs.provider == provider.connection.provider }
  unless config[:local_rule_sets_only]
    sets += Sfn::Lint::RuleSet.get_all(provider.connection.provider)
  end
  if config[:disabled_rule_set]
    disabled = [config[:disabled_rule_set]].flatten.compact
    sets.delete_if { |i| disabled.include?(i.name.to_s) }
  end
  if config[:enabled_rule_set]
    enabled = [config[:enabled_rule_set]].flatten.compact
    sets.delete_if { |i| enabled.include?(i.name.to_s) }
  end
  sets
end