Skip to content
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

dev: refactor prompts for autofix permission #128211

Merged
merged 2 commits into from
Aug 2, 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
2 changes: 1 addition & 1 deletion dev
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fi
set -euo pipefail

# Bump this counter to force rebuilding `dev` on all machines.
DEV_VERSION=97
DEV_VERSION=98

THIS_DIR=$(cd "$(dirname "$0")" && pwd)
BINARY_DIR=$THIS_DIR/bin/dev-versions
Expand Down
112 changes: 63 additions & 49 deletions pkg/cmd/dev/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ type doctorConfig struct {
remote bool
}

// maybePromptForAutofixPermission prompts the user for autofix permission if it
// has not already been provided and we are running in interactive mode. It will
// prompt with the given question with a default ansewr of 'y', call toBoolFuzzy
// with the result, and if permission is given, will set haveAutofixPermission
// on the doctorConfig object before returning.
func (cfg *doctorConfig) maybePromptForAutofixPermission(question string) {
if !cfg.haveAutofixPermission && cfg.interactive {
response := promptInteractiveInput(question, "y")
canAutofix, ok := toBoolFuzzy(response)
if ok && canAutofix {
cfg.haveAutofixPermission = true
}
}
}

// The list of all checks performed by `dev doctor`.
var allDoctorChecks = []doctorCheck{
{
Expand Down Expand Up @@ -201,13 +216,7 @@ Make sure one of the following lines is in the file %s/.bazelrc.user:
}
return d.addLineToBazelRcUser(cfg.workspace, fmt.Sprintf("build --config=%s", response))
}
if !cfg.haveAutofixPermission && cfg.interactive {
response := promptInteractiveInput("Do you want me to add `build --config=dev` to your .bazelrc.user file for you?", "y")
canAutofix, ok := toBoolFuzzy(response)
if ok && canAutofix {
cfg.haveAutofixPermission = true
}
}
cfg.maybePromptForAutofixPermission("Do you want me to add `build --config=dev` to your .bazelrc.user file for you?")
if !cfg.haveAutofixPermission {
return fmt.Errorf("do not have permission to update .bazelrc.user")
}
Expand All @@ -224,13 +233,7 @@ Make sure one of the following lines is in the file %s/.bazelrc.user:
return ""
},
autofix: func(d *dev, ctx context.Context, cfg doctorConfig) error {
if !cfg.haveAutofixPermission && cfg.interactive {
response := promptInteractiveInput("Do you want me to remove the engflow configuration from your .bazelrc.user file for you?", "y")
canAutofix, ok := toBoolFuzzy(response)
if ok && canAutofix {
cfg.haveAutofixPermission = true
}
}
cfg.maybePromptForAutofixPermission("Do you want me to remove the engflow configuration from your .bazelrc.user file for you?")
if !cfg.haveAutofixPermission {
return fmt.Errorf("do not have permission to update .bazelrc.user")
}
Expand All @@ -253,13 +256,7 @@ Make sure one of the following lines is in the file %s/.bazelrc.user:
return ""
},
autofix: func(d *dev, ctx context.Context, cfg doctorConfig) error {
if !cfg.haveAutofixPermission && cfg.interactive {
response := promptInteractiveInput("Do you want me to update your .bazelrc.user file for you? I will set the crosslinux and engflow configs and remove any usage of the dev config if you have any.", "y")
canAutofix, ok := toBoolFuzzy(response)
if ok && canAutofix {
cfg.haveAutofixPermission = true
}
}
cfg.maybePromptForAutofixPermission("Do you want me to update your .bazelrc.user file for you? I will set the crosslinux and engflow configs and remove any usage of the dev config if you have any.")
if !cfg.haveAutofixPermission {
return fmt.Errorf("do not have permission to update .bazelrc.user")
}
Expand Down Expand Up @@ -380,20 +377,55 @@ slightly slower and introduce a noticeable delay in first-time build setup.`
return ""
},
autofix: func(d *dev, ctx context.Context, cfg doctorConfig) error {
if !cfg.haveAutofixPermission && cfg.interactive {
response := promptInteractiveInput("Do you want me to update your .bazelrc.user file for you? I will remove any `test --test_tmpdir=` line from the file.", "y")
canAutofix, ok := toBoolFuzzy(response)
if ok && canAutofix {
cfg.haveAutofixPermission = true
}
}
cfg.maybePromptForAutofixPermission("Do you want me to update your .bazelrc.user file for you? I will remove any `test --test_tmpdir=` line from the file.")
if !cfg.haveAutofixPermission {
return fmt.Errorf("do not have permission to update .bazelrc.user")
}
return d.removeAllPrefixesInFile(filepath.Join(cfg.workspace, ".bazelrc.user"), "test --test_tmpdir=")
},
remoteOnly: true,
},
{
name: "sandbox_add_mount_pair_local",
check: func(d *dev, _ context.Context, cfg doctorConfig) string {
// This check only matters for Linux machines.
if runtime.GOOS != "linux" {
return ""
}
if !d.checkLinePresenceInBazelRcUser(cfg.workspace, "test --test_tmpdir=/tmp") {
return ""
}
if d.checkLinePresenceInBazelRcUser(cfg.workspace, "test --sandbox_add_mount_pair=/tmp") {
return ""
}
return "Should set --sandbox_add_mount_pair=/tmp given the use of --test_tmpdir=/tmp"
},
autofix: func(d *dev, ctx context.Context, cfg doctorConfig) error {
cfg.maybePromptForAutofixPermission("Do you want me to update your .bazelrc.user file for you? I will add a line `test --sandbox_add_mount_pair=/tmp`.")
if !cfg.haveAutofixPermission {
return fmt.Errorf("do not have permission to update .bazelrc.user")
}
return d.addLineToBazelRcUser(cfg.workspace, "test --sandbox_add_mount_pair=/tmp")
},
nonRemoteOnly: true,
},
{
name: "sandbox_add_mount_pair_remote",
check: func(d *dev, _ context.Context, cfg doctorConfig) string {
if d.checkLinePresenceInBazelRcUser(cfg.workspace, "test --sandbox_add_mount_pair=/tmp") {
return "Should not set --sandbox_add_mount_pair in remote mode"
}
return ""
},
autofix: func(d *dev, ctx context.Context, cfg doctorConfig) error {
cfg.maybePromptForAutofixPermission("Do you want me to update your .bazelrc.user file for you? I will remove all --sandbox_add_mount_pair from your .bazelrc.user")
if !cfg.haveAutofixPermission {
return fmt.Errorf("do not have permission to update .bazelrc.user")
}
return d.removeAllPrefixesInFile(filepath.Join(cfg.workspace, ".bazelrc.user"), "test --sandbox_add_mount_pair")
},
remoteOnly: true,
},
{
name: "patchelf",
check: func(d *dev, ctx context.Context, cfg doctorConfig) string {
Expand Down Expand Up @@ -429,13 +461,7 @@ slightly slower and introduce a noticeable delay in first-time build setup.`
return fmt.Sprintf("Please add the string `%s` to your .bazelrc.user", bazelRcLine)
},
autofix: func(d *dev, ctx context.Context, cfg doctorConfig) error {
if !cfg.haveAutofixPermission && cfg.interactive {
response := promptInteractiveInput("Do you want me to update your .bazelrc.user for you to configure the loopback cache? I will also update ~/.bazelrc if necessary.", "y")
canAutofix, ok := toBoolFuzzy(response)
if ok && canAutofix {
cfg.haveAutofixPermission = true
}
}
cfg.maybePromptForAutofixPermission("Do you want me to update your .bazelrc.user for you to configure the loopback cache? I will also update ~/.bazelrc if necessary.")
if !cfg.haveAutofixPermission {
return fmt.Errorf("do not have permission to configure the cache")
}
Expand Down Expand Up @@ -472,13 +498,7 @@ slightly slower and introduce a noticeable delay in first-time build setup.`
return ""
},
autofix: func(d *dev, ctx context.Context, cfg doctorConfig) error {
if !cfg.haveAutofixPermission && cfg.interactive {
response := promptInteractiveInput("Do you want me to update your .bazelrc.user file for you? I will remove any `build --remote_cache=` line from the file.", "y")
canAutofix, ok := toBoolFuzzy(response)
if ok && canAutofix {
cfg.haveAutofixPermission = true
}
}
cfg.maybePromptForAutofixPermission("Do you want me to update your .bazelrc.user file for you? I will remove any `build --remote_cache=` line from the file.")
if !cfg.haveAutofixPermission {
return fmt.Errorf("do not have permission to update .bazelrc.user")
}
Expand All @@ -499,13 +519,7 @@ slightly slower and introduce a noticeable delay in first-time build setup.`
return ""
},
autofix: func(d *dev, ctx context.Context, cfg doctorConfig) error {
if !cfg.haveAutofixPermission && cfg.interactive {
response := promptInteractiveInput("Do you want me to update your .bazelrc.user file for you? I will remove any `build --config=lintonbuild` or `build --config=nolintonbuild` line from the file.", "y")
canAutofix, ok := toBoolFuzzy(response)
if ok && canAutofix {
cfg.haveAutofixPermission = true
}
}
cfg.maybePromptForAutofixPermission("Do you want me to update your .bazelrc.user file for you? I will remove any `build --config=lintonbuild` or `build --config=nolintonbuild` line from the file.")
if !cfg.haveAutofixPermission {
return fmt.Errorf("do not have permission to update .bazelrc.user")
}
Expand Down
Loading