-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcmdline.go
More file actions
296 lines (244 loc) · 6.62 KB
/
cmdline.go
File metadata and controls
296 lines (244 loc) · 6.62 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
)
// Commands for this program
const (
CMD_NONE = iota
CMD_INFO
CMD_PUTTY
CMD_SSH
CMD_INLINE_SSH
CMD_WINSSH
CMD_EXEC
CMD_UPLOAD
CMD_DOWNLOAD
CMD_BITVISE
CMD_WINSCP
CMD_BENCHMARK_DOWNLOAD
CMD_BENCHMARK_UPLOAD
)
func process_cmdline() {
config.Command = CMD_NONE
if len(os.Args) < 2 {
cmd_help()
os.Exit(0)
}
for _, arg := range os.Args {
if arg == "-help" || arg == "--help" {
cmd_help()
os.Exit(0)
}
}
var args []string
for x := 1; x < len(os.Args); x++ {
arg := os.Args[x]
if arg == "-help" || arg == "--help" {
cmd_help()
os.Exit(0)
}
if arg == "-debug" || arg == "--debug" {
config.Debug = true
continue
}
if arg == "-adc" || arg == "--adc" {
config.Flags.Adc = true
continue
}
if arg == "-auth" || arg == "--auth" {
config.Flags.Auth = true
continue
}
if arg == "-login" || arg == "--login" {
fmt.Println("index:", x)
fmt.Println("count:", len(os.Args))
if x == len(os.Args) - 1 {
fmt.Println("Error: Missing email address to --login")
os.Exit(1)
}
config.Flags.Login = os.Args[x + 1]
config.Flags.Auth = true
x++
continue
}
if strings.HasPrefix(arg, "-login=") {
p := arg[7:]
config.Flags.Login = p
config.Flags.Auth = true
continue
}
if strings.HasPrefix(arg, "--login=") {
p := arg[8:]
config.Flags.Login = p
config.Flags.Auth = true
continue
}
// WINSCP args
if strings.HasPrefix(arg, "/rawsettings") {
// config.sshFlags = append(config.sshFlags, os.Args[x:]...)
config.WinscpFlags = os.Args[x:]
break
}
if strings.HasPrefix(arg, "-") {
fmt.Println("Error: Unknown option: " + arg)
os.Exit(1)
}
args = append(args, arg)
}
// fmt.Println("Debug:", config.Debug)
// fmt.Println("Auth:", config.Flags.Auth)
// fmt.Println("Login:", config.Flags.Login)
if len(args) == 0 {
cmd_help()
os.Exit(0)
}
for x := 0; x < len(args); x++ {
arg := args[x]
switch arg {
case "info":
config.Command = CMD_INFO
case "putty":
if isWindows() == true {
config.Command = CMD_PUTTY
} else {
fmt.Println("Error: This command is only supported on Windows. For Linux use ssh")
os.Exit(1)
}
case "ssh":
if isWindows() == true {
config.Command = CMD_INLINE_SSH
} else {
config.Command = CMD_SSH
}
case "bitvise":
if isWindows() == true {
config.Command = CMD_BITVISE
} else {
fmt.Println("Error: This command is only supported on Windows. For Linux use ssh")
os.Exit(1)
}
case "winscp":
if isWindows() == true {
config.Command = CMD_WINSCP
} else {
fmt.Println("Error: This command is only supported on Windows. For Linux use ssh")
os.Exit(1)
}
case "winssh":
if isWindows() == true {
config.Command = CMD_WINSSH
} else {
fmt.Println("Error: This command is only supported on Windows. For Linux use ssh")
os.Exit(1)
}
case "exec":
if len(args) < 2 {
fmt.Println("Error: expected a remote command")
os.Exit(1)
}
config.Command = CMD_EXEC
config.RemoteCommand = args[x + 1]
x++
case "download":
if len(args) < 2 {
fmt.Println("Error: expected a source file name")
os.Exit(1)
}
config.Command = CMD_DOWNLOAD
config.SrcFile = strings.ReplaceAll(args[x + 1], "\\", "/")
x++
if len(args) >= 3 {
config.DstFile = strings.ReplaceAll(args[x + 1], "\\", "/")
x++
} else {
_, file := path.Split(config.SrcFile)
config.DstFile = file
}
if config.Debug == true {
fmt.Println("SrcFile:", config.SrcFile)
fmt.Println("DstFile:", config.DstFile)
}
case "upload":
if len(args) < 2 {
fmt.Println("Error: expected a source file name")
os.Exit(1)
}
path, err := filepath.Abs(args[x + 1])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
config.Command = CMD_UPLOAD
config.SrcFile = path
x++
if len(args) >= 3 {
config.DstFile = strings.ReplaceAll(args[x + 1], "\\", "/")
x++
} else {
file := strings.ReplaceAll(config.SrcFile, "\\", "/")
_, file = filepath.Split(file)
config.DstFile = file
}
if config.Debug == true {
fmt.Println("SrcFile:", config.SrcFile)
fmt.Println("DstFile:", config.DstFile)
}
case "benchmark":
if len(args) < 2 {
fmt.Println("Error: expected download or upload option")
os.Exit(1)
}
x++
subcmd := args[x]
switch subcmd {
case "download":
config.Command = CMD_BENCHMARK_DOWNLOAD
config.benchmark_size = 128 * 1024
config.benchmark_size = 10240 * 1024
case "upload":
config.Command = CMD_BENCHMARK_UPLOAD
config.benchmark_size = 10240 * 1024
default:
fmt.Println("Error: expected a sub command (download, upload)")
os.Exit(1)
}
default:
if config.Command != CMD_NONE {
fmt.Println("Error: Unknown command line argument: ", arg)
os.Exit(1)
}
if isWindows() == true {
fmt.Println("Error: expected a command (info, putty, ssh, winssh, exec, upload, download)")
} else {
fmt.Println("Error: expected a command (info, ssh, exec, upload, download)")
}
os.Exit(1)
}
}
}
func cmd_help() {
fmt.Println("Usage: cloudshell [command]")
fmt.Println(" cloudshell - display cloudshell program help")
fmt.Println(" cloudshell info - display Cloud Shell information")
if isWindows() == true {
fmt.Println(" cloudshell putty - connect to Cloud Shell with Putty")
}
fmt.Println(" cloudshell ssh - connect to Cloud Shell with SSH")
fmt.Println(" cloudshell winssh - connect to Cloud Shell with Windows OpenSSH")
fmt.Println(" cloudshell winscp - connect to Cloud Shell with Windows WinSCP")
fmt.Println(" cloudshell bitvise - connect to Cloud Shell with Windows Bitvise")
fmt.Println(" cloudshell exec \"command\" - Execute remote command on Cloud Shell")
fmt.Println(" cloudshell upload src_file dst_file - Upload local file to Cloud Shell")
fmt.Println(" cloudshell download src_file dst_file - Download from Cloud Shell to local file")
fmt.Println(" cloudshell benchmark download - Benchmark download speed from Cloud Shell")
fmt.Println(" cloudshell benchmark upload - Benchmark upload speed from Cloud Shell")
fmt.Println("")
fmt.Println("--debug - Turn on debug output")
fmt.Println("--adc - Use Application Default Credentials - Compute Engine only")
fmt.Println("--auth - (re)Authenticate ignoring user_credentials.json")
fmt.Println("--login - Specify an email address as a login hint")
}