@@ -10,7 +10,6 @@ import (
10
10
"net/url"
11
11
"os"
12
12
"os/exec"
13
- "path"
14
13
"path/filepath"
15
14
"strings"
16
15
"time"
@@ -50,8 +49,7 @@ func (r *RootCmd) ssh() *clibase.Cmd {
50
49
identityAgent string
51
50
wsPollInterval time.Duration
52
51
noWait bool
53
- logDir string
54
- logToFile bool
52
+ logFilePath string
55
53
)
56
54
client := new (codersdk.Client )
57
55
cmd := & clibase.Cmd {
@@ -74,21 +72,10 @@ func (r *RootCmd) ssh() *clibase.Cmd {
74
72
logger .Error (ctx , "command exit" , slog .Error (retErr ))
75
73
}
76
74
}()
77
- if logToFile {
78
- // we need a way to ensure different ssh invocations don't clobber
79
- // each other's logs. Date-time strings will likely have collisions
80
- // in unit tests and/or scripts unless we extend precision out to
81
- // sub-millisecond, which seems unwieldy. A simple 5-character random
82
- // string will do it, since the operating system already tracks
83
- // dates and times for file IO.
84
- qual , err := cryptorand .String (5 )
75
+ if logFilePath != "" {
76
+ logFile , err := os .OpenFile (logFilePath , os .O_CREATE | os .O_APPEND | os .O_WRONLY , 0o600 )
85
77
if err != nil {
86
- return xerrors .Errorf ("generate random qualifier: %w" , err )
87
- }
88
- logPth := path .Join (logDir , fmt .Sprintf ("coder-ssh-%s.log" , qual ))
89
- logFile , err := os .Create (logPth )
90
- if err != nil {
91
- return xerrors .Errorf ("error opening %s for logging: %w" , logPth , err )
78
+ return xerrors .Errorf ("error opening %s for logging: %w" , logFilePath , err )
92
79
}
93
80
logger = slog .Make (sloghuman .Sink (logFile ))
94
81
defer logFile .Close ()
@@ -354,18 +341,11 @@ func (r *RootCmd) ssh() *clibase.Cmd {
354
341
Value : clibase .BoolOf (& noWait ),
355
342
},
356
343
{
357
- Flag : "log-dir" ,
358
- Default : os .TempDir (),
359
- Description : "Specify the location for the log files." ,
360
- Env : "CODER_SSH_LOG_DIR" ,
361
- Value : clibase .StringOf (& logDir ),
362
- },
363
- {
364
- Flag : "log-to-file" ,
344
+ Flag : "log-file" ,
345
+ Description : "Specify the location of an SSH diagnostic log file." ,
346
+ Env : "CODER_SSH_LOG_FILE" ,
365
347
FlagShorthand : "l" ,
366
- Env : "CODER_SSH_LOG_TO_FILE" ,
367
- Description : "Enable diagnostic logging to file." ,
368
- Value : clibase .BoolOf (& logToFile ),
348
+ Value : clibase .StringOf (& logFilePath ),
369
349
},
370
350
}
371
351
return cmd
0 commit comments