Class: Codesake::Cli

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/codesake/cli.rb', line 6

def options
  @options
end

#targetsObject (readonly)

Returns the value of attribute targets.



7
8
9
# File 'lib/codesake/cli.rb', line 7

def targets
  @targets
end

Instance Method Details

#error_messageObject



69
70
71
# File 'lib/codesake/cli.rb', line 69

def error_message
  @options[:message] if has_errors?
end

#has_errors?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/codesake/cli.rb', line 65

def has_errors?
  (@options[:error])
end

#is_good_target?(target) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/codesake/cli.rb', line 61

def is_good_target?(target)
  (!Dir.glob(target).empty?) or File.exists?(target) or File.directory?(target)
end

#parse(command_line) ⇒ Object



9
10
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/codesake/cli.rb', line 9

def parse(command_line)
  @options = {}

  return {:vulnerabilities=>:all} if (command_line.nil?) or (command_line.send(:empty?))

  begin
    option_parser =OptionParser.new do |opts|
      executable_name = File.basename($PROGRAM_NAME)
      opts.banner = 
        "codesake v#{Codesake::VERSION} - (C) 2012 - [email protected]\nReviews one or more source file for security issues.\n\nUsage #{executable_name} [options] sources\n"
    # opts.on("-h", "--help") do 
    #   @options[:help] = true 
    # end

    opts.on("-v", "--version", "Show codesake version") do
      @options[:version] = true
    end

    opts.on("-V", "--verbose", "Be verbose") do
      @options[:verbose] = true
    end

    opts.on("-k KEYWORDS", "--add-keys", "Add the command separated list of strings as reserved keywords") do |val|
      @options[:keywords] = val.trim.split(",")
    end

    opts.on("-o TARGET", "--output", "Write output to file, to json string or to db usin SQLite3") do |val|
      @options[:output]=:screen
      val=val.trim
      @options[:output]=val.to_sym if (val.to_sym == :file) or (val.to_sym == :json) or (val.to_sym == :db)
    end
    opts.on("-C", "--confirmed-vulnerabilities", "Show only confirmed vulnerabilities") do
      @options[:vulnerabilities] = :confirmed
    end
    opts.on("-A", "--all-vulnerabilities", "Show all vulnerabilities found [default]") do
      @options[:vulnerabilities] = :all
    end

    end

    rest = option_parser.parse(command_line)

    @targets = [] 
    @targets = build_target_list(rest[0].split(" ")) if expect_targets? and (! rest.empty?) and (! rest[0].nil?)
    @options[:vulnerabilities] = :all if @options[:vulnerabilities].nil?
  rescue OptionParser::InvalidOption => e
    @options={:error=>true, :message=>e.message}
  end
  @options
end