piotrmurach / tty-command

Execute shell commands with pretty output logging and capture stdout, stderr and exit status.
https://ttytoolkit.org
MIT License
400 stars 32 forks source link

Allow tty-logger to interop with tty-command #53

Closed amyspark closed 4 years ago

amyspark commented 4 years ago

Are you in the right place?

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

piotrmurach commented 4 years ago

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?

amyspark commented 4 years ago

@piotrmurach I can try!