Skip to content

Commit 0423cef

Browse files
authored
chore: pass logger to maybeDeleteFilesystem (#165)
1 parent 6bc058b commit 0423cef

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

envbuilder.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ func Run(ctx context.Context, options Options) error {
427427

428428
// It's possible that the container will already have files in it, and
429429
// we don't want to merge a new container with the old one.
430-
if err := maybeDeleteFilesystem(options.ForceSafe); err != nil {
430+
if err := maybeDeleteFilesystem(options.Logger, options.ForceSafe); err != nil {
431431
return nil, fmt.Errorf("delete filesystem: %w", err)
432432
}
433433

@@ -1065,21 +1065,20 @@ func findDevcontainerJSON(options Options) (string, string, error) {
10651065

10661066
// maybeDeleteFilesystem wraps util.DeleteFilesystem with a guard to hopefully stop
10671067
// folks from unwittingly deleting their entire root directory.
1068-
func maybeDeleteFilesystem(force bool) error {
1068+
func maybeDeleteFilesystem(log LoggerFunc, force bool) error {
10691069
kanikoDir, ok := os.LookupEnv("KANIKO_DIR")
10701070
if !ok || strings.TrimSpace(kanikoDir) != MagicDir {
10711071
if force {
10721072
bailoutSecs := 10
1073-
_, _ = fmt.Fprintln(os.Stderr, "WARNING! BYPASSING SAFETY CHECK! THIS WILL DELETE YOUR ROOT FILESYSTEM!")
1074-
_, _ = fmt.Fprintf(os.Stderr, "You have %d seconds to bail out", bailoutSecs)
1075-
for i := 0; i < bailoutSecs; i++ {
1076-
_, _ = fmt.Fprintf(os.Stderr, ".")
1073+
log(codersdk.LogLevelWarn, "WARNING! BYPASSING SAFETY CHECK! THIS WILL DELETE YOUR ROOT FILESYSTEM!")
1074+
log(codersdk.LogLevelWarn, "You have %d seconds to bail out!", bailoutSecs)
1075+
for i := bailoutSecs; i > 0; i-- {
1076+
log(codersdk.LogLevelWarn, "%d...", i)
10771077
<-time.After(time.Second)
10781078
}
1079-
_, _ = fmt.Fprintf(os.Stderr, "\n")
10801079
} else {
1081-
_, _ = fmt.Fprintf(os.Stderr, "KANIKO_DIR is not set to %s. Bailing!\n", MagicDir)
1082-
_, _ = fmt.Fprintln(os.Stderr, "To bypass this check, set FORCE_SAFE=true.")
1080+
log(codersdk.LogLevelError, "KANIKO_DIR is not set to %s. Bailing!\n", MagicDir)
1081+
log(codersdk.LogLevelError, "To bypass this check, set FORCE_SAFE=true.")
10831082
return errors.New("safety check failed")
10841083
}
10851084
}

options.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"github.com/go-git/go-billy/v5"
77
)
88

9+
type LoggerFunc func(level codersdk.LogLevel, format string, args ...interface{})
10+
911
// Options contains the configuration for the envbuilder.
1012
type Options struct {
1113
SetupScript string
@@ -38,7 +40,7 @@ type Options struct {
3840
ExportEnvFile string
3941
PostStartScriptPath string
4042
// Logger is the logger to use for all operations.
41-
Logger func(level codersdk.LogLevel, format string, args ...interface{})
43+
Logger LoggerFunc
4244
// Filesystem is the filesystem to use for all operations.
4345
// Defaults to the host filesystem.
4446
Filesystem billy.Filesystem

0 commit comments

Comments
 (0)