-
Notifications
You must be signed in to change notification settings - Fork 39
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
Showing
4 changed files
with
86 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module github.com/smartcontractkit/chainlink-testing-framework/tools/envresolve | ||
|
||
go 1.22.5 | ||
|
||
require ( | ||
github.com/smartcontractkit/chainlink-testing-framework v1.32.5-0.20240716133725-a3b8de948a76 | ||
github.com/spf13/cobra v1.8.1 | ||
) | ||
|
||
require ( | ||
github.com/inconshreveable/mousetrap v1.1.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
) |
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,12 @@ | ||
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= | ||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= | ||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= | ||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||
github.com/smartcontractkit/chainlink-testing-framework v1.32.5-0.20240716133725-a3b8de948a76 h1:U3hc8y9ave4rTINBhqtlVJpgUftEfQq4GO8avP64H5M= | ||
github.com/smartcontractkit/chainlink-testing-framework v1.32.5-0.20240716133725-a3b8de948a76/go.mod h1:5MSx/ZFLZZCzovF0TZU28dy2XPGznlc/sa2pgUN/8D8= | ||
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= | ||
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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,31 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/smartcontractkit/chainlink-testing-framework/utils" | ||
) | ||
|
||
func main() { | ||
var rootCmd = &cobra.Command{ | ||
Use: "envresolve", | ||
Short: "Resolve environment variables in a string. Example envresolve '{{ env.CHAINLINK_IMAGE }}'", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) < 1 { | ||
fmt.Fprintln(cmd.OutOrStdout(), "Error: No input provided") | ||
return | ||
} | ||
input := args[0] | ||
|
||
fmt.Fprintln(cmd.OutOrStdout(), utils.MustResolveEnvPlaceholder(input)) | ||
}, | ||
} | ||
|
||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} |
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,30 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"regexp" | ||
) | ||
|
||
// mustResolveEnvPlaceholder checks if the input string is an environment variable placeholder and resolves it. | ||
func MustResolveEnvPlaceholder(input string) string { | ||
envVarName, hasEnvVar := LookupEnvVarName(input) | ||
if hasEnvVar { | ||
value, set := os.LookupEnv(envVarName) | ||
if !set { | ||
fmt.Fprintf(os.Stderr, "Error resolving '%s'. Environment variable '%s' not set or is empty\n", input, envVarName) | ||
os.Exit(1) | ||
} | ||
return value | ||
} | ||
return input | ||
} | ||
|
||
func LookupEnvVarName(input string) (string, bool) { | ||
re := regexp.MustCompile(`^{{ env\.([a-zA-Z_]+) }}$`) | ||
matches := re.FindStringSubmatch(input) | ||
if len(matches) > 1 { | ||
return matches[1], true | ||
} | ||
return "", false | ||
} |