class Ronin::CLI::Commands::Tips
Prints a random tip on how to use ‘ronin`.
## Usage
ronin tips [options]
## Options
--list-categories Prints all category names -c, --category STR Print a random tip from the category -s, --search TEXT Searches all tip files for the text -h, --help Print help information
Constants
- TIPS_DIR
Path to the ‘data/tips/` directory.
Public Instance Methods
print_matching_tips(text, category: nil)
click to toggle source
Prints all tips that contain the given text.
# File lib/ronin/cli/commands/tips.rb, line 181 def print_matching_tips(text, category: nil) search_tip_files(text, category: category).each do |path| print_tip(path) end end
print_random_tip(category=nil)
click to toggle source
Prints a random tip.
@param [String, nil] category
The optional tips category to select the random tip from.
@api private
# File lib/ronin/cli/commands/tips.rb, line 174 def print_random_tip(category=nil) print_tip(random_tip_path(category)) end
print_tip(path)
click to toggle source
Prints a tip at the given path.
@param [String] path
The path to the tip file.
# File lib/ronin/cli/commands/tips.rb, line 155 def print_tip(path) contents = File.read(path) puts contents unless contents.end_with?("#{$/}#{$/}") puts puts end end
random_tip_path(category=nil)
click to toggle source
Gets a path to a random tip.
@param [String, nil] category
The optional tips category to select the random tip from.
@return [String]
The path to the random tip.
# File lib/ronin/cli/commands/tips.rb, line 127 def random_tip_path(category=nil) tip_paths(category).sample end
run()
click to toggle source
Runs the tip command.
# File lib/ronin/cli/commands/tips.rb, line 63 def run if options[:list_categories] puts tip_category_names else category = options[:category] if category && !tip_category_names.include?(category) print_error "unknown category name: #{category}." print_error "please see `ronin tips --list-categories` for all category names." exit(1) end if options[:search] print_matching_tips(options[:search], category: category) else print_random_tip(category) end end end
search_tip_files(text, category: nil)
click to toggle source
Searches through all tip files for the given text.
@param [String] text
The text to search for.
@param [String, nil] category
The optional tips category to search within.
@return [Array<String>]
The paths to the tip files containing the matching text.
# File lib/ronin/cli/commands/tips.rb, line 143 def search_tip_files(text, category: nil) tip_paths(category).select do |path| File.read(path).include?(text) end end
tip_category_names()
click to toggle source
All of the tip category names.
@return [Array<String>]
# File lib/ronin/cli/commands/tips.rb, line 97 def tip_category_names tip_category_paths.map { |path| File.basename(path) } end
tip_category_paths()
click to toggle source
All of the paths to the tip category directories.
@return [Array<String>]
# File lib/ronin/cli/commands/tips.rb, line 88 def tip_category_paths Dir[File.join(TIPS_DIR,'*/')] end
tip_paths(category=nil)
click to toggle source
Gets the paths to all of the tip files.
@param [String, nil] category
The optional tips category to select the tip files from.
@return [Array<String>]
The paths to all files in `data/tips/`.
# File lib/ronin/cli/commands/tips.rb, line 110 def tip_paths(category=nil) glob_pattern = if category then File.join(TIPS_DIR,category,'*.txt') else File.join(TIPS_DIR,'{*/}*.txt') end return Dir[glob_pattern] end