From 13f743e26b492c3dfb5f5a2781a37e9f88e26e23 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 14 Sep 2025 16:10:35 +0200 Subject: [PATCH] profiling: inject os.Stderr instead of directly using it --- cmd/restic/global_debug.go | 24 +++++++++++++----------- cmd/restic/global_release.go | 8 ++++++-- cmd/restic/main.go | 2 +- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/cmd/restic/global_debug.go b/cmd/restic/global_debug.go index e536bffea..ff1ac3a57 100644 --- a/cmd/restic/global_debug.go +++ b/cmd/restic/global_debug.go @@ -5,9 +5,9 @@ package main import ( "fmt" + "io" "net/http" _ "net/http/pprof" - "os" "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/repository" @@ -17,7 +17,7 @@ import ( "github.com/pkg/profile" ) -func registerProfiling(cmd *cobra.Command) { +func registerProfiling(cmd *cobra.Command, stderr io.Writer) { var profiler profiler origPreRun := cmd.PersistentPreRunE @@ -27,7 +27,7 @@ func registerProfiling(cmd *cobra.Command) { return err } } - return profiler.Start(profiler.opts) + return profiler.Start(profiler.opts, stderr) } // Once https://github.com/spf13/cobra/issues/1893 is fixed, @@ -65,19 +65,21 @@ func (opts *ProfileOptions) AddFlags(f *pflag.FlagSet) { f.BoolVar(&opts.insecure, "insecure-kdf", false, "use insecure KDF settings") } -type fakeTestingTB struct{} - -func (fakeTestingTB) Logf(msg string, args ...interface{}) { - fmt.Fprintf(os.Stderr, msg, args...) +type fakeTestingTB struct { + stderr io.Writer } -func (p *profiler) Start(profileOpts ProfileOptions) error { +func (t fakeTestingTB) Logf(msg string, args ...interface{}) { + fmt.Fprintf(t.stderr, msg, args...) +} + +func (p *profiler) Start(profileOpts ProfileOptions, stderr io.Writer) error { if profileOpts.listen != "" { - fmt.Fprintf(os.Stderr, "running profile HTTP server on %v\n", profileOpts.listen) + fmt.Fprintf(stderr, "running profile HTTP server on %v\n", profileOpts.listen) go func() { err := http.ListenAndServe(profileOpts.listen, nil) if err != nil { - fmt.Fprintf(os.Stderr, "profile HTTP server listen failed: %v\n", err) + fmt.Fprintf(stderr, "profile HTTP server listen failed: %v\n", err) } }() } @@ -111,7 +113,7 @@ func (p *profiler) Start(profileOpts ProfileOptions) error { } if profileOpts.insecure { - repository.TestUseLowSecurityKDFParameters(fakeTestingTB{}) + repository.TestUseLowSecurityKDFParameters(fakeTestingTB{stderr}) } return nil diff --git a/cmd/restic/global_release.go b/cmd/restic/global_release.go index 2c4f28b13..1e1e2147c 100644 --- a/cmd/restic/global_release.go +++ b/cmd/restic/global_release.go @@ -3,8 +3,12 @@ package main -import "github.com/spf13/cobra" +import ( + "io" -func registerProfiling(_ *cobra.Command) { + "github.com/spf13/cobra" +) + +func registerProfiling(_ *cobra.Command, _ io.Writer) { // No profiling in release mode } diff --git a/cmd/restic/main.go b/cmd/restic/main.go index a82d92af3..f373a418d 100644 --- a/cmd/restic/main.go +++ b/cmd/restic/main.go @@ -100,7 +100,7 @@ The full documentation can be found at https://restic.readthedocs.io/ . registerDebugCommand(cmd) registerMountCommand(cmd) registerSelfUpdateCommand(cmd) - registerProfiling(cmd) + registerProfiling(cmd, os.Stderr) return cmd }