Class: SandiMeter::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/sandi_meter/cli.rb

Class Method Summary collapse

Class Method Details

.executeObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/sandi_meter/cli.rb', line 88

def execute
  cli = CommandParser.new
  cli.parse_options

  cli.config[:output_path] ||= File.expand_path(File.join(cli.config[:path], 'sandi_meter'))

  cli.config[:rule_thresholds] = cli.config[:rule_thresholds].split(",").map(&:to_i)

  if cli.config[:graph]
    FileUtils.mkdir_p(cli.config[:output_path]) unless Dir.exists?(cli.config[:output_path])

    create_config_file(cli.config[:output_path], '.sandi_meter', %w(db vendor).join("\n"))
    create_config_file(cli.config[:output_path], 'config.yml', YAML.dump({ thresholds: [90, 90, 90, 90] }))
  end

  if cli.config[:version]
    puts version_info
    exit 0
  end

  if cli.config[:rules]
    show_sandi_rules
    exit 0
  end

  scanner = SandiMeter::FileScanner.new(cli.config[:log])
  data = scanner.scan(cli.config[:path], cli.config[:details] || cli.config[:graph])

  if cli.config[:json]
    formatter = SandiMeter::JsonFormatter.new
  else
    formatter = SandiMeter::Formatter.new
  end

  formatter.print_data(data)

  if cli.config[:graph]
    if File.directory?(cli.config[:output_path])
      logger = SandiMeter::Logger.new(data)
      logger.log!(cli.config[:output_path])

      html_generator = SandiMeter::HtmlGenerator.new
      html_generator.copy_assets!(cli.config[:output_path])
      html_generator.generate_data!(cli.config[:output_path])
      html_generator.generate_details!(cli.config[:output_path], data)

      index_html_path = File.join(cli.config[:output_path], 'index.html')
      unless cli.config[:quiet]
        open_in_browser(index_html_path)
      end
    else
      puts "WARNING!!! HTML mode works only if you scan folder."
    end
  end

  config_file_path = File.join(cli.config[:output_path], 'config.yml')
  config =  if File.exists?(config_file_path)
              YAML.load(File.read(config_file_path))
            else
              { thresholds: cli.config[:rule_thresholds] }
            end

  if RulesChecker.new(data, config).ok?
    exit 0
  else
    exit 1
  end
end

.show_sandi_rulesObject



157
158
159
160
161
162
163
164
# File 'lib/sandi_meter/cli.rb', line 157

def show_sandi_rules
  puts %(
    1. 100 lines per class
    2. 5 lines per method
    3. 4 params per method call (and don't even try cheating with hash params)
    4. 1 instance variables per controller' action
  )
end