-
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.
- Loading branch information
1 parent
b06a7a0
commit 458c37e
Showing
6 changed files
with
38 additions
and
34 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
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,32 @@ | ||
package parser | ||
|
||
import ( | ||
"os" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
|
||
func ReplaceEnvVariables(input string) string { | ||
// Handling the following formats: | ||
// - $ENV_NAME | ||
// - ${ENV_NAME} | ||
// - $(ENV_NAME) | ||
envPattern := regexp.MustCompile(`\$(\w+)|\$(\{?(\w+)\})\.?|\$(\(?(\w+)\))\.?`) | ||
|
||
// Replace all matches with the corresponding environment variable value | ||
return envPattern.ReplaceAllStringFunc(input, func(match string) string { | ||
envName := strings.TrimPrefix(match, "$") | ||
envName = strings.TrimPrefix(envName, "(") | ||
envName = strings.TrimPrefix(envName, "{") | ||
envName = strings.TrimSuffix(envName, "}") | ||
envName = strings.TrimSuffix(envName, ")") | ||
|
||
envValue, found := os.LookupEnv(envName) | ||
if found { | ||
return envValue | ||
} else { | ||
return match // If the variable is not found, leave the original string as-is | ||
} | ||
}) | ||
} |
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 was deleted.
Oops, something went wrong.