Skip to content

Commit

Permalink
resolver for property file
Browse files Browse the repository at this point in the history
resolver for property file
  • Loading branch information
aproskriakov committed Jun 14, 2023
1 parent 790a371 commit 6b02c0b
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"go.uber.org/zap/zapcore"
)

const Version = "0.5.4"
const Version = "0.5.5"
const defaultConfigFile = "load"
const stdinConfigSelector = "-"

Expand Down
1 change: 1 addition & 0 deletions core/import/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func Import(fs afero.Fs) {

confutil.RegisterTagResolver("", confutil.EnvTagResolver)
confutil.RegisterTagResolver("ENV", confutil.EnvTagResolver)
confutil.RegisterTagResolver("PROPERTY", confutil.PropertyTagResolver)

// Required for decoding plugins. Need to be added after Composite Schedule hacky hook.
pluginconfig.AddHooks()
Expand Down
35 changes: 35 additions & 0 deletions lib/confutil/property_var_resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package confutil

import (
"bufio"
"fmt"
"os"
"strings"
)

// Resolve custom tag token with property variable value. Allow read from properties file
// for example: secret: '${property: /etc/datasources/secret.properties#tvm_secret}'
var PropertyTagResolver TagResolver = propertyTokenResolver

func propertyTokenResolver(in string) (string, error) {
split := strings.SplitN(in, "#", 2)
filename, property := split[0], split[1]
file, err := os.Open(filename)
if err != nil {
return "", fmt.Errorf("cannot open file: '%v'", filename)
}

defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "=") {
kv := strings.SplitN(line, "=", 2)
if kv[0] == property {
return kv[1], nil
}
}
}

return "", fmt.Errorf("no such property '%v', in file '%v'", property, filename)
}
64 changes: 64 additions & 0 deletions lib/confutil/property_var_resolver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package confutil

import (
"io/ioutil"
"os"
"testing"

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

func TestPropertyTokenResolver(t *testing.T) {
fileContent := []byte(`name=John Doe
age=25
[email protected]`)
tmpFile, err := ioutil.TempFile("", "testfile*.txt")
assert.NoError(t, err)
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()
_, err = tmpFile.Write(fileContent)
assert.NoError(t, err)

testCases := []struct {
input string
expectedResult string
expectedError string
}{
{
input: tmpFile.Name() + "#name",
expectedResult: "John Doe",
expectedError: "",
},
{
input: tmpFile.Name() + "#age",
expectedResult: "25",
expectedError: "",
},
{
input: tmpFile.Name() + "#email",
expectedResult: "[email protected]",
expectedError: "",
},
{
input: tmpFile.Name() + "#address",
expectedResult: "",
expectedError: "no such property 'address', in file '" + tmpFile.Name() + "'",
},
{
input: "nonexistent.txt#property",
expectedResult: "",
expectedError: "cannot open file: 'nonexistent.txt'",
},
}

for _, testCase := range testCases {
result, err := propertyTokenResolver(testCase.input)

assert.Equal(t, testCase.expectedResult, result)
if testCase.expectedError != "" {
assert.EqualError(t, err, testCase.expectedError)
} else {
assert.NoError(t, err)
}
}
}

0 comments on commit 6b02c0b

Please sign in to comment.