Class: Githuh::CLI::Commands::Repo::List

Inherits:
Base
  • Object
show all
Defined in:
lib/githuh/cli/commands/repo/list.rb

Constant Summary collapse

FORMATS =
{
  markdown: 'md',
  json:     'json'
}.freeze
DEFAULT_FORMAT =
:markdown
DEFAULT_OUTPUT_FORMAT =
"<username>.repositories.<format>"
FORK_OPTIONS =
%w(exclude include only).freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#box, #client, #context, #info, #per_page, #token, #verbose

Instance Method Summary collapse

Methods inherited from Base

inherited

Instance Attribute Details

#fileObject

Returns the value of attribute file.



26
27
28
# File 'lib/githuh/cli/commands/repo/list.rb', line 26

def file
  @file
end

#filenameObject

Returns the value of attribute filename.



26
27
28
# File 'lib/githuh/cli/commands/repo/list.rb', line 26

def filename
  @filename
end

#forksObject

Returns the value of attribute forks.



26
27
28
# File 'lib/githuh/cli/commands/repo/list.rb', line 26

def forks
  @forks
end

#formatObject

Returns the value of attribute format.



26
27
28
# File 'lib/githuh/cli/commands/repo/list.rb', line 26

def format
  @format
end

#outputObject

Returns the value of attribute output.



26
27
28
# File 'lib/githuh/cli/commands/repo/list.rb', line 26

def output
  @output
end

#privateObject

Returns the value of attribute private.



26
27
28
# File 'lib/githuh/cli/commands/repo/list.rb', line 26

def private
  @private
end

#record_countObject

Returns the value of attribute record_count.



26
27
28
# File 'lib/githuh/cli/commands/repo/list.rb', line 26

def record_count
  @record_count
end

#reposObject

Returns the value of attribute repos.



26
27
28
# File 'lib/githuh/cli/commands/repo/list.rb', line 26

def repos
  @repos
end

Instance Method Details

#call(file: nil, format: nil, forks: nil, private: nil, **opts) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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 || "#{.}.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

#create_progress_barObject



101
102
103
104
105
106
107
108
109
# File 'lib/githuh/cli/commands/repo/list.rb', line 101

def create_progress_bar
  number_of_pages = client.last_response.rels[:last].href.match(/page=(\d+).*$/)[1]
  TTY::ProgressBar.new("[:bar]",
                       title:    'Fetching Repositories',
                       total:    number_of_pages.to_i,
                       width:    ui_width - 2,
                       head:     '',
                       complete: ''.magenta)
end

#render_as_json(repositories) ⇒ Object



119
120
121
# File 'lib/githuh/cli/commands/repo/list.rb', line 119

def render_as_json(repositories)
  JSON.pretty_generate(repositories.map(&:to_hash))
end

#render_as_markdown(repositories) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/githuh/cli/commands/repo/list.rb', line 111

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) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/githuh/cli/commands/repo/list.rb', line 123

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

#repositoriesObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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    = create_progress_bar if info && !verbose && page == 0

      bar&.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