From c7264d0a944d67799ba3b2e9e06808d9d5253b4a Mon Sep 17 00:00:00 2001 From: Taimoor Ahmad Date: Sat, 9 Mar 2024 18:00:32 -0500 Subject: [PATCH] cacher logs changes --- .../unreleased/Bugfix-20240309-180022.yaml | 3 ++ src/cmd/check.go | 32 ++++++++++--------- src/cmd/infra.go | 4 +-- src/cmd/root.go | 11 +++++++ src/cmd/service.go | 12 +++---- src/submodules/opslevel-go | 2 +- 6 files changed, 40 insertions(+), 24 deletions(-) create mode 100644 .changes/unreleased/Bugfix-20240309-180022.yaml diff --git a/.changes/unreleased/Bugfix-20240309-180022.yaml b/.changes/unreleased/Bugfix-20240309-180022.yaml new file mode 100644 index 00000000..a207addd --- /dev/null +++ b/.changes/unreleased/Bugfix-20240309-180022.yaml @@ -0,0 +1,3 @@ +kind: Bugfix +body: ALlow setting log level to trace, log level now affects cache logs +time: 2024-03-09T18:00:22.986942-05:00 diff --git a/src/cmd/check.go b/src/cmd/check.go index 91125924..eafc5ba7 100644 --- a/src/cmd/check.go +++ b/src/cmd/check.go @@ -152,11 +152,12 @@ func createCheck(input CheckInputType, usePrompts bool) (*opslevel.Check, error) var output *opslevel.Check var err error clientGQL := getClientGQL() - opslevel.Cache.CacheCategories(clientGQL) - opslevel.Cache.CacheLevels(clientGQL) - opslevel.Cache.CacheTeams(clientGQL) - opslevel.Cache.CacheFilters(clientGQL) - opslevel.Cache.CacheIntegrations(clientGQL) + cacher := getCacher() + cacher.CacheCategories(clientGQL) + cacher.CacheLevels(clientGQL) + cacher.CacheTeams(clientGQL) + cacher.CacheFilters(clientGQL) + cacher.CacheIntegrations(clientGQL) err = input.resolveCategoryAliases(clientGQL, usePrompts) cobra.CheckErr(err) err = input.resolveLevelAliases(clientGQL, usePrompts) @@ -184,11 +185,12 @@ func updateCheck(input CheckInputType, usePrompts bool) (*opslevel.Check, error) var output *opslevel.Check var err error clientGQL := getClientGQL() - opslevel.Cache.CacheCategories(clientGQL) - opslevel.Cache.CacheLevels(clientGQL) - opslevel.Cache.CacheTeams(clientGQL) - opslevel.Cache.CacheFilters(clientGQL) - opslevel.Cache.CacheIntegrations(clientGQL) + cacher := getCacher() + cacher.CacheCategories(clientGQL) + cacher.CacheLevels(clientGQL) + cacher.CacheTeams(clientGQL) + cacher.CacheFilters(clientGQL) + cacher.CacheIntegrations(clientGQL) err = input.resolveCategoryAliases(clientGQL, usePrompts) cobra.CheckErr(err) err = input.resolveLevelAliases(clientGQL, usePrompts) @@ -217,7 +219,7 @@ func updateCheck(input CheckInputType, usePrompts bool) (*opslevel.Check, error) func (checkInputType *CheckInputType) resolveCategoryAliases(client *opslevel.Client, usePrompt bool) error { if item, ok := checkInputType.Spec["category"]; ok { delete(checkInputType.Spec, "category") - if value, ok := opslevel.Cache.TryGetCategory(item.(string)); ok { + if value, ok := getCacher().TryGetCategory(item.(string)); ok { checkInputType.Spec["categoryId"] = value.Id return nil } else { @@ -243,7 +245,7 @@ func (checkInputType *CheckInputType) resolveCategoryAliases(client *opslevel.Cl func (checkInputType *CheckInputType) resolveLevelAliases(client *opslevel.Client, usePrompt bool) error { if item, ok := checkInputType.Spec["level"]; ok { delete(checkInputType.Spec, "level") - if value, ok := opslevel.Cache.TryGetLevel(item.(string)); ok { + if value, ok := getCacher().TryGetLevel(item.(string)); ok { checkInputType.Spec["levelId"] = value.Id return nil } else { @@ -269,7 +271,7 @@ func (checkInputType *CheckInputType) resolveLevelAliases(client *opslevel.Clien func (checkInputType *CheckInputType) resolveTeamAliases(client *opslevel.Client, usePrompt bool) error { if item, ok := checkInputType.Spec["owner"]; ok { delete(checkInputType.Spec, "owner") - if value, ok := opslevel.Cache.TryGetTeam(item.(string)); ok { + if value, ok := getCacher().TryGetTeam(item.(string)); ok { checkInputType.Spec["ownerId"] = value.Id return nil } else { @@ -293,7 +295,7 @@ func (checkInputType *CheckInputType) resolveTeamAliases(client *opslevel.Client func (checkInputType *CheckInputType) resolveFilterAliases(client *opslevel.Client, usePrompt bool) error { if item, ok := checkInputType.Spec["filter"]; ok { delete(checkInputType.Spec, "filter") - if value, ok := opslevel.Cache.TryGetFilter(item.(string)); ok { + if value, ok := getCacher().TryGetFilter(item.(string)); ok { checkInputType.Spec["filterId"] = value.Id return nil } else { @@ -317,7 +319,7 @@ func (checkInputType *CheckInputType) resolveFilterAliases(client *opslevel.Clie func (checkInputType *CheckInputType) resolveIntegrationAliases(client *opslevel.Client, usePrompt bool) error { if item, ok := checkInputType.Spec["integration"]; ok { delete(checkInputType.Spec, "integration") - if value, ok := opslevel.Cache.TryGetIntegration(item.(string)); ok { + if value, ok := getCacher().TryGetIntegration(item.(string)); ok { checkInputType.Spec["integrationId"] = value.Id return nil } else { diff --git a/src/cmd/infra.go b/src/cmd/infra.go index 64e4d8b5..2b0eecfa 100644 --- a/src/cmd/infra.go +++ b/src/cmd/infra.go @@ -69,8 +69,8 @@ opslevel --log-level=ERROR get infra-schema Compute > ~/.opslevel/schemas/comput Run: func(cmd *cobra.Command, args []string) { key := args[0] client := getClientGQL() - opslevel.Cache.CacheInfraSchemas(client) - schema, found := opslevel.Cache.TryGetInfrastructureSchema(key) + getCacher().CacheInfraSchemas(client) + schema, found := getCacher().TryGetInfrastructureSchema(key) if !found { log.Error().Msgf("unable to find infrastructure schema '%s'", key) return diff --git a/src/cmd/root.go b/src/cmd/root.go index 39877288..35b0d66b 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -18,6 +18,7 @@ import ( var ( _clientRest *resty.Client _clientGQL *opslevel.Client + _cacher *opslevel.Cacher ) var rootCmd = &cobra.Command{ @@ -74,6 +75,8 @@ func setupLogging() { zerolog.SetGlobalLevel(zerolog.WarnLevel) case logLevel == "debug": zerolog.SetGlobalLevel(zerolog.DebugLevel) + case logLevel == "trace": + zerolog.SetGlobalLevel(zerolog.TraceLevel) default: zerolog.SetGlobalLevel(zerolog.InfoLevel) } @@ -92,3 +95,11 @@ func getClientGQL(options ...opslevel.Option) *opslevel.Client { } return _clientGQL } + +func getCacher() *opslevel.Cacher { + if _cacher == nil { + logger := log.With().Str("from", "opslevel-go-cacher").Logger() + _cacher = opslevel.NewCacher(&logger) + } + return _cacher +} diff --git a/src/cmd/service.go b/src/cmd/service.go index 906b0616..9fb08728 100644 --- a/src/cmd/service.go +++ b/src/cmd/service.go @@ -160,9 +160,9 @@ EOF Run: func(cmd *cobra.Command, args []string) { reader, err := readImportFilepathAsCSV() client := getClientGQL() - opslevel.Cache.CacheLifecycles(client) - opslevel.Cache.CacheTiers(client) - opslevel.Cache.CacheTeams(client) + getCacher().CacheLifecycles(client) + getCacher().CacheTiers(client) + getCacher().CacheTeams(client) cobra.CheckErr(err) for reader.Rows() { name := reader.Text("Name") @@ -175,19 +175,19 @@ EOF } tier := reader.Text("Tier") if tier != "" { - if item, ok := opslevel.Cache.Tiers[tier]; ok { + if item, ok := getCacher().Tiers[tier]; ok { input.TierAlias = &item.Alias } } lifecycle := reader.Text("Lifecycle") if lifecycle != "" { - if item, ok := opslevel.Cache.Lifecycles[lifecycle]; ok { + if item, ok := getCacher().Lifecycles[lifecycle]; ok { input.LifecycleAlias = &item.Alias } } owner := reader.Text("Owner") if owner != "" { - if item, ok := opslevel.Cache.Teams[owner]; ok { + if item, ok := getCacher().Teams[owner]; ok { input.OwnerInput = opslevel.NewIdentifier(item.Alias) } } diff --git a/src/submodules/opslevel-go b/src/submodules/opslevel-go index f7a5030d..7abaa8de 160000 --- a/src/submodules/opslevel-go +++ b/src/submodules/opslevel-go @@ -1 +1 @@ -Subproject commit f7a5030dc58de0eedfd29f15881845f733d68aa6 +Subproject commit 7abaa8deac90af38efe320b903fc1cd4fb51c226