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

Instantiate the opslevel-go cache, set logger for that cache #261

Closed
wants to merge 4 commits into from
Closed
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
3 changes: 3 additions & 0 deletions .changes/unreleased/Bugfix-20240309-180022.yaml
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ tasks:
dir: "{{.SRC_DIR}}"
cmds:
- test -z "$(gofumpt -d -e . | tee /dev/stderr)"
- go vet ./...
- golangci-lint run

fix:
Expand Down
32 changes: 17 additions & 15 deletions src/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 15 additions & 4 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
var (
_clientRest *resty.Client
_clientGQL *opslevel.Client
_cacher *opslevel.Cacher
)

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -67,13 +68,15 @@ func setupLogging() {
log.Logger = log.Output(output)
}

switch {
case logLevel == "error":
switch logLevel {
case "error":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
case logLevel == "warn":
case "warn":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case logLevel == "debug":
case "debug":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "trace":
zerolog.SetGlobalLevel(zerolog.TraceLevel)
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
Expand All @@ -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
}
12 changes: 6 additions & 6 deletions src/cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
}
}
Expand Down
Loading