Skip to content

Commit 4b3b2bd

Browse files
committed
feat(cli/ssh): simplify log file flags
1 parent fa8f50a commit 4b3b2bd

File tree

3 files changed

+15
-43
lines changed

3 files changed

+15
-43
lines changed

cli/ssh.go

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"net/url"
1111
"os"
1212
"os/exec"
13-
"path"
1413
"path/filepath"
1514
"strings"
1615
"time"
@@ -50,8 +49,7 @@ func (r *RootCmd) ssh() *clibase.Cmd {
5049
identityAgent string
5150
wsPollInterval time.Duration
5251
noWait bool
53-
logDir string
54-
logToFile bool
52+
logFilePath string
5553
)
5654
client := new(codersdk.Client)
5755
cmd := &clibase.Cmd{
@@ -74,21 +72,10 @@ func (r *RootCmd) ssh() *clibase.Cmd {
7472
logger.Error(ctx, "command exit", slog.Error(retErr))
7573
}
7674
}()
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)
8577
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)
9279
}
9380
logger = slog.Make(sloghuman.Sink(logFile))
9481
defer logFile.Close()
@@ -354,18 +341,11 @@ func (r *RootCmd) ssh() *clibase.Cmd {
354341
Value: clibase.BoolOf(&noWait),
355342
},
356343
{
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",
365347
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),
369349
},
370350
}
371351
return cmd

cli/ssh_test.go

Lines changed: 5 additions & 10 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-
dir := t.TempDir()
414+
logFile := filepath.Join(t.TempDir(), "coder-ssh.log")
415415

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

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

444-
entries, err := os.ReadDir(dir)
445-
require.NoError(t, err)
446-
for _, e := range entries {
447-
t.Logf("logdir entry: %s", e.Name())
448-
if strings.HasPrefix(e.Name(), "coder-ssh") {
449-
return
450-
}
444+
_, err := os.Stat(logFile)
445+
if err != nil {
446+
t.Fatalf("failed to find ssh logfile: %v", err)
451447
}
452-
t.Fatal("failed to find ssh logfile")
453448
})
454449
}
455450

cli/testdata/coder_ssh_--help.golden

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ Start a shell into a workspace
1818
Specifies which identity agent to use (overrides $SSH_AUTH_SOCK),
1919
forward agent must also be enabled.
2020

21-
--log-dir string, $CODER_SSH_LOG_DIR (default: /tmp)
22-
Specify the location for the log files.
23-
24-
-l, --log-to-file bool, $CODER_SSH_LOG_TO_FILE
25-
Enable diagnostic logging to file.
21+
-l, --log-file string, $CODER_SSH_LOG_FILE
22+
Specify the location of an SSH diagnostic log file.
2623

2724
--no-wait bool, $CODER_SSH_NO_WAIT
2825
Specifies whether to wait for a workspace to become ready before

0 commit comments

Comments
 (0)