Module: Ronin::DB::CLI::Modifiable Private

Included in:
Commands::Creds, Commands::Emails, Commands::Hosts, Commands::Ips, Commands::Urls
Defined in:
lib/ronin/db/cli/modifiable.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Allows a ModelCommand to add, import, delete, or delete all records.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(command) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds the --add, --import, --delete, and --delete-all options to the command.

Parameters:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ronin/db/cli/modifiable.rb', line 35

def self.included(command)
  command.option :add, value: {
                         type: String,
                         usage: 'VALUE'
                       },
                       desc: 'Adds a value to the database'

  command.option :import, value: {
                            type:  String,
                            usage: 'FILE'
                          },
                          desc: 'Imports the values from the FILE into the database'

  command.option :delete, value: {
                            type: String,
                            usage: 'VALUE'
                          },
                          desc: 'Deletes a value from the database'

  command.option :delete_all, desc: 'Deletes all values from the database'
end

Instance Method Details

#add(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds a value to the database.

Parameters:

  • value (String)

    The value to add.



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ronin/db/cli/modifiable.rb', line 82

def add(value)
  record = model.import(value)

  unless record.valid?
    print_error "failed to import #{value}!"

    record.errors.full_messages.each do |message|
      print_error " - #{message}"
    end
  end
end

#delete(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deletes a value from the database.

Parameters:

  • value (String)

    The value to lookup and delete.



123
124
125
126
127
128
129
130
# File 'lib/ronin/db/cli/modifiable.rb', line 123

def delete(value)
  if (record = model.lookup(value))
    record.destroy
  else
    print_error "value does not exist in the database: #{value}"
    exit(-1)
  end
end

#delete_allObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deletes all values from the database.



135
136
137
# File 'lib/ronin/db/cli/modifiable.rb', line 135

def delete_all
  model.destroy_all
end

#import_file(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Imports the values from the given file.

Parameters:

  • path (String)

    The path to the file.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ronin/db/cli/modifiable.rb', line 100

def import_file(path)
  unless File.file?(path)
    print_error "no such file or directory: #{path}"
    exit(-1)
  end

  File.open(path) do |file|
    model.transaction do
      file.each_line(chomp: true) do |value|
        log_info "Importing #{value} ..." if verbose?

        add(value)
      end
    end
  end
end

#runObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the command.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ronin/db/cli/modifiable.rb', line 60

def run
  connect

  if options[:add]
    add(options[:add])
  elsif options[:import]
    import_file(options[:import])
  elsif options[:delete]
    delete(options[:delete])
  elsif options[:delete_all]
    delete_all
  else
    list
  end
end