-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathargs.go
More file actions
190 lines (177 loc) · 4.45 KB
/
Copy pathargs.go
File metadata and controls
190 lines (177 loc) · 4.45 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package sshpass
import "strings"
// CommandType represents the command type.
type CommandType int
const (
CommandSSH CommandType = iota // ssh session
CommandSCP // scp file transfer
CommandRsync // rsync file transfer
)
// ParseSSHArgs parses ssh-style arguments (user@host or -p port user@host).
// It returns a Config populated from the arguments and any trailing command
// string.
func ParseSSHArgs(args []string) (*Config, string) {
config := NewConfig()
config.Port = "" // clear default; only set if -p is explicitly used
var command string
i := 0
for i < len(args) {
arg := args[i]
if arg == "ssh" {
// skip the ssh command itself
i++
continue
}
if arg == "-p" && i+1 < len(args) {
config.Port = args[i+1]
i += 2
continue
}
if arg == "-i" && i+1 < len(args) {
config.KeyPath = args[i+1]
i += 2
continue
}
if arg == "-o" && i+1 < len(args) {
// skip ssh options like StrictHostKeyChecking=no
i += 2
continue
}
if arg == "-v" {
// skip verbose flag (not supported natively)
i++
continue
}
if arg == "--bg" || arg == "-bg" {
// run the command detached from the session (see BackgroundCommand)
config.Background = true
i++
continue
}
if strings.Contains(arg, "@") {
// user@host format (supports IPv6)
parts := strings.SplitN(arg, "@", 2)
if len(parts) == 2 {
config.User = parts[0]
config.Host = parts[1]
}
i++
continue
}
// Bare host name, e.g. "ssh esxi": the first token before any user@host
// is taken as the host and resolved by the OS. Tokens containing
// whitespace are left alone — they are far more likely to be a command
// than a host name.
if config.Host == "" && !strings.HasPrefix(arg, "-") && !strings.ContainsAny(arg, " \t") {
config.Host = arg
i++
continue
}
// remaining args as command
if config.Host != "" {
command = JoinArgs(args[i:])
break
}
i++
}
return config, command
}
// ParseSCPArgs parses scp command arguments. It returns a Config populated from
// scp-specific flags and the remaining scp arguments (including any
// user@host:path token).
func ParseSCPArgs(args []string) (*Config, []string) {
config := NewConfig()
config.User = "" // clear default; only set if user@host:path is found
config.Port = "" // clear default; only set if -P is explicitly used
var scpArgs []string
i := 0
for i < len(args) {
arg := args[i]
if arg == "scp" {
i++
continue
}
if arg == "-P" && i+1 < len(args) {
// scp uses uppercase -P for port
config.Port = args[i+1]
i += 2
continue
}
if arg == "-i" && i+1 < len(args) {
config.KeyPath = args[i+1]
i += 2
continue
}
if arg == "-o" && i+1 < len(args) {
i += 2
continue
}
if arg == "-r" || arg == "-q" || arg == "-C" || arg == "-v" {
// flags handled natively by SFTP implementation: recursive, quiet, compression, verbose
i++
continue
}
if strings.Contains(arg, "@") && strings.Contains(arg, ":") {
config.SetUserHostFromArg(arg)
}
scpArgs = append(scpArgs, arg)
i++
}
return config, scpArgs
}
// ParseRsyncArgs parses rsync command arguments. It returns a Config populated
// from rsync-specific flags and the remaining rsync arguments (including any
// user@host:path token).
func ParseRsyncArgs(args []string) (*Config, []string) {
config := NewConfig()
config.User = "" // clear default; only set if user@host:path is found
config.Port = "" // clear default; only set if --port= is explicitly used
var rsyncArgs []string
i := 0
for i < len(args) {
arg := args[i]
if arg == "rsync" {
i++
continue
}
if arg == "-e" && i+1 < len(args) {
// skip -e ssh option
i += 2
continue
}
if strings.HasPrefix(arg, "--rsh=") {
// skip --rsh=ssh option
i++
continue
}
if strings.HasPrefix(arg, "--port=") {
// --port=N format (rsync uses -p for --perms, not port)
portPart := arg[len("--port="):]
if isAllDigits(portPart) {
config.Port = portPart
}
i++
continue
}
if strings.Contains(arg, "@") && strings.Contains(arg, ":") {
config.SetUserHostFromArg(arg)
}
rsyncArgs = append(rsyncArgs, arg)
i++
}
return config, rsyncArgs
}
// DetectCommandType detects the command type from the leading argument.
func DetectCommandType(args []string) CommandType {
if len(args) == 0 {
return CommandSSH
}
switch args[0] {
case "scp":
return CommandSCP
case "rsync":
return CommandRsync
default:
return CommandSSH
}
}