Class: Ronin::Repos::CacheDir Private

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ronin/repos/cache_dir.rb

Overview

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

Manages the ~/.cache/ronin-repos/ directory and the repositories contained within.

Constant Summary collapse

PATH =

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

The ~/.cache/ronin-repos/ directory where all repos are stored.

Core::Home.cache_dir('ronin-repos')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = PATH) ⇒ CacheDir

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.

Initializes the repository cache.

Parameters:

  • path (String) (defaults to: PATH)

    The path to the repository cache directory.



51
52
53
# File 'lib/ronin/repos/cache_dir.rb', line 51

def initialize(path=PATH)
  @path = path
end

Instance Attribute Details

#pathString (readonly)

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.

The path to the cache directory.

Returns:

  • (String)


43
44
45
# File 'lib/ronin/repos/cache_dir.rb', line 43

def path
  @path
end

Instance Method Details

#[](name) ⇒ Repository

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.

Accesses a repository from the cache directory.

Parameters:

  • name (String)

    The name of the repository.

Returns:

Raises:

  • (RepositoryNotFound)

    No repository exists with the given name in the cache directory.



67
68
69
70
71
72
73
74
75
# File 'lib/ronin/repos/cache_dir.rb', line 67

def [](name)
  path = File.join(@path,name.to_s)

  unless File.directory?(path)
    raise(RepositoryNotFound,"repository not found: #{name.inspect}")
  end

  return Repository.new(path)
end

#each {|repo| ... } ⇒ Enumerator

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.

Enumerates through every repository in the cache directory.

Yields:

  • (repo)

    The given block will be passed each repository.

Yield Parameters:

  • repo (Repository)

    A repository from the cache directory.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.



89
90
91
92
93
94
95
# File 'lib/ronin/repos/cache_dir.rb', line 89

def each
  return enum_for unless block_given?

  each_child_directory do |path|
    yield Repository.new(path)
  end
end

#find_file(path) ⇒ String?

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.

Finds the first matching file.

Examples:

repos.find_file("wordlists/wordlist.txt")
# => "/home/user/.cache/ronin-repos/foo-repo/wordlists/wordlist.txt"

Parameters:

  • path (String)

    The relative path of the file.

Returns:

  • (String, nil)

    The absolute path of the matching file or nil if no matching file could be found.



173
174
175
176
177
178
179
# File 'lib/ronin/repos/cache_dir.rb', line 173

def find_file(path)
  each do |repo|
    if (file = repo.find_file(path))
      return file
    end
  end
end

#glob(pattern, &block) ⇒ Array<String>

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.

Finds all files in all repos that matches the glob pattern.

Examples:

repos.glob("wordlists/*.txt")
# => ["/home/user/.cache/ronin-repos/foo-repo/wordlists/cities.txt",
#     "/home/user/.cache/ronin-repos/foo-repo/wordlists/states.txt",
#     "/home/user/.cache/ronin-repos/bar-repo/wordlists/bands.txt",
#     "/home/user/.cache/ronin-repos/bar-repo/wordlists/beers.txt"]

Parameters:

  • pattern (String)

    The file glob pattern to search for.

Returns:

  • (Array<String>)

    The absolute paths to the files that match the glob pattern.



197
198
199
200
201
202
203
# File 'lib/ronin/repos/cache_dir.rb', line 197

def glob(pattern,&block)
  return enum_for(__method__,pattern).to_a unless block_given?

  each do |repo|
    repo.glob(pattern,&block)
  end
end

#install(uri, name = nil) ⇒ Repository

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.

Clones and installs a repository into the cache directory.

Parameters:

  • uri (String, URI::HTTPS)

    The URI to clone the repository from.

  • name (String, nil) (defaults to: nil)

    The explicit name of the repository to use. Defaults to the base-name of the URI's path, sans any .git extension.

Returns:

  • (Repository)

    The newly installed repository.

Raises:



116
117
118
119
120
121
122
# File 'lib/ronin/repos/cache_dir.rb', line 116

def install(uri,name=nil)
  uri    = uri.to_s
  name ||= File.basename(uri,File.extname(uri))
  path   = File.join(@path,name)

  return Repository.install(uri,path)
end

#list_files(pattern = '{**/}*.*') ⇒ Set<String>

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.

Lists all files across all repos installed in the cache directory.

Examples:

repos.list_files('exploits/{**/}*.rb')
# => #<Set: {"exploits/exploit1.rb", "exploits/exploit2.rb"}>

Parameters:

  • pattern (String) (defaults to: '{**/}*.*')

    The optional glob pattern to use to list specific files.

Returns:

  • (Set<String>)

    The matching paths within the repository.



218
219
220
221
222
# File 'lib/ronin/repos/cache_dir.rb', line 218

def list_files(pattern='{**/}*.*')
  each_with_object(Set.new) do |repo,files|
    files.merge(repo.list_files(pattern))
  end
end

#purgeObject

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 repositories in the cache directory.



155
156
157
# File 'lib/ronin/repos/cache_dir.rb', line 155

def purge
  each(&:delete)
end

#remove(name) ⇒ 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.

Removes a repository from the cache directory.

Parameters:

  • name (String)

    The repository name to find and delete.

Raises:

  • (RepositoryNotFound)

    The repository with the given name does not exist in the cache directory.



148
149
150
# File 'lib/ronin/repos/cache_dir.rb', line 148

def remove(name)
  self[name].delete
end

#to_sString

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.

Converts the cache directory to a String.

Returns:

  • (String)

    The path to the cache directory.



230
231
232
# File 'lib/ronin/repos/cache_dir.rb', line 230

def to_s
  @path
end

#updateObject

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.

Updates all repositories in the cache directory.

Raises:



130
131
132
133
134
135
136
# File 'lib/ronin/repos/cache_dir.rb', line 130

def update
  each do |repo|
    repo.update
  rescue CommandFailed
    # ignore any `git` errors when updating
  end
end