Sha256: 27f2c125e9da50bd3ee9a373f37b15f83b9cf0d1aeda86a0cc5c4b5203973a84

Contents?: true

Size: 1 KB

Versions: 17

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

module Conductor
  # Command runner
  class Command
    attr_reader :args, :path

    def initialize(command)
      parts = Shellwords.split(command)
      self.path = parts[0]
      self.args = parts[1..].join(" ")
    end

    def path=(path)
      @path = if %r{^[%/]}.match?(path)
        File.expand_path(path)
      else
        which = TTY::Which.which(path)
        which || path
      end
    end

    def args=(array)
      @args = if array.is_a?(Array)
        array.join(" ")
      else
        array
      end
    end

    def run
      stdin = Conductor.stdin

      raise "Command path not found" unless @path

      use_stdin = true
      if /\$\{?file\}?/.match?(args)
        use_stdin = false
        args.sub!(/\$\{?file\}?/, %("#{Env.env[:filepath]}"))
      else
        raise "No input" unless stdin

      end

      if use_stdin
        `echo #{Shellwords.escape(stdin)} | #{Env} #{path} #{args}`
      else
        `#{Env} #{path} #{args}`
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
marked-conductor-1.0.27 lib/conductor/command.rb
marked-conductor-1.0.26 lib/conductor/command.rb
marked-conductor-1.0.25 lib/conductor/command.rb
marked-conductor-1.0.24 lib/conductor/command.rb
marked-conductor-1.0.23 lib/conductor/command.rb
marked-conductor-1.0.22 lib/conductor/command.rb
marked-conductor-1.0.21 lib/conductor/command.rb
marked-conductor-1.0.20 lib/conductor/command.rb
marked-conductor-1.0.19 lib/conductor/command.rb
marked-conductor-1.0.18 lib/conductor/command.rb
marked-conductor-1.0.17 lib/conductor/command.rb
marked-conductor-1.0.16 lib/conductor/command.rb
marked-conductor-1.0.12 lib/conductor/command.rb
marked-conductor-1.0.11 lib/conductor/command.rb
marked-conductor-1.0.10 lib/conductor/command.rb
marked-conductor-1.0.9 lib/conductor/command.rb
marked-conductor-1.0.8 lib/conductor/command.rb