Skip to content

Commit

Permalink
parser package
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelhenry committed Mar 3, 2023
1 parent b06a7a0 commit 458c37e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:
- uses: actions/setup-go@v3
with:
go-version-file: go.mod
- run: go test
- run: go test ./parser -coverprofile=coverage.out -v
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/setup-go@v3
with:
go-version-file: go.mod
- run: go test
- run: go test ./parser -coverprofile=coverage.out -v
- uses: goreleaser/goreleaser-action@v4
with:
distribution: goreleaser
Expand Down
31 changes: 2 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,10 @@ import (
"flag"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
"michaelhenry/envject/parser"
)



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
}
})
}

func main() {
// Define command-line flags
filePath := flag.String("file", "", "File to inject environment variables")
Expand All @@ -49,7 +22,7 @@ func main() {
}

fileContent := string(fileBytes)
updatedContent := ReplaceEnvVariables(fileContent)
updatedContent := parser.ReplaceEnvVariables(fileContent)

// Check if debug flag is true
if flag.Lookup("debug").Value.String() == "true" {
Expand Down
32 changes: 32 additions & 0 deletions parser/parser.go
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
}
})
}
4 changes: 2 additions & 2 deletions envject_test.go → parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package parser

import (
"os"
Expand Down Expand Up @@ -84,4 +84,4 @@ func TestVenvInjectWithNoSetEnvValues(t *testing.T){
if got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}
}
1 change: 0 additions & 1 deletion sample.config.txt

This file was deleted.

0 comments on commit 458c37e

Please sign in to comment.