Sha256: a4966d209b7acee9979a709aeae60aeb69abdd9de03ea3d76fcf225ea1abd5bc

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

module Hexlet
  class BaseCLI < Thor
    include Thor::Actions

    CONFIG_DIR = File.join(Dir.home, ".hexlet")
    CREDENTIALS_FILE = File.join(Dir.home, ".hexlet", "credentials")

    class_option :verbose, type: :boolean
    class_option :host, type: :string, default: "http://hexlet.io"

    desc "login HEXLET_API_KEY", "login on hexlet.io"
    def login(key)
      client = BaseClient.new key, logger: logger
      if client.login
        puts (t :ok)
        write_config("hexlet_api_key" => key)
      else
        puts (t :not_found)
      end
    end

    no_commands do
      def logger
        logger = Logger.new(STDOUT)
        logger.level = options[:verbose] ? Logger::DEBUG : Logger::INFO
        logger
      end

      def t key
        command_name =  @_invocations.values.last.last
        ns = self.class.to_s.downcase.split("::").last
        I18n.t key, scope: [ns, command_name]
      end

      def config
        if File.file?(CREDENTIALS_FILE)
          YAML.load_file(CREDENTIALS_FILE)
        else
          {}
        end
      end

      def write_config(data)
        FileUtils.mkdir_p(CONFIG_DIR)
        File.open(CREDENTIALS_FILE, 'w') do |f|
          f.write data.to_yaml
        end
        # TODO puts (I18n.t "update_config")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hexlet-0.1.1 lib/hexlet/base_cli.rb
hexlet-0.1.0 lib/hexlet/base_cli.rb
hexlet-0.0.1 lib/hexlet/base_cli.rb