Skip to content

Commit

Permalink
Config: get rid of default registry values
Browse files Browse the repository at this point in the history
All default values are empty strings, and the linter was also
complaining about readValue always receiving the same value for its
'defaultValue' argument.

Hence, I simplified the code so that there are no default values, or
rather the default values are always "". We can bring them back if we
ever need one of the defaults to be non-empty.
  • Loading branch information
EduardGomezEscandell committed Oct 24, 2023
1 parent 37de580 commit 9cbf554
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions windows-agent/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,9 @@ import (
const (
registryPath = `Software\Canonical\UbuntuPro`

defaultToken = ""

fieldLandscapeClientConfig = "LandscapeClientConfig"
defaultLandscapeClientConfig = ""

fieldLandscapeAgentURL = "LandscapeAgentURL"
defaultLandscapeAgentURL = ""

fieldLandscapeAgentUID = "LandscapeAgentUID"
defaultLandscapeAgentUID = ""
fieldLandscapeClientConfig = "LandscapeClientConfig"
fieldLandscapeAgentURL = "LandscapeAgentURL"
fieldLandscapeAgentUID = "LandscapeAgentUID"
)

// fieldsProToken contains the fields in the registry where each source will store its token.
Expand Down Expand Up @@ -304,9 +297,6 @@ func (c *Config) loadRegistry(ctx context.Context) (proTokens map[SubscriptionSo
k, err := c.registry.HKCUOpenKey(registryPath, registry.READ)
if errors.Is(err, registry.ErrKeyNotExist) {
log.Debug(ctx, "Registry key does not exist, using default values")
data.landscapeAgentURL = defaultLandscapeAgentURL
data.landscapeClientConfig = defaultLandscapeClientConfig
data.landscapeAgentUID = defaultLandscapeAgentUID
return proTokens, data, nil
}
if err != nil {
Expand All @@ -315,7 +305,7 @@ func (c *Config) loadRegistry(ctx context.Context) (proTokens map[SubscriptionSo
defer c.registry.CloseKey(k)

for source, field := range fieldsProToken {
proToken, e := c.readValue(ctx, k, field, defaultToken)
proToken, e := c.readValue(ctx, k, field)
if e != nil {
err = errors.Join(err, fmt.Errorf("could not read %q: %v", field, e))
continue
Expand All @@ -332,29 +322,29 @@ func (c *Config) loadRegistry(ctx context.Context) (proTokens map[SubscriptionSo
return nil, data, err
}

data.landscapeAgentURL, err = c.readValue(ctx, k, fieldLandscapeAgentURL, defaultLandscapeAgentURL)
data.landscapeAgentURL, err = c.readValue(ctx, k, fieldLandscapeAgentURL)
if err != nil {
return proTokens, data, err
}

data.landscapeClientConfig, err = c.readValue(ctx, k, fieldLandscapeClientConfig, defaultLandscapeClientConfig)
data.landscapeClientConfig, err = c.readValue(ctx, k, fieldLandscapeClientConfig)
if err != nil {
return proTokens, data, err
}

data.landscapeAgentUID, err = c.readValue(ctx, k, fieldLandscapeAgentUID, defaultLandscapeAgentUID)
data.landscapeAgentUID, err = c.readValue(ctx, k, fieldLandscapeAgentUID)
if err != nil {
return proTokens, data, err
}

return proTokens, data, nil
}

func (c *Config) readValue(ctx context.Context, key uintptr, field string, defaultValue string) (string, error) {
func (c *Config) readValue(ctx context.Context, key uintptr, field string) (string, error) {
value, err := c.registry.ReadValue(key, field)
if errors.Is(err, registry.ErrFieldNotExist) {
log.Debugf(ctx, "Registry value %q does not exist, defaulting to %q", field, defaultValue)
return defaultValue, nil
log.Debugf(ctx, "Registry value %q does not exist, defaulting to empty", field)
return "", nil
}
if err != nil {
return "", err
Expand Down

0 comments on commit 9cbf554

Please sign in to comment.