-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.go
More file actions
51 lines (49 loc) · 1.1 KB
/
token.go
File metadata and controls
51 lines (49 loc) · 1.1 KB
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
package ssh_client
import (
"strings"
)
// ExpandTokens expand % tokens
//
// %% A literal ‘%’.
// %h The remote hostname.
// %n The original remote hostname, as given on the command line.
// %p The remote port.
// %r The remote username.
//
// TODO: complete the list of tokens and allow selecting them depending on the key
// see: https://github.com/openssh/openssh-portable/blob/master/sshconnect.h#L54
// %c see https://github.com/openssh/openssh-portable/blob/master/readconf.c#L354
func (h *Host) ExpandTokens(in string) string {
var out string
for {
percentIdx := strings.IndexByte(in, '%')
if percentIdx == -1 {
// No %
break
}
if percentIdx+1 >= len(in) {
// Last char is %
break
}
out = out + in[:percentIdx]
token := in[percentIdx+1]
in = in[percentIdx+2:]
switch token {
case '%':
out = out + "%"
case 'h':
out = out + h.Hostname
case 'n':
out = out + h.Name
case 'p':
out = out + h.Port
case 'r':
out = out + h.User
default:
// TODO: unknown token should err?
out = out + "%" + string(token)
}
}
out = out + in
return out
}