Class: Usedby::Cli

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

Overview

Define the command line interface.

Constant Summary collapse

GEMFILE_LOCK_SEARCH_TERM =
'org:%s filename:Gemfile.lock'
GEMSPEC_SEARCH_TERM =
'org:%s extension:gemspec'
USAGE =
<<~USAGE
  Usage: usedby [options] GITHUB_ORGANIZATION
USAGE

Instance Method Summary collapse

Instance Method Details

#runObject



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
59
# File 'lib/usedby/cli.rb', line 24

def run
  parse_options
  if ARGV.size != 1
    STDERR.puts USAGE
    return 1
  end
  github_organization = ARGV[0]

  access_token = ENV['GITHUB_ACCESS_TOKEN'] || \
                 STDIN.getpass('GitHub Personal Access Token: ')
  github = Octokit::Client.new(access_token: access_token)

  gems = {}

  remote_search(github, github_organization, GEMFILE_LOCK_SEARCH_TERM) do |gemfile_lock|
    content = remote_file(github, gemfile_lock)
    next unless content
    content = Bundler::LockfileParser.new(content)
    merge!(gems, process_gemfile(content, "#{gemfile_lock.repository.name}/#{gemfile_lock.path}"))
  end

  remote_search(github, github_organization, GEMSPEC_SEARCH_TERM) do |gemspec|
    content = remote_file(github, gemspec)
    next unless content
    merge!(gems, process_gemspec(content, "#{gemspec.repository.name}/#{gemspec.path}"))
  end

  case @options[:order]
  when 'projects'
    output_projects gems
  else
    output_gems gems
  end

  0
end