fix(sg): conditionally show protips (#63541)

Small improvement as reported here
https://github.com/sourcegraph/devx-support/issues/1068

## Test plan
Tested locally
```
sourcegraph on  wb/sg-bazel/rust-hint [$!+?] via 🐹 v1.22.4 via ❄️  impure (sourcegraph-dev-env) took 9m54s
❯ CARGO_BAZEL_ISOLATED=0 CARGO_BAZEL_REPIN_ONLY=crate_index go run ./dev/sg bazel configure rustdeps
✱ Invoking the following Bazel generating categories: rustdeps
👉 running command "bazel sync --only=crate_index"

sourcegraph on  wb/sg-bazel/rust-hint [$!+?] via 🐹 v1.22.4 via ❄️  impure (sourcegraph-dev-env) took 51s
❯ CARGO_BAZEL_ISOLATED=1 CARGO_BAZEL_REPIN_ONLY=crate_index go run ./dev/sg bazel configure rustdeps
✱ Invoking the following Bazel generating categories: rustdeps
👉 running command "bazel sync --only=crate_index"
💡 pro-tip: run with CARGO_BAZEL_ISOLATED=0 for faster (but less sandboxed) repinning.
```
## Changelog
* sg - conditionally show protips when running `sg bazel`
This commit is contained in:
William Bezuidenhout 2024-06-28 12:11:07 +02:00 committed by GitHub
parent c6bc1b5181
commit 40dc6965e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -20,12 +20,25 @@ import (
"github.com/sourcegraph/sourcegraph/lib/output"
)
type tipFn func() string
type bzlgenTarget struct {
order int
cmd string
args []string
env []string
protip string
protip tipFn
}
func (t bzlgenTarget) showProTip(out *std.Output) {
tip := ""
if t.protip != nil {
tip = t.protip()
}
if tip != "" {
out.WriteLine(output.Emojif(output.EmojiLightbulb, "pro-tip: %s", tip))
}
}
var bzlgenTargets = map[string]bzlgenTarget{
@ -39,10 +52,15 @@ var bzlgenTargets = map[string]bzlgenTarget{
args: []string{"//:gazelle-update-repos"},
},
"rustdeps": {
cmd: "sync",
args: []string{"--only=crate_index"},
env: []string{"CARGO_BAZEL_REPIN=1"},
protip: "run with CARGO_BAZEL_ISOLATED=0 for faster (but less sandboxed) repinning.",
cmd: "sync",
args: []string{"--only=crate_index"},
env: []string{"CARGO_BAZEL_REPIN=1"},
protip: func() string {
if os.Getenv("CARGO_BAZEL_ISOLATED") != "0" {
return "run with CARGO_BAZEL_ISOLATED=0 for faster (but less sandboxed) repinning."
}
return ""
},
},
}
@ -153,9 +171,7 @@ If no categories are referenced, then 'builds' is assumed as the default.`,
for _, c := range categories {
std.Out.WriteNoticef("running command %q", strings.Join(append([]string{"bazel", c.cmd}, c.args...), " "))
if c.protip != "" {
std.Out.WriteLine(output.Emojif(output.EmojiLightbulb, "pro-tip: %s", c.protip))
}
c.showProTip(std.Out)
args := append([]string{c.cmd, "--noshow_progress"}, c.args...)
cmd := exec.CommandContext(ctx.Context, "bazel", args...)