-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/log-the-name-of-environment-variable' into 'mas…
…ter' feat: log the names of environment variables See merge request backend/envconf!8
- Loading branch information
Showing
5 changed files
with
138 additions
and
9 deletions.
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,14 @@ | ||
package envconf | ||
|
||
type Option func(*loader) | ||
|
||
// CustomHandleEnvVarsOption creates an option that calls `cb` with a map | ||
// indicating the environment variables checked by envconf. | ||
// | ||
// The map keys are the environment variable names that are checked by envconf, | ||
// and the values present whether the corresponding environment variables are set. | ||
func CustomHandleEnvVarsOption(cb func(map[string]*EnvStatus)) Option { | ||
return func(l *loader) { | ||
l.handleEnvironmentVariables = cb | ||
} | ||
} |
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,27 @@ | ||
package options | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"log" | ||
|
||
"gitlab.rayark.com/backend/envconf" | ||
) | ||
|
||
// MakeJSONLogger will log the status of environment variables used by given structure. | ||
// The message is in JSON format. | ||
func MakeJSONLogger(w io.Writer) func(map[string]*envconf.EnvStatus) { | ||
return func(status map[string]*envconf.EnvStatus) { | ||
logger := log.New(w, "", 0) | ||
|
||
result := map[string]interface{}{} | ||
result["message"] = "envconf: show environment variables used by configuration and whether they are set" | ||
result["environment-variables"] = status | ||
b, err := json.MarshalIndent(result, "", " ") | ||
if err != nil { | ||
logger.Printf("envconf encounters an error: %v", err.Error()) | ||
return | ||
} | ||
logger.Printf(string(b)) | ||
} | ||
} |
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,26 @@ | ||
package options_test | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
|
||
"gitlab.rayark.com/backend/envconf" | ||
"gitlab.rayark.com/backend/envconf/options" | ||
) | ||
|
||
func TestLogger(t *testing.T) { | ||
os.Setenv("TEST_INTEGER", "-3") | ||
os.Setenv("TEST_UNSIGNED_INTEGER", "3") | ||
|
||
config := struct { | ||
String string `env:"string"` | ||
Integer int `env:"integer"` | ||
}{} | ||
buf := bytes.NewBuffer(nil) | ||
envconf.Load("TEST", &config, envconf.CustomHandleEnvVarsOption(options.MakeJSONLogger(buf))) | ||
|
||
if buf.String() == "" { | ||
t.Errorf("failed to log environment variable status") | ||
} | ||
} |