Module: GemContribute::CLI

Defined in:
lib/gem_contribute/cli.rb,
lib/gem_contribute/cli/auth.rb,
lib/gem_contribute/cli/scan.rb,
lib/gem_contribute/cli/config.rb,
lib/gem_contribute/cli/issues.rb,
lib/gem_contribute/cli/submit.rb,
lib/gem_contribute/cli/fork_clone_branch.rb

Defined Under Namespace

Classes: Auth, Config, ForkCloneBranch, Git, Issues, Scan, Submit

Constant Summary collapse

USAGE =
<<~USAGE
  Usage: gem-contribute <command> [options]

  Commands:
    scan [path]              Summarize the contributable surface of a Gemfile.lock.
                             Path defaults to ./Gemfile.lock.
    issues <gem|all>         List open "good first issue" issues for a gem (or all gems).
    config set <key> <val>   Persist a configuration value.
    config get <key>         Print a configuration value.
    config list              Print all configuration values.
    auth login               Authenticate with GitHub via OAuth device flow.
    auth status              Show whether you're authenticated.
    auth logout              Remove the cached token for github.com.
    fix <gem>/<issue#>       Fork the gem's repo, clone the fork, branch from main.
                             (alias: fork-clone-branch)
    submit                   Push the current branch and open a pre-filled
                             PR compare page in the browser. Run from inside
                             a clone created by `fix`.

  Global options:
    --refresh                Invalidate caches before running.
    -h, --help               Show this help.
    --version                Print the version and exit.
USAGE
COMMANDS =
{
  "scan" => ->(o, e) { Scan.new(stdout: o, stderr: e, adapter: github_adapter) },
  "issues" => ->(o, e) { Issues.new(stdout: o, stderr: e, adapter: github_adapter) },
  "config" => ->(o, e) { Config.new(stdout: o, stderr: e) },
  "auth" => ->(o, e) { Auth.new(stdout: o, stderr: e) },
  "fix" => lambda { |o, e|
    ForkCloneBranch.new(stdout: o, stderr: e,
                        clone_root: GemContribute::Config.new.clone_root)
  },
  "fork-clone-branch" => lambda { |o, e|
    ForkCloneBranch.new(stdout: o, stderr: e,
                        clone_root: GemContribute::Config.new.clone_root)
  },
  "submit" => ->(o, e) { Submit.new(stdout: o, stderr: e) }
}.freeze

Class Method Summary collapse

Class Method Details

.dispatch(command, argv, stdout:, stderr:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/gem_contribute/cli.rb', line 49

def dispatch(command, argv, stdout:, stderr:)
  builder = COMMANDS[command]
  if builder.nil?
    return print_help(stdout) if [nil, "help", "-h", "--help"].include?(command)

    return unknown_command(command, stderr)
  end

  builder.call(stdout, stderr).run(argv)
end

.github_adapterObject



87
88
89
90
# File 'lib/gem_contribute/cli.rb', line 87

def github_adapter
  token = TokenStore.new.token_for("github.com")
  HostAdapters::GitHubAdapter.new(token: token)
end

.handle_global_flags!(argv, stdout:) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/gem_contribute/cli.rb', line 92

def handle_global_flags!(argv, stdout:)
  if argv.include?("--version")
    stdout.puts "gem-contribute #{GemContribute::VERSION}"
    exit 0
  end

  return unless argv.delete("--refresh")

  Cache.new.clear!
  stdout.puts "Cache cleared at #{Cache.default_root}"
end


76
77
78
79
# File 'lib/gem_contribute/cli.rb', line 76

def print_help(stdout)
  stdout.puts USAGE
  0
end

.run(argv, stdout: $stdout, stderr: $stderr) ⇒ Object

Entry point for exe/gem-contribute. Returns an integer exit status so the caller can ‘exit GemContribute::CLI.run(ARGV)`.



43
44
45
46
47
# File 'lib/gem_contribute/cli.rb', line 43

def run(argv, stdout: $stdout, stderr: $stderr)
  argv = argv.dup
  handle_global_flags!(argv, stdout: stdout)
  dispatch(argv.shift, argv, stdout: stdout, stderr: stderr)
end

.unknown_command(command, stderr) ⇒ Object



81
82
83
84
85
# File 'lib/gem_contribute/cli.rb', line 81

def unknown_command(command, stderr)
  stderr.puts "gem-contribute: unknown command #{command.inspect}"
  stderr.puts USAGE
  2
end