From f5bbbcb572db7e8d87f01a29d71eb6bcf72992c4 Mon Sep 17 00:00:00 2001 From: Jean-Hadrien Chabran Date: Wed, 3 Jul 2024 15:21:03 +0200 Subject: [PATCH] chore(local): sg warns about opposite default value on dev builds (#63612) As I just reviewed a PR for `sg` about analytics, I've wasted 10m wondering why I wasn't seeing anything (thought I might be on the wrong branch, that type of things). Turned out, we obviously flip certain default value for flags (such as disabling analytics) when running a dev build. `sg` now prints out a warning at the beginning stating which of those are flipped out. ## Test plan CI + local run ![CleanShot 2024-07-03 at 15 15 57@2x](https://github.com/sourcegraph/sourcegraph/assets/10151/5af2fb3d-eaa3-4503-804f-8336435a86f4) Warning sign is missing, but it's there, something wrong with my font. ## Changelog --- dev/sg/main.go | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/dev/sg/main.go b/dev/sg/main.go index 225e84a21ff..c4a1931f934 100644 --- a/dev/sg/main.go +++ b/dev/sg/main.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "time" hashstructure "github.com/mitchellh/hashstructure/v2" @@ -128,18 +129,18 @@ var sg = &cli.App{ Value: false, Destination: &disableOverwrite, }, - &cli.BoolFlag{ + warnSkippedInDev(&cli.BoolFlag{ Name: "skip-auto-update", Usage: "prevent sg from automatically updating itself", EnvVars: []string{"SG_SKIP_AUTO_UPDATE"}, Value: BuildCommit == "dev", // Default to skip in dev - }, - &cli.BoolFlag{ + }), + warnSkippedInDev(&cli.BoolFlag{ Name: "disable-analytics", Usage: "disable event logging (logged to '~/.sourcegraph/events')", EnvVars: []string{"SG_DISABLE_ANALYTICS"}, Value: BuildCommit == "dev", // Default to skip in dev - }, + }), &cli.BoolFlag{ Name: "disable-output-detection", Usage: "use fixed output configuration instead of detecting terminal capabilities", @@ -178,6 +179,12 @@ var sg = &cli.App{ // Configure global output std.Out = std.NewOutput(cmd.App.Writer, verbose) + // Print out a warning about flags which are disabled by default only in dev, but + // enabled otherwise. + if BuildCommit == "dev" { + printSkippedInDevWarning() + } + // Set up analytics and hooks for each command - do this as the first context // setup if !cmd.Bool("disable-analytics") { @@ -424,6 +431,24 @@ func watchConfig(ctx context.Context) (<-chan *sgconf.Config, error) { return output, err } +var skippedInDevFlags = []cli.Flag{} + +// warnSkippedInDev registers a flag as having a different default value when sg is running in dev mode. +func warnSkippedInDev(flag cli.Flag) cli.Flag { + skippedInDevFlags = append(skippedInDevFlags, flag) + return flag +} + +// printSkippedInDevWarning reminds the user that sg is running in dev mode and certain flags have a different default value. +func printSkippedInDevWarning() { + names := []string{} + for _, f := range skippedInDevFlags { + // Safe because it's not possible for a flag to not have name. + names = append(names, f.Names()[0]) + } + std.Out.WriteWarningf("Running sg with a dev build, following flags have different default value unless explictly set: %s", strings.Join(names, ", ")) +} + func exists(file string) bool { _, err := os.Stat(file) return !os.IsNotExist(err)