Sha256: 7364bf72b8c599fdc60f4433fac595e7d9b6e955baebd73b7eb08f48905c8390

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

require "ggem/version"
require "ggem/cli/clirb"
require "ggem/cli/commands"

module GGem; end

class GGem::CLI
  COMMANDS = CommandSet.new{ |unknown| InvalidCommand.new(unknown) }.tap do |c|
    c.add(GenerateCommand, "generate", "g")
    c.add(BuildCommand,    "build",    "b")
    c.add(InstallCommand,  "install",  "i")
    c.add(PushCommand,     "push",     "p")
    c.add(TagCommand,      "tag",      "t")
    c.add(ReleaseCommand,  "release",  "r")
  end

  def self.run(args)
    new.run(args)
  end

  def initialize(kernel = nil, stdout = nil, stderr = nil)
    @kernel = kernel || Kernel
    @stdout = stdout || $stdout
    @stderr = stderr || $stderr
  end

  def run(args)
    begin
      cmd_name = args.shift
      cmd = COMMANDS[cmd_name]
      cmd.run(args)
    rescue CLIRB::HelpExit
      @stdout.puts cmd.help
    rescue CLIRB::VersionExit
      @stdout.puts GGem::VERSION
    rescue CLIRB::Error, ArgumentError, InvalidCommandError => ex
      display_debug(ex)
      @stderr.puts "#{ex.message}\n\n"
      @stdout.puts cmd.help
      @kernel.exit 1
    rescue CommandExitError
      @kernel.exit 1
    rescue => ex
      @stderr.puts "#{ex.class}: #{ex.message}"
      @stderr.puts ex.backtrace.join("\n")
      @kernel.exit 1
    end
    @kernel.exit 0
  end

  private

  def display_debug(exception)
    if ENV["DEBUG"]
      @stderr.puts "#{exception.class}: #{exception.message}"
      @stderr.puts exception.backtrace.join("\n")
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ggem-1.10.3 lib/ggem/cli.rb
ggem-1.10.2 lib/ggem/cli.rb
ggem-1.10.1 lib/ggem/cli.rb
ggem-1.10.0 lib/ggem/cli.rb