cli/bin/peas

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
require 'gli'

root_path = File.join(File.dirname(__FILE__), "../")
$LOAD_PATH.unshift(root_path)
require 'lib/peas'

@api = API.new

include GLI::App
reset # Used when this file is repetitively loaded by rspec

program_desc 'CLI client for Peas'

version Peas::VERSION

# Load all the commands
Dir["#{root_path}/lib/peas/commands/**/*.rb"].each { |f| load f }

pre do |_global, _command, _options, _args|
  # Pre logic here
  # Return true to proceed; false to abort and not call the
  # chosen command
  # Use skips_pre before a command to skip this block
  # on that command only
  true
end

post do |_global, _command, _options, _args|
  # Post logic here
  # Use skips_post before a command to skip this
  # block on that command only
end

on_error do |exception|
  # Show a custom error message with backtrace if it's not a GLI error
  if exception.class.to_s.split('::').first != 'GLI'
    if [Errno::ECONNREFUSED, SocketError].include? exception.class
      Peas.error_message "Couldn't connect to the Peas API. The current endpoint is '#{Peas.api_domain}', make " \
        "sure it is up and accessible."
    else
      Peas.error_message "Unexpected error: #{exception.class}"
      Peas.error_message "#{exception.message} @ #{exception.backtrace.last}"
    end
  end
  # GLI needs this to do its default error reporting things
  true
end

# Exit 0 if sent SIGINT
trap('INT') {
  exit!
}

exit_status = run(ARGV)
exit exit_status if ENV['GLI_ENV'] != 'test'