Sha256: 59167fec779fd8b2a63609813ca1613e2b8d1ddc7a2a22a51afd581a1502ea0a

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

module Bard
  class RemoteCommand < Struct.new(:server, :command, :home)
    def self.run! *args
      new(*args).run!
    end

    def local_command
      uri = URI.parse("ssh://#{server.ssh}")
      ssh_key = server.ssh_key ? "-i #{server.ssh_key} " : ""
      cmd = command
      if server.env
        cmd = "#{server.env} #{command}"
      end
      unless home
        cmd = "cd #{server.path} && #{cmd}"
      end
      cmd = "ssh -tt #{ssh_key}#{"-p#{uri.port} " if uri.port}#{uri.user}@#{uri.host} '#{cmd}'"
      if server.gateway
        uri = URI.parse("ssh://#{server.gateway}")
        cmd = "ssh -tt #{" -p#{uri.port} " if uri.port}#{uri.user}@#{uri.host} \"#{cmd}\""
      end
      cmd
    end

    def run! verbose: false
      failed = false

      if verbose
        failed = !(system local_command)
      else
        stdout, stderr, status = Open3.capture3(local_command)
        failed = status.to_i.nonzero?
        if failed
          $stdout.puts stdout
          $stderr.puts stderr
        end
      end

      if failed
        raise "Running command failed: #{local_command}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bard-0.68.0 lib/bard/remote_command.rb