feat(go-config): multi-format codecs, envc CLI, fixtures, ASRs

- Module github.com/eslider/go-config with env/yaml/json/ini packages
- internal: source, keymap, merge, structconv, bytesutil
- cmd/envc: convert, get, merge
- docs/asr ASR-0001..0008, README/CHANGELOG/AGENTS refresh
- golangci-lint v2 config; tests + fixtures

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-05-02 14:04:01 +01:00
co-authored by Cursor
parent 288a5b600c
commit c575341178
75 changed files with 2810 additions and 528 deletions
+96
View File
@@ -0,0 +1,96 @@
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"github.com/eslider/go-config/env"
"github.com/eslider/go-config/ini"
"github.com/eslider/go-config/internal/bytesutil"
libjson "github.com/eslider/go-config/json"
"github.com/eslider/go-config/yaml"
yaml3 "gopkg.in/yaml.v3"
)
// RunConvert implements: envc convert --from F --to T [--input] [--output].
func RunConvert(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
fs := flag.NewFlagSet("convert", flag.ContinueOnError)
fs.SetOutput(stderr)
from := fs.String("from", "", "source format: yaml|json|ini|env")
to := fs.String("to", "", "target format: yaml|json|ini|env")
input := fs.String("input", "-", "input path, URL, or - for stdin")
output := fs.String("output", "-", "output path or - for stdout")
if err := fs.Parse(args); err != nil {
return 2
}
if *from == "" || *to == "" {
_, _ = fmt.Fprintln(stderr, "convert: --from and --to are required")
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
}
outb, err := marshalMap(*to, m)
if err != nil {
_, _ = fmt.Fprintf(stderr, "convert: marshal: %v\n", err)
return 1
}
out, err := openOutput(*output, stdout)
if err != nil {
_, _ = fmt.Fprintf(stderr, "convert: output: %v\n", err)
return 1
}
defer func() { _ = out.Close() }()
if _, err := out.Write(outb); err != nil {
_, _ = fmt.Fprintf(stderr, "convert: write: %v\n", err)
return 1
}
if *output == "-" || *output == "" {
if len(outb) > 0 && outb[len(outb)-1] != '\n' {
_, _ = out.Write([]byte("\n"))
}
}
return 0
}
func loadMapFromBytes(ctx context.Context, format string, b []byte) (map[string]any, error) {
switch format {
case "yaml":
return yaml.New(yaml.WithBytes(b)).Map(ctx)
case "json":
return libjson.New(libjson.WithBytes(b)).Map(ctx)
case "ini":
return ini.New(ini.WithBytes(b)).Map(ctx)
case "env":
return env.New(env.WithBytes(b)).Map(ctx)
default:
return nil, fmt.Errorf("unknown format %q", format)
}
}
func marshalMap(format string, m map[string]any) ([]byte, error) {
switch format {
case "yaml":
return yaml3.Marshal(m)
case "json":
return json.MarshalIndent(m, "", " ")
case "ini":
return ini.New().Marshal(m)
case "env":
return env.New().Marshal(m)
default:
return nil, fmt.Errorf("unknown format %q", format)
}
}
+53
View File
@@ -0,0 +1,53 @@
package main
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
)
func TestRunConvert_YAMLToJSON(t *testing.T) {
var stdout, stderr bytes.Buffer
stdin := strings.NewReader("a: 1\n")
code := RunConvert([]string{"--from", "yaml", "--to", "json", "--input", "-"}, stdin, &stdout, &stderr)
if code != 0 {
t.Fatalf("stderr: %s", stderr.String())
}
if !strings.Contains(stdout.String(), `"a"`) {
t.Fatalf("out: %s", stdout.String())
}
}
func TestRunGet(t *testing.T) {
var stdout, stderr bytes.Buffer
in := "service:\n name: x\n"
code := RunGet([]string{"--from", "yaml", "--path", "service.name", "-"}, strings.NewReader(in), &stdout, &stderr)
if code != 0 {
t.Fatalf("stderr: %s", stderr.String())
}
if strings.TrimSpace(stdout.String()) != "x" {
t.Fatalf("got %q", stdout.String())
}
}
func TestRunMerge_TwoYAMLFiles(t *testing.T) {
dir := t.TempDir()
f1 := filepath.Join(dir, "a.yaml")
f2 := filepath.Join(dir, "b.yaml")
if err := os.WriteFile(f1, []byte("k: 1\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(f2, []byte("k: 2\n"), 0o644); err != nil {
t.Fatal(err)
}
var stdout, stderr bytes.Buffer
code := RunMerge([]string{"--from", "yaml", "--to", "json", f1, f2}, strings.NewReader(""), &stdout, &stderr)
if code != 0 {
t.Fatalf("stderr: %s", stderr.String())
}
if !strings.Contains(stdout.String(), `"k"`) || !strings.Contains(stdout.String(), `2`) {
t.Fatalf("out: %s", stdout.String())
}
}
+3
View File
@@ -0,0 +1,3 @@
// Command envc converts, queries, and merges configuration between env, YAML,
// JSON, and INI formats.
package main
+83
View File
@@ -0,0 +1,83 @@
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"strings"
"github.com/eslider/go-config/internal/bytesutil"
"github.com/eslider/go-config/internal/keymap"
)
// RunGet implements: envc get --from F --path p [input].
func RunGet(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
fs := flag.NewFlagSet("get", flag.ContinueOnError)
fs.SetOutput(stderr)
from := fs.String("from", "", "format: yaml|json|ini|env")
path := fs.String("path", "", "dot-separated path (e.g. service.subservice.name)")
if err := fs.Parse(args); err != nil {
return 2
}
if *from == "" || *path == "" {
_, _ = fmt.Fprintln(stderr, "get: --from and --path are required")
return 2
}
pos := fs.Args()
input := "-"
if len(pos) > 0 {
input = pos[0]
}
ctx := context.Background()
src := openSource(input, stdin)
b, err := bytesutil.ReadAll(ctx, src)
if err != nil {
_, _ = fmt.Fprintf(stderr, "get: read: %v\n", err)
return 1
}
m, err := loadMapFromBytes(ctx, *from, b)
if err != nil {
_, _ = fmt.Fprintf(stderr, "get: parse: %v\n", err)
return 1
}
v, err := walkPath(m, strings.Split(*path, "."))
if err != nil {
_, _ = fmt.Fprintf(stderr, "get: %v\n", err)
return 1
}
switch val := v.(type) {
case string, bool, float64, int, int64, nil:
_, _ = fmt.Fprintln(stdout, val)
default:
enc := json.NewEncoder(stdout)
enc.SetIndent("", " ")
if err := enc.Encode(val); err != nil {
_, _ = fmt.Fprintf(stderr, "get: encode: %v\n", err)
return 1
}
}
return 0
}
func walkPath(m map[string]any, parts []string) (any, error) {
var cur any = m
for i, p := range parts {
if p == "" {
continue
}
pk := keymap.LowerAlnum(p)
switch t := cur.(type) {
case map[string]any:
nx, ok := t[pk]
if !ok {
return nil, fmt.Errorf("missing key %q (normalized %q) at segment %d", p, pk, i)
}
cur = nx
default:
return nil, fmt.Errorf("not a map at segment %d", i)
}
}
return cur, nil
}
+28
View File
@@ -0,0 +1,28 @@
package main
import (
"fmt"
"os"
)
func main() {
stdin, stdout, stderr := os.Stdin, os.Stdout, os.Stderr
args := os.Args[1:]
if len(args) < 1 {
_, _ = fmt.Fprintln(stderr, "usage: envc <convert|get|merge> [flags]")
os.Exit(2)
}
var code int
switch args[0] {
case "convert":
code = RunConvert(args[1:], stdin, stdout, stderr)
case "get":
code = RunGet(args[1:], stdin, stdout, stderr)
case "merge":
code = RunMerge(args[1:], stdin, stdout, stderr)
default:
_, _ = fmt.Fprintf(stderr, "unknown command %q\n", args[0])
code = 2
}
os.Exit(code)
}
+80
View File
@@ -0,0 +1,80 @@
package main
import (
"context"
"flag"
"fmt"
"io"
"github.com/eslider/go-config/internal/bytesutil"
"github.com/eslider/go-config/internal/merge"
"github.com/eslider/go-config/internal/source"
)
// RunMerge implements: envc merge --from F --to T [--output] inputs...
func RunMerge(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
fs := flag.NewFlagSet("merge", flag.ContinueOnError)
fs.SetOutput(stderr)
from := fs.String("from", "", "input format: yaml|json|ini|env")
to := fs.String("to", "", "output format: yaml|json|ini|env")
output := fs.String("output", "-", "output path or - for stdout")
if err := fs.Parse(args); err != nil {
return 2
}
if *from == "" || *to == "" {
_, _ = fmt.Fprintln(stderr, "merge: --from and --to are required")
return 2
}
inputs := fs.Args()
if len(inputs) == 0 {
_, _ = fmt.Fprintln(stderr, "merge: at least one input file or URL required")
return 2
}
ctx := context.Background()
acc := make(map[string]any)
for _, in := range inputs {
var src source.Source
if in == "-" {
b, err := io.ReadAll(stdin)
if err != nil {
_, _ = fmt.Fprintf(stderr, "merge: stdin: %v\n", err)
return 1
}
src = source.Bytes{Data: b, Name: "stdin"}
} else {
src = openSource(in, stdin)
}
b, err := bytesutil.ReadAll(ctx, src)
if err != nil {
_, _ = fmt.Fprintf(stderr, "merge: read %s: %v\n", in, err)
return 1
}
m, err := loadMapFromBytes(ctx, *from, b)
if err != nil {
_, _ = fmt.Fprintf(stderr, "merge: parse %s: %v\n", in, err)
return 1
}
merge.DeepMerge(acc, m)
}
outb, err := marshalMap(*to, acc)
if err != nil {
_, _ = fmt.Fprintf(stderr, "merge: marshal: %v\n", err)
return 1
}
out, err := openOutput(*output, stdout)
if err != nil {
_, _ = fmt.Fprintf(stderr, "merge: output: %v\n", err)
return 1
}
defer func() { _ = out.Close() }()
if _, err := out.Write(outb); err != nil {
_, _ = fmt.Fprintf(stderr, "merge: write: %v\n", err)
return 1
}
if *output == "-" || *output == "" {
if len(outb) > 0 && outb[len(outb)-1] != '\n' {
_, _ = out.Write([]byte("\n"))
}
}
return 0
}
+33
View File
@@ -0,0 +1,33 @@
package main
import (
"io"
"os"
"strings"
"github.com/eslider/go-config/internal/source"
)
func openSource(pathOrURL string, stdin io.Reader) source.Source {
if pathOrURL == "" || pathOrURL == "-" {
return source.Reader{R: stdin, Name: "stdin"}
}
if strings.HasPrefix(pathOrURL, "http://") || strings.HasPrefix(pathOrURL, "https://") {
return source.URL{Raw: pathOrURL}
}
return source.File{Path: pathOrURL}
}
type nopCloser struct{ io.Writer }
func (nopCloser) Close() error { return nil }
func openOutput(path string, stdout io.Writer) (io.WriteCloser, error) {
if path == "" || path == "-" {
if wc, ok := stdout.(io.WriteCloser); ok {
return wc, nil
}
return nopCloser{stdout}, nil
}
return os.Create(path)
}