Sha256: 77b017f2c3715717806f4327cb15d1047f20869e52a2ea38bc5113d376b7b534

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 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 path =~ %r{^[%/]}
                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 args =~ /\$\{?file\}?/
        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

1 entries across 1 versions & 1 rubygems

Version Path
marked-conductor-1.0.7 lib/conductor/command.rb