-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add env subst command [CPE-1773] (#910)
* feat: add env subst command * chore: lint
- Loading branch information
Showing
6 changed files
with
149 additions
and
1 deletion.
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,47 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/a8m/envsubst" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newEnvCmd() *cobra.Command { | ||
var envCmd = &cobra.Command{ | ||
Use: "env", | ||
Short: "Manage environment variables", | ||
} | ||
var substCmd = &cobra.Command{ | ||
Use: "subst", | ||
Short: "Substitute environment variables in a string", | ||
RunE: substRunE, | ||
} | ||
envCmd.AddCommand(substCmd) | ||
return envCmd | ||
} | ||
|
||
// Accepts a string as an argument, or reads from stdin if no argument is provided. | ||
func substRunE(cmd *cobra.Command, args []string) error { | ||
var input string | ||
if len(args) > 0 { | ||
input = args[0] | ||
} else { | ||
// Read from stdin | ||
b, err := io.ReadAll(cmd.InOrStdin()) | ||
if err != nil { | ||
return err | ||
} | ||
input = string(b) | ||
} | ||
if input == "" { | ||
return nil | ||
} | ||
output, err := envsubst.String(input) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = fmt.Fprint(cmd.OutOrStdout(), output) | ||
return err | ||
} |
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,97 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestSubstRunE(t *testing.T) { | ||
// Set environment variables for testing | ||
err := os.Setenv("ENV_NAME", "world") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
testCases := []struct { | ||
name string | ||
input string | ||
output string | ||
}{ | ||
{ | ||
name: "substitute variables", | ||
input: "Hello $ENV_NAME!", | ||
output: "Hello world!", | ||
}, | ||
{ | ||
name: "no variables to substitute", | ||
input: "Hello, world!", | ||
output: "Hello, world!", | ||
}, | ||
{ | ||
name: "empty input", | ||
input: "", | ||
output: "", | ||
}, | ||
{ | ||
name: "no variables JSON", | ||
input: `{"foo": "bar"}`, | ||
output: `{"foo": "bar"}`, | ||
}, | ||
{ | ||
name: "substitute variables JSON", | ||
input: `{"foo": "$ENV_NAME"}`, | ||
output: `{"foo": "world"}`, | ||
}, | ||
{ | ||
name: "no variables key=value", | ||
input: `foo=bar`, | ||
output: `foo=bar`, | ||
}, | ||
} | ||
|
||
// Run tests for each test case as argument | ||
for _, tc := range testCases { | ||
t.Run("arg: "+tc.name, func(t *testing.T) { | ||
// Set up test command | ||
cmd := newEnvCmd() | ||
|
||
// Capture output | ||
outputBuf := bytes.Buffer{} | ||
cmd.SetOut(&outputBuf) | ||
|
||
// Run command | ||
cmd.SetArgs([]string{"subst", tc.input}) | ||
err := cmd.Execute() | ||
|
||
// Check output and error | ||
assert.NilError(t, err) | ||
assert.Equal(t, tc.output, outputBuf.String()) | ||
}) | ||
} | ||
// Run tests for each test case as stdin | ||
for _, tc := range testCases { | ||
t.Run("stdin: "+tc.name, func(t *testing.T) { | ||
// Set up test command | ||
cmd := newEnvCmd() | ||
|
||
// Set up input | ||
inputBuf := bytes.NewBufferString(tc.input) | ||
cmd.SetIn(inputBuf) | ||
|
||
// Capture output | ||
outputBuf := bytes.Buffer{} | ||
cmd.SetOut(&outputBuf) | ||
|
||
// Run command | ||
cmd.SetArgs([]string{"subst"}) | ||
err = cmd.Execute() | ||
|
||
// Check output and error | ||
assert.NilError(t, err) | ||
assert.Equal(t, tc.output, outputBuf.String()) | ||
}) | ||
} | ||
} |
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