class EacRubyUtils::Envs::SshEnv

def command_line(line)

def command_line(line)
  "#{ssh_command_line} #{Shellwords.escape(line)}"
end

def initialize(uri)

def initialize(uri)
  @uri = self.class.parse_uri(uri).freeze
end

def parse_uri(uri)

def parse_uri(uri)
  uri_by_url(uri) || uri_by_user_hostname(uri) || raise("URI has no SSH scheme: #{uri}")
end

def ssh_command_line

def ssh_command_line
  (%w[ssh] +
    %w[nodasho dasho port].flat_map { |m| send("ssh_command_line_#{m}_args") } +
    [user_hostname_uri])
    .map { |a| Shellwords.escape(a) }.join(' ')
end

def ssh_command_line_port_args

def ssh_command_line_port_args
  uri.port.if_present([]) do |v|
    ['-p', v]
  end
end

def to_s

def to_s
  uri.to_s
end

def uri_by_url(url)

def uri_by_url(url)
  r = ::Addressable::URI.parse(url)
  r.scheme == 'ssh' && r.host.present? ? r : nil
end

def uri_by_user_hostname(user_hostname)

def uri_by_user_hostname(user_hostname)
  m = USER_HOSTNAME_PATTERN.match(user_hostname)
  m ? ::Addressable::URI.new(scheme: 'ssh', host: m[2], user: m[1]) : nil
rescue Addressable::URI::InvalidURIError
  nil
end

def user_hostname_uri

def user_hostname_uri
  r = uri.host
  r = "#{uri.user}@#{r}" if uri.user.present?
  r
end