Skip to content

Commit

Permalink
Fix parse environment variables error (#323)
Browse files Browse the repository at this point in the history
* fix: parse environment variables error
  • Loading branch information
ButterBright authored Sep 4, 2023
1 parent 91f0b71 commit 589cfb0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Release Notes.
- Support for recovery buffer using wal.
- Register the node role to the metadata registry.
- Implement the remote queue to spreading data to data nodes.
- Fix parse environment variables error

### Bugs

Expand Down
4 changes: 2 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func bindFlags(fs *pflag.FlagSet, v *viper.Viper) error {
fs.VisitAll(func(f *pflag.Flag) {
// Environment variables can't have dashes in them, so bind them to their equivalent
// keys with underscores.
if strings.Contains(f.Name, ".") {
envVarSuffix := strings.ToUpper(strings.ReplaceAll(f.Name, ".", "_"))
if strings.Contains(f.Name, "-") {
envVarSuffix := strings.ToUpper(strings.ReplaceAll(f.Name, "-", "_"))
err = multierr.Append(err, v.BindEnv(f.Name, fmt.Sprintf("%s_%s", envPrefix, envVarSuffix)))
}

Expand Down
64 changes: 64 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package config

import (
"os"
"testing"

"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)

func TestLoadConfig(t *testing.T) {
tester := assert.New(t)
fs := pflag.NewFlagSet("test", pflag.ExitOnError)
tests := []struct {
flagName string
flagDescription string
envName string
envValue string
}{
{
flagName: "cert-file",
flagDescription: "path to cert file",
envName: "BYDB_CERT_FILE",
envValue: "foo",
},
{
flagName: "key-file",
flagDescription: "path to key file",
envName: "BYDB_KEY_FILE",
envValue: "bar",
},
}
for _, tt := range tests {
name := "bind flag: " + tt.flagName
t.Run(name, func(t *testing.T) {
var flagValue string
fs.StringVar(&flagValue, tt.flagName, "", tt.flagDescription)
os.Setenv(tt.envName, tt.envValue)
err := Load("cfg", fs)
tester.NoError(err, name)
tester.NotNil(flagValue, name)
tester.Equal(flagValue, tt.envValue, name)
tester.NotNil(fs.Lookup(tt.flagName).Value.String(), name)
tester.Equal(fs.Lookup(tt.flagName).Value.String(), tt.envValue, name)
})
}
}

0 comments on commit 589cfb0

Please sign in to comment.