Sha256: 9226d4d1db545b9ca0fe98dca3812b74afba24ab0e88afa98b4a6666ede27eb4

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require "tty-spinner"
require "stringio"

require_relative "component_exec"

module CobraCommander
  module Executor
    # Executes a command on multiple components simultaniously
    class MultiExec
      def initialize(components)
        @components = components
      end

      def run(command, output: $stdout, spin_output: $stderr, only_output_on_error: true, **cmd_options)
        cmmd_output = StringIO.new
        multi = TTY::Spinner::Multi.new("Running #{command}", output: spin_output)
        @components.each do |component|
          component_exec(multi, component, command, only_output_on_error: only_output_on_error,
                                                    stderr: :stdout, output: cmmd_output,
                                                    **cmd_options)
        end
        multi.auto_spin
        output << cmmd_output.string
        true
      end

    private

      def component_exec(multi, component, command, **options)
        exec = ComponentExec.new(component)
        multi.register(*spinner(component.name)) do |spin|
          exec.run(command, **options) ? spin.success : spin.error
        end
      end

      def spinner(title)
        pastel = Pastel.new
        [":spinner #{title}", { format: :bouncing,
                                success_mark: pastel.green("[DONE]"),
                                error_mark: pastel.red("[ERROR]"), },]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cobra_commander-0.10.0 lib/cobra_commander/executor/multi_exec.rb
cobra_commander-0.9.2 lib/cobra_commander/executor/multi_exec.rb