Class: Pedant::CommandCheck

Inherits:
Command
  • Object
show all
Defined in:
lib/pedant/commands/check.rb

Class Method Summary collapse

Methods inherited from Command

all, banner, find, inherited, initialize!, list, run, usage

Class Method Details

.bindingObject



31
32
33
# File 'lib/pedant/commands/check.rb', line 31

def self.binding
  'check'
end

.helpObject



35
36
37
# File 'lib/pedant/commands/check.rb', line 35

def self.help
  @@optparse.to_s
end

.optparse(options, args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/pedant/commands/check.rb', line 39

def self.optparse(options, args)
  options[:checks] = Set.new

  @@optparse = OptionParser.new do |opts|
    opts.banner = "Usage: pedant [global-options] #{binding} [command-options] [args]"

    opts.separator ""
    opts.separator "Input formats:"

    opts.on('-f', '--filesystem', 'Read input from the filesystem.') do
      options[:input_mode] = :filesystem
    end

    opts.on('-g', '--git', 'Read input from a Git repository.') do
      options[:input_mode] = :git
    end

    opts.separator ""
    opts.separator "Output formats:"

    opts.on('-e', '--email', 'Output in a form suitable for an email.') do
      options[:output_mode] = :email
    end

    opts.on('-t', '--terminal', 'Output in a form suitable for a terminal.') do
      options[:output_mode] = :terminal
    end

    opts.separator ""
    opts.separator "Common operations:"

    opts.on('-c=NAME', '--check=NAME', 'Run only the check named, and its dependencies.') do |name|
      # Unmangle class name and be very forgiving.
      name = "Check" + name.gsub(/[-._ ]/, '')

      begin
        cls = Pedant.const_get(name)
      rescue NameError => e
        usage(e.message)
      end

      ([cls] + cls.depends).each { |cls| options[:checks] << cls }
    end

    opts.on('-h', '--help', 'Display this help screen.') do
      puts opts
      exit 1
    end

    opts.on('-l', '--list', 'List the available checks.') do
      puts Check.list
      exit 0
    end

    opts.on('-q', '--quiet', "Only speak up when something should be fixed.") do
      options[:quiet] = true
    end

    opts.on('-v', '--verbose', 'Output more information, use multiple time to increase verbosity.') do
      options[:verbosity] += 1
    end
  end

  # Load all of the checks.
  Check.initialize!

  @@optparse.order!(args)

  return options, args
end

.run_all(opts, args) ⇒ Object



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
# File 'lib/pedant/commands/check.rb', line 110

def self.run_all(opts, args)
  # Separate plugins and libraries from the rest of the arguments.
  paths = args.select { |a| a =~ /(\/|\.(inc|nasl|pasl|tasl))$/ }
  args -= paths

  # If we have paths that aren't acceptable, there's a problem.
  usage("One or more unacceptable files were specified.") unless args.empty?

  # If we have no paths to process, there's a problem.
  usage("No directories (/), libraries (.inc), or plugins (.nasl) were specified.") if paths.empty?

  # Collect all the paths together, recursively.
  dirents = []
  paths.each do |path|
    begin
      Pathname.new(path).find do |dirent|
        if dirent.file? && dirent.extname =~ /inc|nasl|pasl|tasl/
          dirents << dirent
        end
      end
    rescue SystemCallError => e
      usage(e.message)
    end
  end

  dirents.each { |d| run_one(opts, d) }
end

.run_one(opts, path) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/pedant/commands/check.rb', line 138

def self.run_one(opts, path)
  # Get a list of the checks we're going to be running.
  if not opts[:checks].empty?
    pending = opts[:checks].to_a
  else
    pending = Array.new(Check.all)
  end

  # Initialize the knowledge base where checks can store information for
  # other checks.
  kb = KnowledgeBase.new(:file_mode, path)

  run_checks = Check.run_checks_in_dependency_order(kb, pending)
  # When in quiet mode, only make a report for this file if a check did not pass
  return if opts[:quiet] && run_checks.all? { |chk| [:skip, :pass].include? chk.result }

  puts Rainbow("CHECKING: #{path}").cyan
  run_checks.each do |chk|
    next if [:skip, :pass].include?(chk.result) && opts[:quiet]
    puts chk.report(opts[:verbosity])
  end
  # Notify the user if any checks did not run due to unsatisfied
  # dependencies or a fatal error occurring before they had the chance to
  # run.
  pending.each do |cls|
    # Special case. This check is a shim to set things up for unit tests.
    next if cls == CheckParseTestCode
    puts cls.new(kb).report(opts[:verbosity])
  end
  puts
end