class Githuh::CLI::Commands::Repo::List

Constants

DEFAULT_FORMAT
DEFAULT_OUTPUT_FORMAT
FORK_OPTIONS
FORMATS

Attributes

file[RW]
filename[RW]
forks[RW]
format[RW]
output[RW]
private[RW]
record_count[RW]
repos[RW]

Public Instance Methods

bar_size() click to toggle source
# File lib/githuh/cli/commands/repo/list.rb, line 100
def bar_size
  return 1 if client&.last_response.nil?

  client&.last_response.rels[:last].href.match(/page=(\d+).*$/)[1].to_i
end
call(file: nil, format: nil, forks: nil, private: nil, **opts) click to toggle source
Calls superclass method Githuh::CLI::Commands::Base#call
# File lib/githuh/cli/commands/repo/list.rb, line 36
def call(file: nil, format: nil, forks: nil, private: nil, **opts)
  super(**opts)

  self.record_count = 0
  self.forks        = forks
  self.private      = private
  self.repos        = []
  self.output       = StringIO.new
  self.format       = (format || DEFAULT_FORMAT).to_sym

  self.filename = file || "#{user_info.login}.repositories.#{FORMATS[self.format]}"
  self.file     = File.open(filename, 'w')

  puts
  puts TTY::Box.info("Format : #{self.format}\n" \
                     "File   : #{filename}\n" \
                     "Forks  : #{self.forks}\n",
                     width:   ui_width,
                     padding: 1)
  puts
  # —————————— actually get all repositories ———————————————
  self.file.write send("render_as_#{format}", repositories)
  # ————————————————————————————————————————————————————————

  puts
  puts TTY::Box.info("Success: written a total of #{record_count} records to #{filename}",
                     width: ui_width, padding: 1)
  puts
ensure
  file.close if file.respond_to?(:close) && !file.closed?
end
render_as_json(repositories) click to toggle source
# File lib/githuh/cli/commands/repo/list.rb, line 114
def render_as_json(repositories)
  JSON.pretty_generate(repositories.map(&:to_hash))
end
render_as_markdown(repositories) click to toggle source
# File lib/githuh/cli/commands/repo/list.rb, line 106
def render_as_markdown(repositories)
  output.puts "### #{client.user.name}'s Repos\n"
  repositories.each_with_index do |repo, index|
    output.puts repo_as_markdown(index, repo)
  end
  output.string
end
repo_as_markdown(index, repo) click to toggle source
# File lib/githuh/cli/commands/repo/list.rb, line 118
          def repo_as_markdown(index, repo)
            <<~REPO

              ### #{index + 1}. [#{repo.name}](#{repo.url}) (#{repo.stargazers_count} ★)

              #{repo.language ? "**#{repo.language}**. " : ''}
              #{repo.license ? "Distributed under the **#{repo.license.name}** license." : ''}

              #{repo.description}

            REPO
          end
repositories() click to toggle source
# File lib/githuh/cli/commands/repo/list.rb, line 68
def repositories
  page = 0
  bar  = nil

  [].tap do |repo_list|
    loop do
      options = {
        page:     page,
        per_page: per_page,
        type:     :owner,
      }

      result = client.repos({}, query: options)
      bar('Repositories')&.advance

      filter_result!(result)

      break if result.empty?

      result.each { |repo| printf "%s\n", repo.name } if verbose

      repo_list << result

      page += 1

      self.record_count += result.size
    end

    bar&.finish; puts
  end.flatten.sort_by(&:stargazers_count).reverse.uniq(&:name)
end

Private Instance Methods

filter_result!(result) click to toggle source
# File lib/githuh/cli/commands/repo/list.rb, line 133
def filter_result!(result)
  result.reject! do |r|
    fork_reject = case forks
    when 'exclude'
      r.fork
    when 'only'
      !r.fork
    when 'include'
      false
    end

    private_reject = case private
    when true
      !r.private
    when false
      r.private
    when nil
      false
    end

    fork_reject || private_reject
  end
end