class Githuh::CLI::Commands::Base

Attributes

box[RW]
context[RW]
info[RW]
per_page[RW]
token[RW]
verbose[RW]

Public Class Methods

inherited(base) click to toggle source
Calls superclass method
# File lib/githuh/cli/commands/base.rb, line 20
def inherited(base)
  super
  base.instance_eval do
    option :api_token, required: false, desc: "Github API token; if not given, user.token is read from ~/.gitconfig"
    option :per_page, required: false, default: DEFAULT_PAGE_SIZE, desc: "Pagination page size for Github API"
    option :info, type: :boolean, default: true, desc: 'Print UI elements, like a the progress bar'
    option :verbose, type: :boolean, default: false, desc: 'Print additional debugging info'
  end
end

Public Instance Methods

call(api_token: nil, per_page: DEFAULT_PAGE_SIZE, verbose: false, info: true) click to toggle source
# File lib/githuh/cli/commands/base.rb, line 33
def call(api_token: nil,
         per_page: DEFAULT_PAGE_SIZE,
         verbose: false,
         info: true)

  self.context  = Githuh
  self.verbose  = verbose
  self.info     = info
  self.token    = api_token || determine_github_token
  self.per_page = per_page.to_i || DEFAULT_PAGE_SIZE

  if info
    begin
      print_userinfo
    rescue StandardError
      nil
    end
  end
end
client() click to toggle source
# File lib/githuh/cli/commands/base.rb, line 53
def client
  @client ||= Octokit::Client.new(access_token: token)
end

Protected Instance Methods

bar(title = DEFAULT_TITLE) click to toggle source
# File lib/githuh/cli/commands/base.rb, line 75
def bar(title = DEFAULT_TITLE)
  @bar ||= create_progress_bar(title: title)
end
bar_size() click to toggle source

Overwrite me

# File lib/githuh/cli/commands/base.rb, line 80
def bar_size
  0
end
create_progress_bar(size = bar_size, title: DEFAULT_TITLE) click to toggle source
# File lib/githuh/cli/commands/base.rb, line 84
def create_progress_bar(size = bar_size, title: DEFAULT_TITLE)
  return unless info || verbose

  TTY::ProgressBar.new("[:bar]",
                       title:    title,
                       total:    size.to_i,
                       width:    ui_width - 2,
                       head:     '',
                       complete: '▉'.magenta)
end
determine_github_token() click to toggle source
# File lib/githuh/cli/commands/base.rb, line 95
def determine_github_token
  @github_token ||= (ENV['GITHUB_TOKEN'] || `git config --global --get user.token`.chomp)

  return @github_token unless @github_token.empty?

  raise "No token was found in your ~/.gitconfig.\n" \
        "To add, run the following command: \n" \
        "git config --global --set user.token YOUR_GITHUB_TOKEN\n" \
        "or set environment variable GITHUB_TOKEN"
end
puts(*args) click to toggle source
# File lib/githuh/cli/commands/base.rb, line 59
def puts(*args)
  stdout.puts(*args)
end
ui_width() click to toggle source
# File lib/githuh/cli/commands/base.rb, line 71
def ui_width
  80
end
user_info() click to toggle source
# File lib/githuh/cli/commands/base.rb, line 67
def user_info
  @user_info ||= client.user
end
warn(*args) click to toggle source
# File lib/githuh/cli/commands/base.rb, line 63
def warn(*args)
  stderr.puts(*args)
end

Private Instance Methods

h(arg) click to toggle source
# File lib/githuh/cli/commands/base.rb, line 136
def h(arg)
  arg.to_s
end
print_userinfo() click to toggle source