Skip to content

Commit 5870861

Browse files
committed
WIP
Signed-off-by: apostasie <[email protected]>
1 parent d196c7e commit 5870861

File tree

9 files changed

+174
-125
lines changed

9 files changed

+174
-125
lines changed

Diff for: mod/tigron/go.mod

+1-6
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@ go 1.23.0
44

55
require (
66
github.com/creack/pty v1.1.24
7-
github.com/rs/zerolog v1.34.0
87
go.uber.org/goleak v1.3.0
98
golang.org/x/sync v0.11.0
109
golang.org/x/term v0.29.0
1110
)
1211

13-
require (
14-
github.com/mattn/go-colorable v0.1.13 // indirect
15-
github.com/mattn/go-isatty v0.0.19 // indirect
16-
golang.org/x/sys v0.31.0 // indirect
17-
)
12+
require golang.org/x/sys v0.31.0 // indirect

Diff for: mod/tigron/go.sum

-14
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
1-
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
21
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
32
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
43
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
54
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6-
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
7-
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
8-
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
9-
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
10-
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
11-
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
12-
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
135
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
146
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
15-
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
16-
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
17-
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
187
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
198
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
209
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
2110
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
2211
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
2312
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
24-
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
25-
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
26-
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2713
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
2814
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
2915
golang.org/x/term v0.29.0 h1:L6pJp37ocefwRRtYPKSWOWzOtWSxVajvz2ldH/xi3iU=

Diff for: mod/tigron/internal/com/command.go

+29-16
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ import (
2727
"syscall"
2828
"time"
2929

30-
"github.com/rs/zerolog"
31-
"github.com/rs/zerolog/log"
30+
"github.com/containerd/nerdctl/mod/tigron/internal/logger"
3231
)
3332

