-
Notifications
You must be signed in to change notification settings - Fork 1
Config Hydration #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Config Hydration #19
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c94915b
Add multi-platform Docker image support (amd64 + arm64)
akshat-kumar-singhal 2412097
Update README.md with tag v0.0.7
akshat-kumar-singhal aa0c392
Add QEMU setup for multi-platform Docker builds
akshat-kumar-singhal ecb09f7
Use cross-compilation for multi-platform Docker builds
akshat-kumar-singhal 3449b51
Update GitHub Actions to latest versions and add image output
akshat-kumar-singhal 519197f
Added support for env hydration
akshat-kumar-singhal 18fa6b9
Updated README.md
akshat-kumar-singhal 5c8a53f
Code refactor
akshat-kumar-singhal fb75fc4
Fixed implementation
akshat-kumar-singhal d54c4e3
Fixed linter issues
akshat-kumar-singhal 8f96c8e
Converted fields to unexported
akshat-kumar-singhal 46b3f7a
Updated tests
akshat-kumar-singhal ff25bad
Fixed linter issue
akshat-kumar-singhal 46500c3
Merge branch 'zopdev:main' into main
akshat-kumar-singhal a3de302
Merge pull request #1 from akshat-kumar-singhal/entrypoint-update
akshat-kumar-singhal 43c00ef
Added go doc for HydrateFile
akshat-kumar-singhal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,71 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "regexp" | ||
|
|
||
| "gofr.dev/pkg/gofr/config" | ||
| "gofr.dev/pkg/gofr/datasource/file" | ||
| ) | ||
|
|
||
| var ( | ||
| errMissingVars = errors.New("missing config variables") | ||
| errReadConfig = errors.New("failed to read config file") | ||
| errWriteConfig = errors.New("failed to write config file") | ||
|
|
||
| envVarRe = regexp.MustCompile(`\$\{(\w+)\}`) | ||
| ) | ||
|
|
||
| const filePathVar = "CONFIG_FILE_PATH" | ||
|
|
||
| func HydrateFile(fs file.FileSystem, cfg config.Config) error { | ||
| configPath := cfg.Get(filePathVar) | ||
| if configPath == "" { | ||
| return nil | ||
| } | ||
|
|
||
| configFile, err := fs.Open(filepath.Clean(configPath)) | ||
| if err != nil { | ||
| return fmt.Errorf("%w: %w", errReadConfig, err) | ||
| } | ||
|
|
||
| content, err := io.ReadAll(configFile) | ||
| if err != nil { | ||
| return fmt.Errorf("%w: %w", errReadConfig, err) | ||
| } | ||
|
|
||
| _ = configFile.Close() | ||
|
|
||
| // Hydrate with available vars | ||
| result := os.Expand(string(content), cfg.Get) | ||
|
|
||
| wf, err := fs.OpenFile(configPath, os.O_WRONLY|os.O_TRUNC, 0) | ||
| if err != nil { | ||
| return fmt.Errorf("%w: %w", errWriteConfig, err) | ||
| } | ||
|
|
||
| if _, err = wf.Write([]byte(result)); err != nil { | ||
| return fmt.Errorf("%w: %w", errWriteConfig, err) | ||
| } | ||
|
|
||
| // Detect vars that were missing (replaced with empty string) | ||
| matches := envVarRe.FindAllStringSubmatch(string(content), -1) | ||
|
|
||
| var missing []string | ||
|
|
||
| for _, m := range matches { | ||
| if cfg.Get(m[1]) == "" { | ||
| missing = append(missing, m[1]) | ||
| } | ||
| } | ||
|
|
||
| if len(missing) > 0 { | ||
| return fmt.Errorf("%w: %v", errMissingVars, missing) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or 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,121 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "runtime" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "gofr.dev/pkg/gofr/config" | ||
| "gofr.dev/pkg/gofr/datasource/file" | ||
| "gofr.dev/pkg/gofr/logging" | ||
| ) | ||
|
|
||
| func writeTempFile(t *testing.T, fs file.FileSystem, content string) string { | ||
| t.Helper() | ||
|
|
||
| dir := t.TempDir() | ||
| path := filepath.Join(dir, "config.json") | ||
|
|
||
| f, err := fs.Create(path) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = f.Write([]byte(content)) | ||
| require.NoError(t, err) | ||
|
|
||
| require.NoError(t, f.Close()) | ||
|
|
||
| return path | ||
| } | ||
|
|
||
| func TestConfig(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| template string | ||
| vars map[string]string | ||
| expected string | ||
| wantErr error | ||
| }{ | ||
| { | ||
| name: "all vars present", | ||
| template: `{"a":"${A}","b":"${B}"}`, | ||
| vars: map[string]string{"A": "1", "B": "2"}, | ||
| expected: `{"a":"1","b":"2"}`, | ||
| }, | ||
| { | ||
| name: "no config path is a no-op", | ||
| vars: map[string]string{}, | ||
| wantErr: nil, | ||
| }, | ||
| { | ||
| name: "extra vars ignored", | ||
| template: `{"a":"${A}"}`, | ||
| vars: map[string]string{"A": "1", "EXTRA": "x"}, | ||
| expected: `{"a":"1"}`, | ||
| }, | ||
| { | ||
| name: "partial vars missing", | ||
| template: `{"a":"${A}","b":"${MISSING}"}`, | ||
| vars: map[string]string{"A": "1"}, | ||
| expected: `{"a":"1","b":""}`, | ||
| wantErr: errMissingVars, | ||
| }, | ||
| { | ||
| name: "all vars missing", | ||
| template: `{"a":"${X}","b":"${Y}"}`, | ||
| vars: map[string]string{}, | ||
| expected: `{"a":"","b":""}`, | ||
| wantErr: errMissingVars, | ||
| }, | ||
| { | ||
| name: "invalid config path", | ||
| vars: map[string]string{"CONFIG_FILE_PATH": "/no/such/file"}, | ||
| wantErr: errReadConfig, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| fs := file.New(logging.NewMockLogger(logging.ERROR)) | ||
|
|
||
| if tt.template != "" { | ||
| path := writeTempFile(t, fs, tt.template) | ||
| tt.vars[filePathVar] = path | ||
| } | ||
|
|
||
| err := HydrateFile(fs, config.NewMockConfig(tt.vars)) | ||
|
|
||
| require.ErrorIs(t, err, tt.wantErr) | ||
|
|
||
| if tt.expected != "" { | ||
| rf, readErr := fs.Open(tt.vars[filePathVar]) | ||
| require.NoError(t, readErr) | ||
| got, readErr := io.ReadAll(rf) | ||
| require.NoError(t, readErr) | ||
| require.Equal(t, tt.expected, string(got)) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestHydrateFile_WriteError(t *testing.T) { | ||
| if runtime.GOOS == "windows" { | ||
| t.Skip("chmod not effective on Windows") | ||
| } | ||
|
|
||
| fs := file.New(logging.NewMockLogger(logging.ERROR)) | ||
| path := writeTempFile(t, fs, `{"a":"${A}"}`) | ||
|
|
||
| err := os.Chmod(path, 0444) | ||
| require.NoError(t, err) | ||
|
|
||
| vars := map[string]string{ | ||
| filePathVar: path, | ||
| "A": "1", | ||
| } | ||
|
|
||
| err = HydrateFile(fs, config.NewMockConfig(vars)) | ||
| require.ErrorIs(t, err, errWriteConfig) | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
config should not be sent here, we should read in main.go and inject the key value pairs, config.Config should not be the dependency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also add a comment of why this is being done is because we are doing something which is not recommended and we also need to make sure we don't follow this elsewhere and it was done here because this is the cleanest way as of now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done