Skip to content

Commit

Permalink
[feat] ignore option (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelhenry authored Mar 13, 2023
1 parent c4d093d commit ebe8afb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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" {
Expand Down
8 changes: 7 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Expand Down
38 changes: 35 additions & 3 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit ebe8afb

Please sign in to comment.