Class: GithubCLI::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/github_cli/cli.rb

Overview

Main command line interface

Constant Summary collapse

ALIASES =
{
  'repository' => 'repo',
  'reference'  => 'ref',
  'is'         => :issue,
  '--version'  => 'version',
  '-V'         => 'version',
  'ls'         => 'list'
}

Constants inherited from Thor

Thor::DynamicTask, Thor::HELP_MAPPINGS, Thor::HiddenTask, Thor::THOR_RESERVED_WORDS, Thor::Task, Thor::UndefinedTaskError, Thor::VERSION

Instance Attribute Summary

Attributes included from Thor::Actions

#behavior

Attributes included from Thor::Base

#args, #options, #parent_options

Instance Method Summary collapse

Methods included from Thor::Actions

#action, #append_to_file, #apply, #chmod, #comment_lines, #copy_file, #create_file, #create_link, #destination_root, #destination_root=, #directory, #empty_directory, #find_in_source_paths, #get, #gsub_file, #in_root, included, #inject_into_class, #insert_into_file, #inside, #link_file, #prepend_to_file, #relative_to_original_destination_root, #remove_file, #run, #run_ruby_script, #source_paths, #template, #thor, #uncomment_lines

Methods inherited from Thor

check_unknown_options!, check_unknown_options?, command_help, default_command, desc, global_flags, handle_no_task_error, help, #help, long_desc, map, method_option, method_options, package_name, printable_commands, register, stop_on_unknown_option!, stop_on_unknown_option?, subcommand, subcommands

Methods included from Thor::Base

included, register_klass_file, shell, shell=, subclass_files, subclasses

Constructor Details

#initialize(*args) ⇒ CLI


8
9
10
11
12
13
14
# File 'lib/github_cli/cli.rb', line 8

def initialize(*args)
  super
  the_shell = (options["no-color"] ? Thor::Shell::Basic.new : shell)
  GithubCLI.ui = UI.new(the_shell)
  GithubCLI.ui.debug! if options["verbose"]
  options["no-pager"] ? Pager.disable : Pager.enable
end

Instance Method Details

#authorizeObject


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/github_cli/cli.rb', line 91

def authorize
  global_options = options.dup
  params = {}
  params['scopes']   = options[:scopes] || %w(public_repo repo)
  params['note']     = options[:note] || 'github_cli'
  params['note_url'] = options[:note_url] || 'https://github.com/peter-murach/github_cli'
  global_options[:params] = params
  # Need to configure client with login and password
      = TTY.shell.ask("login: ").read_string.strip!
  password = TTY.shell.ask("password: ").read_password.strip!

  global_options['login']    = 
  global_options['password'] = password
  global_options['quiet']    = options[:quiet]

  res   = self.invoke("auth:create", [], global_options)
  token = res.body['token']

  config = GithubCLI.config
  config.location = options[:local] ? 'local' : 'global'
  config['user.login']    = 
  config['user.password'] = password
  config['user.token']    = token

  GithubCLI.ui.warn <<-EOF
    \nYour #{GithubCLI.config.location} configuration file has been overwritten!
  EOF
end

#config(*args) ⇒ Object


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/github_cli/cli.rb', line 171

def config(*args)
  name, value = args.shift, args.shift
  GithubCLI.config.location = options[:local] ? 'local' : 'global'

  if !File.exists?(GithubCLI.config.path)
    GithubCLI.ui.error "Configuration file does not exist. Please use `#{GithubCLI.executable_name} init` to create one."
    exit 1
  end

  if options[:list]
    Terminal.print_config(name) && return
  elsif options[:edit]
    editor = Editor.new GithubCLI.config.path
    editor.open && return
  end

  if !name
    Terminal.print_config && return
  end

  if !value
    Terminal.line GithubCLI.config[name]
  else
    Terminal.line GithubCLI.config[name] = value
  end

  return
end

#init(filename = nil) ⇒ Object


140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/github_cli/cli.rb', line 140

def init(filename=nil)
  config_filename = filename ? filename : options[:filename]
  config = GithubCLI.config
  config.filename = config_filename
  config.location = options[:local] ? 'local' : 'global'

  if File.exists?(GithubCLI.config.path) && !options[:force]
    GithubCLI.ui.error "Not overwritting existing config file #{GithubCLI.config.path}, use --force to override."
    exit 1
  end

  config.save(defaults)
  GithubCLI.ui.confirm "Writing new configuration file to #{GithubCLI.config.path}"
end

#list(pattern = "") ⇒ Object


201
202
203
204
# File 'lib/github_cli/cli.rb', line 201

def list(pattern="")
  pattern = /^#{pattern}.*$/i
  Terminal.print_commands pattern
end

#versionObject


207
208
209
# File 'lib/github_cli/cli.rb', line 207

def version
  GithubCLI.ui.info "#{GithubCLI.program_name} #{GithubCLI::VERSION}"
end

#whoamiObject


121
122
123
124
125
# File 'lib/github_cli/cli.rb', line 121

def whoami
  config = GithubCLI.config
  me = config['user.login'] || "Not authed. Run 'gcli authorize'"
  GithubCLI.ui.info me
end