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
# 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

      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
  end

  # Load all of the checks.
  Check.initialize!

  @@optparse.order!(args)

  return options, args
end

.run_all(opts, args) ⇒ Object



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

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



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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/pedant/commands/check.rb', line 130

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 = Check.all
  end

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

  # Try to run each pending check, until we've run all our checks or
  # deadlocked.
  fatal = false
  until pending.empty? || fatal
    # Find all of the checks that can run right now.
    ready = pending.select { |cls| cls.ready?(kb) }
    break if ready.empty?

    # Run all of the checks that are ready.
    ready.each do |cls|
      # Create a new check instance.
      chk = cls.new(kb)
      pending.delete(cls)

      chk.run

      # Fatal errors mean that no further checks should be processed.
      if chk.result == :fatal
        fatal = true
        break
      end

      # Display the results of the check.
      puts chk.report(opts[:verbosity])
    end
  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 { |cls| puts cls.new(kb).report(opts[:verbosity]) }
end