Sha256: f3b0c803248c23acd37e4e38730d395b2f69621c215f0e957b4ebf18c8e93db2

Contents?: true

Size: 1.52 KB

Versions: 4

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

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

    ##
    ## Instantiate a command runner
    ##
    ## @param      command  [String] The command
    ##
    def initialize(command)
      parts = Shellwords.split(command)
      self.path = parts[0]
      self.args = parts[1..].join(" ")
    end

    ##
    ## Writer method for command path
    ##
    ## @param      path  [String] The path
    ##
    ## @return     [String] New path
    ##
    def path=(path)
      @path = if %r{^[~/.]}.match?(path)
        File.expand_path(path)
      else
        which = TTY::Which.which(path)
        which || path
      end
    end

    ##
    ## Writer method for arguments
    ##
    ## @param      array  [Array] Array of arguments
    ##
    ## @return     [String] Arguments as string
    ##
    def args=(array)
      @args = if array.is_a?(Array)
        array.join(" ")
      else
        array
      end
    end

    ##
    ## Run the command
    ##
    ## @return     [String] result of running STDIN through command
    ##
    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

4 entries across 4 versions & 1 rubygems

Version Path
marked-conductor-1.0.31 lib/conductor/command.rb
marked-conductor-1.0.30 lib/conductor/command.rb
marked-conductor-1.0.29 lib/conductor/command.rb
marked-conductor-1.0.28 lib/conductor/command.rb