Skip to content

Commit

Permalink
Add envresolve tool (#1030)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcl authored Jul 17, 2024
1 parent 258e4a6 commit 9d88f6d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tools/envresolve/go.mod
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
)
12 changes: 12 additions & 0 deletions tools/envresolve/go.sum
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=
31 changes: 31 additions & 0 deletions tools/envresolve/main.go
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)
}
}
30 changes: 30 additions & 0 deletions utils/env_vars.go
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
}

0 comments on commit 9d88f6d

Please sign in to comment.