From ebe8afb40804cf4d44c5fbf3cbd162818d00a441 Mon Sep 17 00:00:00 2001 From: KEL Date: Mon, 13 Mar 2023 23:26:47 +1100 Subject: [PATCH] [feat] ignore option (#4) --- main.go | 5 ++++- parser/parser.go | 8 +++++++- parser/parser_test.go | 38 +++++++++++++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index f79b476..ad78c4d 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,8 @@ func main() { // Define command-line flags sourcePath := flag.String("file", "", "File to inject the environment variables") outputPath := flag.String("output", "", "The output file. (This creates a new file instead of overriding the original file.)") + ignore := flag.String("ignore", "", "Regex pattern to ignore.") + flag.Bool("debug", false, "Enable debug mode") flag.Parse() @@ -22,8 +24,9 @@ func main() { return } + fileContent := string(fileBytes) - updatedContent := parser.ReplaceEnvVariables(fileContent) + updatedContent := parser.ReplaceEnvVariables(fileContent, *ignore) // Check if debug flag is true if flag.Lookup("debug").Value.String() == "true" { diff --git a/parser/parser.go b/parser/parser.go index d89f663..02550f3 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -7,7 +7,7 @@ import ( ) -func ReplaceEnvVariables(input string) string { +func ReplaceEnvVariables(input string, ignorePattern string) string { // Handling the following formats: // - $ENV_NAME // - ${ENV_NAME} @@ -22,6 +22,12 @@ func ReplaceEnvVariables(input string) string { envName = strings.TrimSuffix(envName, "}") envName = strings.TrimSuffix(envName, ")") + if ignorePattern != "" { + if regexp.MustCompile(ignorePattern).MatchString(envName) { + return match + } + } + envValue, found := os.LookupEnv(envName) if found { return envValue diff --git a/parser/parser_test.go b/parser/parser_test.go index 0d4ebfb..6a6612d 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -25,7 +25,7 @@ func TestVenvInject(t *testing.T){ let key3 = "This is value 3" } ` - got := ReplaceEnvVariables(configText) + got := ReplaceEnvVariables(configText, "") if got != want { t.Errorf("got %q, wanted %q", got, want) @@ -58,7 +58,7 @@ func TestVenvInjectWithInvalidFormatting(t *testing.T){ let key5 = "$(KEY5" } ` - got := ReplaceEnvVariables(configText) + got := ReplaceEnvVariables(configText, "") if got != want { t.Errorf("got %q, wanted %q", got, want) @@ -79,7 +79,39 @@ func TestVenvInjectWithNoSetEnvValues(t *testing.T){ } ` want := configText - got := ReplaceEnvVariables(configText) + got := ReplaceEnvVariables(configText, "") + + if got != want { + t.Errorf("got %q, wanted %q", got, want) + } +} + +func TestVenvInjectWithIgnoreOption(t *testing.T){ + + os.Setenv("KEY1", "This is value 1") + os.Setenv("KEY2", "This is value 2") + os.Setenv("KEY3", "This is value 3") + os.Setenv("IGNORE_ME", "This should be ignored") + os.Setenv("IGNORE_THIS", "This should be ignored") + + configText := ` + struct AppConfig { + let key1 = "$KEY1" + let key2 = "${KEY2}" + let key3 = "$(KEY3)" + let key4 = "${IGNORE_ME}" + let key5 = "${IGNORE_THIS}" + }` + want := ` + struct AppConfig { + let key1 = "This is value 1" + let key2 = "This is value 2" + let key3 = "This is value 3" + let key4 = "${IGNORE_ME}" + let key5 = "${IGNORE_THIS}" + }` + + got := ReplaceEnvVariables(configText, "^IGNORE_") if got != want { t.Errorf("got %q, wanted %q", got, want)