Module: GemContribute::CLI

Defined in:
lib/gem_contribute/cli.rb,
lib/gem_contribute/cli/auth.rb,
lib/gem_contribute/cli/init.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,
lib/gem_contribute/cli/rate_limit_footer.rb

Defined Under Namespace

Modules: RateLimitFooter Classes: Auth, Config, ForkCloneBranch, Git, Init, Issues, Scan, Submit

Constant Summary collapse

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

  Commands:
    init                     One-time interactive setup (sets clone_root).
    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 =
{
  "init" => ->(o, e) { Init.new(stdout: o, stderr: e) },
  "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



52
53
54
55
56
57
58
59
60
61
# File 'lib/gem_contribute/cli.rb', line 52

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



91
92
93
94
# File 'lib/gem_contribute/cli.rb', line 91

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

.handle_global_flags!(argv, stdout:) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gem_contribute/cli.rb', line 96

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


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

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)`.



46
47
48
49
50
# File 'lib/gem_contribute/cli.rb', line 46

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



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

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