Skip to content

Commit 1ca5930

Browse files
committed
Restore directory flag
1 parent ad2c946 commit 1ca5930

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

cli/ssh.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (r *RootCmd) ssh() *clibase.Cmd {
5050
identityAgent string
5151
wsPollInterval time.Duration
5252
noWait bool
53-
logFilePath string
53+
logDirPath string
5454
)
5555
client := new(codersdk.Client)
5656
cmd := &clibase.Cmd{
@@ -73,11 +73,33 @@ func (r *RootCmd) ssh() *clibase.Cmd {
7373
logger.Error(ctx, "command exit", slog.Error(retErr))
7474
}
7575
}()
76-
if logFilePath != "" {
77-
logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
76+
if logDirPath != "" {
77+
nonce, err := cryptorand.StringCharset(cryptorand.Lower, 5)
7878
if err != nil {
79-
return xerrors.Errorf("error opening %s for logging: %w", logFilePath, err)
79+
panic(err)
8080
}
81+
logFilePath := filepath.Join(
82+
logDirPath,
83+
fmt.Sprintf(
84+
"coder-ssh-%s-%s.log",
85+
// The time portion makes it easier to find the right
86+
// log file.
87+
time.Now().Format("20060102-150405"),
88+
// The nonce prevents collisions, as SSH invocations
89+
// frequently happen in parallel.
90+
nonce,
91+
),
92+
)
93+
logFile, err := os.OpenFile(
94+
logFilePath,
95+
os.O_CREATE|os.O_APPEND|os.O_WRONLY|os.O_EXCL,
96+
0o600,
97+
)
98+
if err != nil {
99+
return xerrors.Errorf("error opening %s for logging: %w", logDirPath, err)
100+
}
101+
defer logFile.Close()
102+
81103
logger = slog.Make(sloghuman.Sink(logFile))
82104
if r.verbose {
83105
logger = logger.Leveled(slog.LevelDebug)
@@ -359,11 +381,11 @@ func (r *RootCmd) ssh() *clibase.Cmd {
359381
Value: clibase.BoolOf(&noWait),
360382
},
361383
{
362-
Flag: "log-file",
363-
Description: "Specify the location of an SSH diagnostic log file.",
364-
Env: "CODER_SSH_LOG_FILE",
384+
Flag: "log-dir",
385+
Description: "Specify the directory containing SSH diagnostic log files.",
386+
Env: "CODER_SSH_LOG_DIR",
365387
FlagShorthand: "l",
366-
Value: clibase.StringOf(&logFilePath),
388+
Value: clibase.StringOf(&logDirPath),
367389
},
368390
}
369391
return cmd

cli/ssh_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,10 @@ func TestSSH(t *testing.T) {
411411
t.Run("FileLogging", func(t *testing.T) {
412412
t.Parallel()
413413

414-
logFile := filepath.Join(t.TempDir(), "coder-ssh.log")
414+
logDir := t.TempDir()
415415

416416
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
417-
inv, root := clitest.New(t, "ssh", workspace.Name, "-l", logFile)
417+
inv, root := clitest.New(t, "ssh", workspace.Name, "-l", logDir)
418418
clitest.SetupConfig(t, client, root)
419419
pty := ptytest.New(t).Attach(inv)
420420

@@ -441,12 +441,10 @@ func TestSSH(t *testing.T) {
441441
pty.WriteLine("exit")
442442
<-cmdDone
443443

444-
info, err := os.Stat(logFile)
445-
if err != nil {
446-
t.Fatalf("failed to find ssh logfile: %v", err)
447-
}
444+
ents, err := os.ReadDir(t.TempDir())
445+
require.NoError(t, err)
448446

449-
require.Greater(t, info.Size(), int64(0), "ssh logfile is empty")
447+
require.Len(t, ents, 1, "expected one file in logdir")
450448
})
451449
}
452450

0 commit comments

Comments
 (0)