From 7bd32bac018d97f45846a5fefcab009bfe910eab Mon Sep 17 00:00:00 2001 From: Paul Cody Johnston Date: Thu, 30 Nov 2023 15:44:09 -0700 Subject: [PATCH] Add -scala_gazelle_print_cache_key flag (#108) --- language/scala/flags.go | 5 +++++ language/scala/flags_test.go | 24 ++++++++++++++++++++++++ language/scala/language.go | 2 ++ 3 files changed, 31 insertions(+) diff --git a/language/scala/flags.go b/language/scala/flags.go index f311279..cc937ec 100644 --- a/language/scala/flags.go +++ b/language/scala/flags.go @@ -23,6 +23,7 @@ const ( scalaGazelleCacheFileFlagName = "scala_gazelle_cache_file" scalaGazelleDebugProcessFileFlagName = "scala_gazelle_debug_process" scalaGazelleCacheKeyFlagName = "scala_gazelle_cache_key" + scalaGazellePrintCacheKeyFlagName = "scala_gazelle_print_cache_key" cpuprofileFileFlagName = "cpuprofile_file" memprofileFileFlagName = "memprofile_file" ) @@ -30,6 +31,7 @@ const ( // RegisterFlags implements part of the language.Language interface func (sl *scalaLang) RegisterFlags(flags *flag.FlagSet, cmd string, c *config.Config) { flags.BoolVar(&sl.debugProcessFlagValue, scalaGazelleDebugProcessFileFlagName, false, "if true, prints the process ID and waits for debugger to attach") + flags.BoolVar(&sl.printCacheKey, scalaGazellePrintCacheKeyFlagName, true, "if a cache key is set, print the version for auditing purposes") flags.StringVar(&sl.cacheFileFlagValue, scalaGazelleCacheFileFlagName, "", "optional path a cache file (.json or .pb)") flags.StringVar(&sl.cacheKeyFlagValue, scalaGazelleCacheKeyFlagName, "", "optional string that can be used to bust the cache file") flags.StringVar(&sl.cpuprofileFlagValue, cpuprofileFileFlagName, "", "optional path a cpuprofile file (.prof)") @@ -58,6 +60,9 @@ func (sl *scalaLang) registerConflictResolvers(flags *flag.FlagSet, cmd string, // CheckFlags implements part of the language.Language interface func (sl *scalaLang) CheckFlags(flags *flag.FlagSet, c *config.Config) error { + if sl.printCacheKey && sl.cacheKeyFlagValue != "" { + fmt.Printf("scala-gazelle: cache v.%s\n", sl.cacheKeyFlagValue) + } if sl.debugProcessFlagValue { fmt.Printf("Debugging session requested (Process ID: %d)\n", os.Getpid()) fmt.Printf("NOTE: binary must be built with debug symbols for this to work (e.g 'bazel run -c dbg //:gazelle')\n") diff --git a/language/scala/flags_test.go b/language/scala/flags_test.go index 56c8783..3239a5b 100644 --- a/language/scala/flags_test.go +++ b/language/scala/flags_test.go @@ -48,6 +48,30 @@ func TestCacheFlags(t *testing.T) { } }, }, + "scala_gazelle_print_cache_key_on": { + args: []string{ + "-scala_gazelle_cache_key=12345", + "-scala_gazelle_print_cache_key=true", + }, + check: func(t *testing.T, tmpDir string, lang *scalaLang) { + if diff := cmp.Diff("12345", lang.cacheKeyFlagValue); diff != "" { + t.Errorf("cacheKeyFlagValue (-want got):\n%s", diff) + } + if diff := cmp.Diff(true, lang.printCacheKey); diff != "" { + t.Errorf("printCacheKey (-want got):\n%s", diff) + } + }, + }, + "scala_gazelle_print_cache_key_off": { + args: []string{ + "-scala_gazelle_print_cache_key=false", + }, + check: func(t *testing.T, tmpDir string, lang *scalaLang) { + if diff := cmp.Diff(false, lang.printCacheKey); diff != "" { + t.Errorf("printCacheKey (-want got):\n%s", diff) + } + }, + }, "scala_gazelle_cache_key__valid": { files: []testtools.FileSpec{ { diff --git a/language/scala/language.go b/language/scala/language.go index bdd1cf8..fcc8250 100644 --- a/language/scala/language.go +++ b/language/scala/language.go @@ -25,6 +25,8 @@ type scalaLang struct { // debugProcessFlagValue halts processing and prints the PID for attaching a // delve debugger. debugProcessFlagValue bool + // optional flag to print the cache key version + printCacheKey bool // wantProgress is a flag that prints docker style progress messages if enabled wantProgress bool // cacheFileFlagValue is the main cache file, if enabled