Skip to content

Commit

Permalink
revert bad rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasmota committed Mar 8, 2024
1 parent e4af3c0 commit 7b2906b
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions confmap/converter/expandconverter/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,30 @@
package expandconverter // import "go.opentelemetry.io/collector/confmap/converter/expandconverter"

import (
"fmt"
"context"
"fmt"
"os"
"regexp"

"go.opentelemetry.io/collector/confmap"
"go.uber.org/zap"
)

type converter struct{
logger *zap.Logger
type converter struct {
logger *zap.Logger

// Record of which env vars we have logged a warning for
loggedDeprecations map[string]bool
// Record of which env vars we have logged a warning for
loggedDeprecations map[string]struct{}
}

// New returns a confmap.Converter, that expands all environment variables for a given confmap.Conf.
//
// Notice: This API is experimental.
func New(_ confmap.ConverterSettings) confmap.Converter {
return converter{
loggedDeprecations: make(map[string]bool),
logger: zap.NewNop(), // TODO: pass logger in ConverterSettings
}
loggedDeprecations: make(map[string]struct{}),
logger: zap.NewNop(), // TODO: pass logger in ConverterSettings
}
}

func (c converter) Convert(_ context.Context, conf *confmap.Conf) error {
Expand Down Expand Up @@ -60,12 +61,15 @@ func (c converter) expandStringValues(value any) any {

func (c converter) expandEnv(s string) string {
return os.Expand(s, func(str string) string {
if (!c.loggedDeprecations[str]) {
msg := fmt.Sprintf("Variable substitution using $VAR will be deprecated in favor of ${env:VAR}, please update $%v", str)
c.logger.Warn(msg, zap.String("variable", str))
c.loggedDeprecations[str] = true
}

// Matches on $VAR style environment variables
// in order to make sure we don't log a warning for ${VAR}
var regex = regexp.MustCompile(fmt.Sprintf(`\$%s`, str))
if _, exists := c.loggedDeprecations[str]; !exists && regex.MatchString(s) {
msg := fmt.Sprintf("Variable substitution using $VAR will be deprecated in favor of ${VAR} and ${env:VAR}, please update $%s", str)
c.logger.Warn(msg, zap.String("variable", str))
c.loggedDeprecations[str] = struct{}{}
}
// This allows escaping environment variable substitution via $$, e.g.
// - $FOO will be substituted with env var FOO
// - $$FOO will be replaced with $FOO
Expand Down

0 comments on commit 7b2906b

Please sign in to comment.