Class: Nasl::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/nasl/command.rb

Class Method Summary collapse

Class Method Details

.allObject



33
34
35
# File 'lib/nasl/command.rb', line 33

def self.all
  (@_all ||= [])
end


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nasl/command.rb', line 49

def self.banner(title, width=80)
  # Create center of banner.
  middle = "[ #{title} ]"

  # Make sure width is a float.
  width = width.to_f

  # Create bars on either side.
  leftover = (width - middle.length) / 2
  left = '-' * leftover.floor
  right = '-' * leftover.ceil

  left + middle + right
end

.find(cmd) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/nasl/command.rb', line 41

def self.find(cmd)
  all.each do |cls|
    return cls if cls.binding == cmd
  end

  nil
end

.inherited(cls) ⇒ Object



37
38
39
# File 'lib/nasl/command.rb', line 37

def self.inherited(cls)
  all << cls
end

.initialize!Object



29
30
31
# File 'lib/nasl/command.rb', line 29

def self.initialize!
  Dir.glob(Nasl.lib + 'nasl/commands/*.rb').each { |f| load(f) }
end

.run(cfg, args) ⇒ Object



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
# File 'lib/nasl/command.rb', line 64

def self.run(cfg, args)
  # Separate plugins and libraries from the rest of the arguments.
  paths = args.select { |arg| arg =~ /(\/|\.(inc|nasl))$/ }
  args -= paths

  # If we have no paths to process, there's a problem. Special purpose
  # commands that don't require files to be declared should override this
  # method.
  if paths.empty?
    puts "No directories (/), libraries (.inc), or plugins (.nasl) were specified."
    exit 1
  end

  # Collect all the paths together, recursively.
  dirents = []
  paths.each do |path|
    Pathname.new(path).find do |dirent|
      if dirent.file? && dirent.extname =~ /inc|nasl/
        dirents << dirent
      end
    end
  end

  # If the command is capable of handling all the paths at once, send them
  # in a group, otherwise send them individually.
  if self.respond_to? :analyze_all then
    analyze_all(cfg, dirents, args)
  else
    dirents.each do |d|
      puts banner(d.basename)
      analyze(cfg, d, args)
      puts banner(d.basename)
    end
  end
end