-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolver for property file
- Loading branch information
aproskriakov
committed
Jun 14, 2023
1 parent
790a371
commit 6b02c0b
Showing
4 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |