feat(envc): add support for converting current process environment to YAML

Enhance the `envc convert` command to allow conversion of the current process environment to YAML format. Update README with usage examples and clarify input options. Add tests to ensure correct functionality for environment variable handling.
This commit is contained in:
2026-05-12 12:27:46 +01:00
parent 474cd88739
commit b7709d693b
4 changed files with 105 additions and 12 deletions
+26 -11
View File
@@ -22,7 +22,7 @@ func RunConvert(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
fs.SetOutput(stderr)
from := fs.String("from", "", "source format: yaml|json|toml|ini|env")
to := fs.String("to", "", "target format: yaml|json|toml|ini|env")
input := fs.String("input", "-", "input path, URL, or - for stdin")
input := fs.String("input", "-", "input path, URL, - for stdin, or \"environ\" for current process env (requires --from env)")
output := fs.String("output", "-", "output path or - for stdout")
if err := fs.Parse(args); err != nil {
return 2
@@ -32,16 +32,31 @@ func RunConvert(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
return 2
}
ctx := context.Background()
src := openSource(*input, stdin)
b, err := bytesutil.ReadAll(ctx, src)
if err != nil {
_, _ = fmt.Fprintf(stderr, "convert: read: %v\n", err)
return 1
}
m, err := loadMapFromBytes(ctx, *from, b)
if err != nil {
_, _ = fmt.Fprintf(stderr, "convert: parse: %v\n", err)
return 1
var m map[string]any
var err error
if *input == "environ" {
if *from != "env" {
_, _ = fmt.Fprintln(stderr, "convert: --input environ only works with --from env")
return 2
}
m, err = env.New(env.WithCurrentEnvironment()).Map(ctx)
if err != nil {
_, _ = fmt.Fprintf(stderr, "convert: environ: %v\n", err)
return 1
}
} else {
src := openSource(*input, stdin)
var b []byte
b, err = bytesutil.ReadAll(ctx, src)
if err != nil {
_, _ = fmt.Fprintf(stderr, "convert: read: %v\n", err)
return 1
}
m, err = loadMapFromBytes(ctx, *from, b)
if err != nil {
_, _ = fmt.Fprintf(stderr, "convert: parse: %v\n", err)
return 1
}
}
outb, err := marshalMap(*to, m)
if err != nil {
+27
View File
@@ -8,6 +8,33 @@ import (
"testing"
)
func TestRunConvert_EnvironToYAML(t *testing.T) {
t.Setenv("ENVC_TEST_PUBLISH", "xyzzy")
var stdout, stderr bytes.Buffer
code := RunConvert([]string{
"--from", "env", "--to", "yaml",
"--input", "environ", "--output", "-",
}, strings.NewReader(""), &stdout, &stderr)
if code != 0 {
t.Fatalf("stderr: %s", stderr.String())
}
out := stdout.String()
if !strings.Contains(out, "xyzzy") {
t.Fatalf("expected value in yaml out: %s", out)
}
}
func TestRuknConvert_EnvironRequiresFromEnv(t *testing.T) {
var stdout, stderr bytes.Buffer
code := RunConvert([]string{
"--from", "yaml", "--to", "json",
"--input", "environ", "--output", "-",
}, strings.NewReader(""), &stdout, &stderr)
if code != 2 {
t.Fatalf("expected exit 2, got %d stderr=%s", code, stderr.String())
}
}
func TestRunConvert_TOMLToJSON(t *testing.T) {
var stdout, stderr bytes.Buffer
stdin := strings.NewReader("[app]\nport = 8080\n")