ShogunPanda/clavem

View on GitHub
bin/clavem

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# encoding: utf-8
#
# This file is part of the clavem gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
# Licensed under the MIT license, which can be found at https://choosealicense.com/licenses/mit.
#

require "bovem"
require "clavem"

Bovem::Application.create do
  localizer = Bovem::I18n.new(root: "clavem", path: ::Pathname.new(::File.dirname(__FILE__)).to_s + "/../locales/")
  name "Clavem"
  description localizer.application_description
  version Clavem::Version::STRING

  option(:url, [], {type: String, help: localizer.application_help_url, meta: "URL", required: true})
  option(:host, [:i, "host"], {type: String, help: localizer.application_help_host, meta: "HOST", default: "localhost"})
  option(:port, [], {type: Integer, help: localizer.application_help_port, meta: localizer.application_meta_port, default: 7772})
  option(:command, [], {type: String, help: localizer.application_help_command, meta: localizer.application_meta_command, default: "open \"{{URL}}\""})
  option(:skip_callback, [:C, "skip-callback"], {help: localizer.application_help_no_callback})
  option(:timeout, [:n, "timeout"], {type: Integer, help: localizer.application_help_timeout, meta: "TIMEOUT"})
  option(:quiet, [], {help: localizer.application_help_quiet})

  action do |command|
    opts = command.get_options
    quiet = opts["quiet"]
    skip_callback = opts["skip_callback"]

    authorizer = Clavem::Authorizer.instance(host: opts["host"], port: opts["port"], command: opts["command"], timeout: opts["timeout"])

    command.console.info(localizer.cli_obtaining(opts["url"])) unless quiet
    command.console.task(!quiet ? localizer.cli_running(authorizer.callback_url) : nil) do
      begin
        authorizer.authorize(opts["url"], !skip_callback) ? :ok : :fail
      rescue Clavem::Exceptions::Failure => e
        command.console.error(e.message)
      end
    end

    if authorizer.succeeded?
      command.console.info(localizer.cli_succeeded(authorizer.token)) unless quiet
    else
      command.console.error(localizer.cli_failed)
    end
  end
end