5
5
"fmt"
6
6
"log/slog"
7
7
"os/exec"
8
+ "path/filepath"
8
9
"strings"
9
10
10
11
"github.com/spf13/cobra"
@@ -20,7 +21,10 @@ type Options struct {
20
21
User string
21
22
Address string
22
23
23
- IDE bool
24
+ IDE bool
25
+ Quiet bool
26
+
27
+ WithProxy func (ctx context.Context , info * SSHProxyInfo ) error
24
28
}
25
29
26
30
func NewCommand (cli labcli.CLI ) * cobra.Command {
@@ -31,6 +35,8 @@ func NewCommand(cli labcli.CLI) *cobra.Command {
31
35
Short : `Start SSH proxy to the playground's machine` ,
32
36
Args : cobra .ExactArgs (1 ),
33
37
RunE : func (cmd * cobra.Command , args []string ) error {
38
+ cli .SetQuiet (opts .Quiet )
39
+
34
40
opts .PlayID = args [0 ]
35
41
36
42
if opts .Address != "" && strings .Count (opts .Address , ":" ) != 1 {
@@ -68,10 +74,25 @@ func NewCommand(cli labcli.CLI) *cobra.Command {
68
74
false ,
69
75
`Open the playground in the IDE (only VSCode is supported at the moment)` ,
70
76
)
77
+ flags .BoolVarP (
78
+ & opts .Quiet ,
79
+ "quiet" ,
80
+ "q" ,
81
+ false ,
82
+ `Quiet mode (don't print any messages except errors)` ,
83
+ )
71
84
72
85
return cmd
73
86
}
74
87
88
+ type SSHProxyInfo struct {
89
+ User string
90
+ Machine string
91
+ ProxyHost string
92
+ ProxyPort string
93
+ IdentityFile string
94
+ }
95
+
75
96
func RunSSHProxy (ctx context.Context , cli labcli.CLI , opts * Options ) error {
76
97
p , err := cli .Client ().GetPlay (ctx , opts .PlayID )
77
98
if err != nil {
@@ -140,28 +161,7 @@ func RunSSHProxy(ctx context.Context, cli labcli.CLI, opts *Options) error {
140
161
}
141
162
}()
142
163
143
- if ! opts .IDE {
144
- cli .PrintOut ("SSH proxy is running on %s\n " , localPort )
145
- cli .PrintOut (
146
- "\n # Connect from the terminal:\n ssh -i %s/%s ssh://%s@%s:%s\n " ,
147
- cli .Config ().SSHDir , ssh .IdentityFile , opts .User , localHost , localPort ,
148
- )
149
-
150
- cli .PrintOut ("\n # Or add the following to your ~/.ssh/config:\n " )
151
- cli .PrintOut ("Host %s\n " , opts .PlayID + "-" + opts .Machine )
152
- cli .PrintOut (" HostName %s\n " , localHost )
153
- cli .PrintOut (" Port %s\n " , localPort )
154
- cli .PrintOut (" User %s\n " , opts .User )
155
- cli .PrintOut (" IdentityFile %s/%s\n " , cli .Config ().SSHDir , ssh .IdentityFile )
156
- cli .PrintOut (" StrictHostKeyChecking no\n " )
157
- cli .PrintOut (" UserKnownHostsFile /dev/null\n \n " )
158
-
159
- cli .PrintOut ("# To access the playground in Visual Studio Code:\n " )
160
- cli .PrintOut ("code --folder-uri vscode-remote://ssh-remote+%s@%s:%s%s\n \n " ,
161
- opts .User , localHost , localPort , userHomeDir (opts .User ))
162
-
163
- cli .PrintOut ("\n Press Ctrl+C to stop\n " )
164
- } else {
164
+ if opts .IDE {
165
165
cli .PrintAux ("Opening the playground in the IDE...\n " )
166
166
167
167
// Hack: SSH into the playground first - otherwise, VSCode will fail to connect for some reason.
@@ -184,8 +184,44 @@ func RunSSHProxy(ctx context.Context, cli labcli.CLI, opts *Options) error {
184
184
}
185
185
}
186
186
187
- // Wait for ctrl+c
188
- <- ctx .Done ()
187
+ if ! opts .IDE && ! opts .Quiet {
188
+ cli .PrintAux ("SSH proxy is running on %s\n " , localPort )
189
+ cli .PrintAux (
190
+ "\n # Connect from the terminal:\n ssh -i %s/%s ssh://%s@%s:%s\n " ,
191
+ cli .Config ().SSHDir , ssh .IdentityFile , opts .User , localHost , localPort ,
192
+ )
193
+
194
+ cli .PrintAux ("\n # Or add the following to your ~/.ssh/config:\n " )
195
+ cli .PrintAux ("Host %s\n " , opts .PlayID + "-" + opts .Machine )
196
+ cli .PrintAux (" HostName %s\n " , localHost )
197
+ cli .PrintAux (" Port %s\n " , localPort )
198
+ cli .PrintAux (" User %s\n " , opts .User )
199
+ cli .PrintAux (" IdentityFile %s/%s\n " , cli .Config ().SSHDir , ssh .IdentityFile )
200
+ cli .PrintAux (" StrictHostKeyChecking no\n " )
201
+ cli .PrintAux (" UserKnownHostsFile /dev/null\n \n " )
202
+
203
+ cli .PrintAux ("# To access the playground in Visual Studio Code:\n " )
204
+ cli .PrintAux ("code --folder-uri vscode-remote://ssh-remote+%s@%s:%s%s\n \n " ,
205
+ opts .User , localHost , localPort , userHomeDir (opts .User ))
206
+
207
+ cli .PrintAux ("\n Press Ctrl+C to stop\n " )
208
+ }
209
+
210
+ if opts .WithProxy != nil {
211
+ info := & SSHProxyInfo {
212
+ User : opts .User ,
213
+ Machine : opts .Machine ,
214
+ ProxyHost : localHost ,
215
+ ProxyPort : localPort ,
216
+ IdentityFile : filepath .Join (cli .Config ().SSHDir , ssh .IdentityFile ),
217
+ }
218
+ if err := opts .WithProxy (ctx , info ); err != nil {
219
+ return fmt .Errorf ("proxy callback failed: %w" , err )
220
+ }
221
+ } else {
222
+ // Wait for ctrl+c
223
+ <- ctx .Done ()
224
+ }
189
225
190
226
return nil
191
227
}
0 commit comments