Skip to content

chore: pass logger to maybeDeleteFilesystem #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions envbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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")
}
}
Expand Down
4 changes: 3 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Comment on lines -41 to +43
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review: I mainly did this to avoid having to type that big long signature again

// Filesystem is the filesystem to use for all operations.
// Defaults to the host filesystem.
Filesystem billy.Filesystem
Expand Down
Loading