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, #context, #info, #per_page, #token, #verbose

Instance Method Summary collapse

Methods inherited from Base

#client, 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

#bar_sizeObject



100
101
102
103
104
# 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) ⇒ 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 || "#{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) ⇒ Object



114
115
116
# 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) ⇒ Object



106
107
108
109
110
111
112
# 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) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/githuh/cli/commands/repo/list.rb', line 118

def repo_as_markdown(index, repo)
  "\n    ### \#{index + 1}. [\#{repo.name}](\#{repo.url}) (\#{repo.stargazers_count} \u2605)\n\n    \#{repo.language ? \"**\#{repo.language}**. \" : ''}\n    \#{repo.license ? \"Distributed under the **\#{repo.license.name}** license.\" : ''}\n\n    \#{repo.description}\n\n  REPO\nend\n"

#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
# 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