From 70148a9ebb2a67fcc8578fa170914f0503b81ced Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Wed, 1 May 2024 10:12:20 +0000 Subject: [PATCH] chore: pass logger to maybeDeleteFilesystem --- envbuilder.go | 17 ++++++++--------- options.go | 4 +++- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/envbuilder.go b/envbuilder.go index 92ffc84f..334450da 100644 --- a/envbuilder.go +++ b/envbuilder.go @@ -427,7 +427,7 @@ func Run(ctx context.Context, options Options) error { // It's possible that the container will already have files in it, and // we don't want to merge a new container with the old one. - if err := maybeDeleteFilesystem(options.ForceSafe); err != nil { + if err := maybeDeleteFilesystem(options.Logger, options.ForceSafe); err != nil { return nil, fmt.Errorf("delete filesystem: %w", err) } @@ -1065,21 +1065,20 @@ func findDevcontainerJSON(options Options) (string, string, error) { // maybeDeleteFilesystem wraps util.DeleteFilesystem with a guard to hopefully stop // folks from unwittingly deleting their entire root directory. -func maybeDeleteFilesystem(force bool) error { +func maybeDeleteFilesystem(log LoggerFunc, force bool) error { kanikoDir, ok := os.LookupEnv("KANIKO_DIR") if !ok || strings.TrimSpace(kanikoDir) != MagicDir { if force { bailoutSecs := 10 - _, _ = fmt.Fprintln(os.Stderr, "WARNING! BYPASSING SAFETY CHECK! THIS WILL DELETE YOUR ROOT FILESYSTEM!") - _, _ = fmt.Fprintf(os.Stderr, "You have %d seconds to bail out", bailoutSecs) - for i := 0; i < bailoutSecs; i++ { - _, _ = fmt.Fprintf(os.Stderr, ".") + log(codersdk.LogLevelWarn, "WARNING! BYPASSING SAFETY CHECK! THIS WILL DELETE YOUR ROOT FILESYSTEM!") + log(codersdk.LogLevelWarn, "You have %d seconds to bail out!", bailoutSecs) + for i := bailoutSecs; i > 0; i-- { + log(codersdk.LogLevelWarn, "%d...", i) <-time.After(time.Second) } - _, _ = fmt.Fprintf(os.Stderr, "\n") } else { - _, _ = fmt.Fprintf(os.Stderr, "KANIKO_DIR is not set to %s. Bailing!\n", MagicDir) - _, _ = fmt.Fprintln(os.Stderr, "To bypass this check, set FORCE_SAFE=true.") + log(codersdk.LogLevelError, "KANIKO_DIR is not set to %s. Bailing!\n", MagicDir) + log(codersdk.LogLevelError, "To bypass this check, set FORCE_SAFE=true.") return errors.New("safety check failed") } } diff --git a/options.go b/options.go index 792f4e1a..134fdca5 100644 --- a/options.go +++ b/options.go @@ -6,6 +6,8 @@ import ( "github.com/go-git/go-billy/v5" ) +type LoggerFunc func(level codersdk.LogLevel, format string, args ...interface{}) + // Options contains the configuration for the envbuilder. type Options struct { SetupScript string @@ -38,7 +40,7 @@ type Options struct { ExportEnvFile string PostStartScriptPath string // Logger is the logger to use for all operations. - Logger func(level codersdk.LogLevel, format string, args ...interface{}) + Logger LoggerFunc // Filesystem is the filesystem to use for all operations. // Defaults to the host filesystem. Filesystem billy.Filesystem