-
Notifications
You must be signed in to change notification settings - Fork 4
/
scp_uri.rb
101 lines (84 loc) · 2.38 KB
/
scp_uri.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require 'addressable/uri'
require 'gitable/uri'
module Gitable
class ScpURI < Gitable::URI
##
# Deprecated: This serves no purpose. You might as well just parse the URI.
def self.scp?(uri)
$stderr.puts "DEPRECATED: Gitable::ScpURI.scp?. You're better off parsing the URI and checking #scp?."
Gitable::URI.parse(uri).scp?
end
##
# Deprecated: This serves no purpose. Just use Gitable::URI.parse.
def self.parse(uri)
$stderr.puts "DEPRECATED: Gitable::ScpURI.parse just runs Gitable::URI.parse. Please use this directly."
Gitable::URI.parse(uri)
end
# Keep URIs like this as they were input:
#
# git@github.com:martinemde/gitable.git
#
# Without breaking URIs like these:
#
# git@host.com:/home/martinemde/gitable.git
#
# @param [String] new_path The new path to be set.
# @return [String] The same path passed in.
def path=(new_path)
super
if new_path[0] != '/' # addressable adds a / but scp-style uris are altered by this behavior
@path = path.delete_prefix("/")
@normalized_path = nil
validate
end
path
end
# Get the URI as a string in the same form it was input.
#
# Taken from Addressable::URI.
#
# @return [String] The URI as a string.
def to_s
@uri_string ||= "#{normalized_authority}:#{normalized_path}".force_encoding(Encoding::UTF_8)
end
alias to_str to_s
# Return the actual scheme even though we don't show it
#
# @return [String] always 'ssh' for scp style URIs
def inferred_scheme
'ssh'
end
# Scp style URIs are always ssh
#
# @return [true] always ssh
def ssh?
true
end
# Is this an scp formatted uri? (Yes, always)
#
# @return [true] always scp formatted uri
def scp?
true
end
protected
def validate
return if @validation_deferred
if host.nil? || host.empty?
invalid! "Hostname segment missing"
end
if scheme && !scheme.empty?
invalid! "Scp style URI must not have a scheme"
end
if port
invalid! "Scp style URI cannot have a port"
end
if path.nil? || path.empty?
invalid! "Absolute URI missing hierarchical segment"
end
nil
end
def invalid!(reason)
raise InvalidURIError, "#{reason}: '#{to_s}'"
end
end
end