3433
const (
@@ -61,6 +60,11 @@ var (
6160
errExecutionCancelled = errors.New("command execution cancelled")
6261
)
6362

63+
type contextKey string
64+
65+
// LoggerKey defines the key to attach a logger to on the context.
66+
const LoggerKey = contextKey("logger")
67+
6468
// Result carries the resulting output of a command once it has finished.
6569
type Result struct {
6670
Environ []string
@@ -76,7 +80,7 @@ type execution struct {
7680
cancel context.CancelFunc
7781
command *exec.Cmd
7882
pipes *stdPipes
79-
log zerolog.Logger
83+
log logger.Logger
8084
err error
8185
}
8286

@@ -193,17 +197,28 @@ func (gc *Command) Run(parentCtx context.Context) error {
193197

194198
// Create a contextual command, set the logger
195199
cmd = gc.buildCommand(ctx)
196-
logg := log.Logger.With().Ctx(ctx).Str("module", "com").Str("command", cmd.String()).Logger()
200+
201+
// Get a debug-logger from the context
202+
var (
203+
log logger.Logger
204+
ok bool
205+
)
206+
207+
if log, ok = parentCtx.Value(LoggerKey).(logger.Logger); !ok {
208+
log = nil
209+
}
210+
211+
conLog := logger.NewLogger(log).Set("command", cmd.String())
197212

198213
gc.exec = &execution{
199214
context: ctx,
200215
cancel: ctxCancel,
201216
command: cmd,
202-
log: logg,
217+
log: conLog,
203218
}
204219

205220
// Prepare pipes
206-
pipes, err = newStdPipes(ctx, logg, gc.ptyStdout, gc.ptyStderr, gc.ptyStdin, gc.writers)
221+
pipes, err = newStdPipes(ctx, conLog, gc.ptyStdout, gc.ptyStderr, gc.ptyStdin, gc.writers)
207222
if err != nil {
208223
ctxCancel()
209224

@@ -223,7 +238,7 @@ func (gc *Command) Run(parentCtx context.Context) error {
223238
// Start it
224239
if err = cmd.Start(); err != nil {
225240
// On failure, can the context, wrap whatever we have and return
226-
gc.exec.log.Warn().Err(err).Msg("start failed")
241+
gc.exec.log.Log("start failed", err)
227242

228243
gc.exec.err = errors.Join(ErrFailedStarting, err)
229244

@@ -239,13 +254,11 @@ func (gc *Command) Run(parentCtx context.Context) error {
239254
// There is no good reason for this to happen, so, log it
240255
err = gc.wrap()
241256

242-
gc.exec.log.Error().
243-
Err(ctx.Err()).
244-
Err(err).
245-
Str("stdout", gc.result.Stdout).
246-
Str("stderr", gc.result.Stderr).
247-
Int("exit", gc.result.ExitCode).
248-
Send()
257+
gc.exec.log.Log("stdout", gc.result.Stdout)
258+
gc.exec.log.Log("stderr", gc.result.Stderr)
259+
gc.exec.log.Log("exitcode", gc.result.ExitCode)
260+
gc.exec.log.Log("err", err)
261+
gc.exec.log.Log("ctxerr", ctx.Err())
249262

250263
return err
251264
default:
@@ -328,7 +341,7 @@ func (gc *Command) wrap() error {
328341
if cmd.ProcessState != nil {
329342
var ok bool
330343
if status, ok = cmd.ProcessState.Sys().(syscall.WaitStatus); !ok {
331-
log.Panic().Msg("failed casting process state sys")
344+
panic("failed casting process state sys")
332345
}
333346

334347
if status.Signaled() {
@@ -415,7 +428,7 @@ func (gc *Command) buildCommand(ctx context.Context) *exec.Cmd {
415428
// Attach platform ProcAttr and get optional custom cancellation routine
416429
if cancellation := addAttr(cmd); cancellation != nil {
417430
cmd.Cancel = func() error {
418-
gc.exec.log.Trace().Msg("command cancelled")
431+
gc.exec.log.Log("command cancelled")
419432

420433
// Call the platform dependent cancellation routine
421434
return cancellation()

Diff for: mod/tigron/internal/com/command_test.go

+25-24
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ func TestFaultyDoubleRunWait(t *testing.T) {
4747
Timeout: time.Second,
4848
}
4949

50-
err := command.Run(context.Background())
50+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
5151

5252
assertive.ErrorIsNil(t, err)
5353

54-
err = command.Run(context.Background())
54+
err = command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
5555

5656
assertive.ErrorIs(t, err, com.ErrExecAlreadyStarted)
5757

@@ -73,7 +73,7 @@ func TestFaultyRunDoubleWait(t *testing.T) {
7373
Timeout: time.Second,
7474
}
7575

76-
err := command.Run(context.Background())
76+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
7777

7878
assertive.ErrorIsNil(t, err)
7979

@@ -99,11 +99,11 @@ func TestFailRun(t *testing.T) {
9999
Binary: "does-not-exist",
100100
}
101101

102-
err := command.Run(context.Background())
102+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
103103

104104
assertive.ErrorIs(t, err, com.ErrFailedStarting)
105105

106-
err = command.Run(context.Background())
106+
err = command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
107107

108108
assertive.ErrorIs(t, err, com.ErrExecAlreadyFinished)
109109

@@ -130,7 +130,7 @@ func TestBasicRunWait(t *testing.T) {
130130
Args: []string{"one"},
131131
}
132132

133-
err := command.Run(context.Background())
133+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
134134

135135
assertive.ErrorIsNil(t, err)
136136

@@ -150,7 +150,7 @@ func TestBasicFail(t *testing.T) {
150150
Args: []string{"-c", "--", "does-not-exist"},
151151
}
152152

153-
err := command.Run(context.Background())
153+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
154154

155155
assertive.ErrorIsNil(t, err)
156156

@@ -171,7 +171,7 @@ func TestWorkingDir(t *testing.T) {
171171
WorkingDir: dir,
172172
}
173173

174-
err := command.Run(context.Background())
174+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
175175

176176
assertive.ErrorIsNil(t, err)
177177

@@ -198,7 +198,7 @@ func TestEnvBlacklist(t *testing.T) {
198198
Binary: "env",
199199
}
200200

201-
err := command.Run(context.Background())
201+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
202202

203203
assertive.ErrorIsNil(t, err)
204204

@@ -214,7 +214,7 @@ func TestEnvBlacklist(t *testing.T) {
214214
EnvBlackList: []string{"FOO"},
215215
}
216216

217-
err = command.Run(context.Background())
217+
err = command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
218218

219219
assertive.ErrorIsNil(t, err)
220220

@@ -238,7 +238,8 @@ func TestEnvBlacklist(t *testing.T) {
238238
EnvBlackList: []string{"*"},
239239
}
240240

241-
err = command.Run(context.Background())
241+
err = command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
242+
242243
assertive.ErrorIsNil(t, err)
243244

244245
res, err = command.Wait()
@@ -263,7 +264,7 @@ func TestEnvAdd(t *testing.T) {
263264
EnvBlackList: []string{"BLED"},
264265
}
265266

266-
err := command.Run(context.Background())
267+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
267268

268269
assertive.ErrorIsNil(t, err)
269270

@@ -285,7 +286,7 @@ func TestStdoutStderr(t *testing.T) {
285286
Args: []string{"-c", "--", "printf onstdout; >&2 printf onstderr;"},
286287
}
287288

288-
err := command.Run(context.Background())
289+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
289290

290291
assertive.ErrorIsNil(t, err)
291292

@@ -309,7 +310,7 @@ func TestTimeoutPlain(t *testing.T) {
309310
Timeout: 1 * time.Second,
310311
}
311312

312-
err := command.Run(context.Background())
313+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
313314

314315
assertive.ErrorIsNil(t, err)
315316

@@ -336,7 +337,7 @@ func TestTimeoutDelayed(t *testing.T) {
336337
Timeout: 1 * time.Second,
337338
}
338339

339-
err := command.Run(context.Background())
340+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
340341

341342
assertive.ErrorIsNil(t, err)
342343

@@ -372,7 +373,7 @@ func TestPTYStdout(t *testing.T) {
372373

373374
command.WithPTY(false, true, false)
374375

375-
err := command.Run(context.Background())
376+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
376377

377378
assertive.ErrorIsNil(t, err)
378379

@@ -403,7 +404,7 @@ func TestPTYStderr(t *testing.T) {
403404

404405
command.WithPTY(false, false, true)
405406

406-
err := command.Run(context.Background())
407+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
407408

408409
assertive.ErrorIsNil(t, err)
409410

@@ -432,7 +433,7 @@ func TestPTYBoth(t *testing.T) {
432433

433434
command.WithPTY(true, true, true)
434435

435-
err := command.Run(context.Background())
436+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
436437

437438
assertive.ErrorIsNil(t, err)
438439

@@ -465,7 +466,7 @@ func TestWriteStdin(t *testing.T) {
465466
command.Feed(strings.NewReader("hello world\n"))
466467
command.Feed(strings.NewReader("hello again\n"))
467468

468-
err := command.Run(context.Background())
469+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
469470

470471
assertive.ErrorIsNil(t, err)
471472

@@ -500,7 +501,7 @@ func TestWritePTYStdin(t *testing.T) {
500501
command.Feed(strings.NewReader("hello world"))
501502
command.Feed(strings.NewReader("hello again"))
502503

503-
err := command.Run(context.Background())
504+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
504505

505506
assertive.ErrorIsNil(t, err)
506507

@@ -521,7 +522,7 @@ func TestSignalOnCompleted(t *testing.T) {
521522
Timeout: 3 * time.Second,
522523
}
523524

524-
err := command.Run(context.Background())
525+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
525526

526527
assertive.ErrorIsNil(t, err)
527528

@@ -546,7 +547,7 @@ func TestSignalOnCompleted(t *testing.T) {
546547
// Timeout: 3 * time.Second,
547548
// }
548549
//
549-
// err := command.Run(context.Background())
550+
// err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
550551
//
551552
// assertive.ErrorIsNil(t, err)
552553
//
@@ -580,7 +581,7 @@ func TestSignalNormal(t *testing.T) {
580581
Timeout: 3 * time.Second,
581582
}
582583

583-
err := command.Run(context.Background())
584+
err := command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
584585

585586
assertive.ErrorIsNil(t, err)
586587

@@ -605,7 +606,7 @@ func TestSignalNormal(t *testing.T) {
605606
Timeout: 3 * time.Second,
606607
}
607608

608-
err = command.Run(context.Background())
609+
err = command.Run(context.WithValue(context.Background(), com.LoggerKey, t))
609610

610611
assertive.ErrorIsNil(t, err)
611612

0 commit comments

Comments
 (0)