piotrmurach/tty-command

Allow tty-logger to interop with tty-command

amyspark opened this issue · 2 comments

Are you in the right place?

  • For issues or feature requests file a GitHub issue in this repository
  • For general questions or discussion post in Gitter

Describe the problem

tty-command should allow tty-logger's Loggers to be used in Command.new.

Steps to reproduce the problem

#!/usr/bin/env ruby
# frozen_string_literal: true

require 'tty-command'
require 'tty-logger'
require 'tty-option'

# rubocop disable Style/Documentation
class Demo
  include TTY::Option

  def initialize
    @logger = TTY::Logger.new do |config|
      config.metadata = %i[date time]
      config.level = :debug
    end
    @cmd = TTY::Command.new(printer: @logger)
  end

  def demo
    @logger.info "running demo"

    @cmd.run 'cat', '/etc/hosts'
  end

  def run!
    parse
    demo
  rescue StandardError => e
    @logger.fatal 'Error:', e
  end
end

Demo.new.run!

Actual behaviour

Traceback (most recent call last):
        7: from _scripts/demo-1.rb:46:in `<main>'
        6: from _scripts/demo-1.rb:46:in `new'
        5: from _scripts/demo-1.rb:22:in `initialize'
        4: from _scripts/demo-1.rb:22:in `new'
        3: from /home/amalia/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/tty-command-0.9.0/lib/tty/command.rb:60:in `initialize'
        2: from /home/amalia/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/tty-command-0.9.0/lib/tty/command.rb:197:in `use_printer'
        1: from /home/amalia/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/tty-command-0.9.0/lib/tty/command.rb:208:in `find_printer_class'
/home/amalia/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/tty-command-0.9.0/lib/tty/command.rb:208:in `const_defined?': wrong constant name #<tty::logger:0x0000560318e216b8> (NameError)

Expected behaviour

The command's output should go to the Logger.

Describe your environment

  • OS version: 5.4.34-1-MANJARO
  • Ruby version: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]
  • TTY version: tty-command (0.9.0), tty-logger (0.3.0)

Thank you for reporting this and using the tty gems!

Agree, this should work out of the box. Though I'm not sure in this instance whether tty-logger should be supplied via the :output option rather than :printer. Would you have time to submit PR to tty-logger repo to provide the bridge?

@piotrmurach I can try!