~alazarte/cfgreader

e248f7483f2dcf69155c75ba58785348f9872d4d — Alejandro Lazarte 3 months ago 52a1f19 master
ReadFrom instead of FromFile, update regex
2 files changed, 12 insertions(+), 19 deletions(-)

M unmarshaler.go
M unmarshaler_test.go
M unmarshaler.go => unmarshaler.go +3 -8
@@ 3,7 3,6 @@ package cfgreader
import (
	"bufio"
	"io"
	"os"
	"reflect"
	"regexp"
)


@@ 36,13 35,9 @@ func unmarshalCfg(m map[string]interface{}, v interface{}) error {
	return nil
}

func FromFile(filepath string, v interface{}) error {
	f, err := os.Open(filepath)
	if err != nil {
		return err
	}
	buf := bufio.NewReader(f)
	group := regexp.MustCompile(`(?:([A-Z][a-z]+)\.)?([A-Z][a-z]+)=([A-Za-z0-9\./]+)\n`)
func ReadFrom(reader io.Reader, v interface{}) error {
	buf := bufio.NewReader(reader)
	group := regexp.MustCompile(`(?:(\w+)\.)?(\w+)=([\w\./-]+)\n`)
	m := make(map[string]interface{})
	for {
		l, err := buf.ReadString('\n')

M unmarshaler_test.go => unmarshaler_test.go +9 -11
@@ 2,21 2,19 @@ package cfgreader

import (
	"fmt"
	"os"
	"strings"
	"testing"
)

func TestInit(t *testing.T) {
	contents := `Pem=fullchainfile.pem
Sk=privkeyfile.pem
var (
	reader = strings.NewReader(`Pem=full-chainfile.pem
Sk=priv-keyfile.pem
Handler.Name=someName
Handler.Path=/tmp
Handler.Type=type
`
	if err := os.WriteFile("test", []byte(contents), 0644); err != nil {
		t.Fatal(err)
	}
}
`)
)

func TestHelloWorld(t *testing.T) {
	c := struct {
		Pem     string


@@ 27,10 25,10 @@ func TestHelloWorld(t *testing.T) {
			Type string
		}
	}{}
	if err := FromFile("test", &c); err != nil {
	if err := ReadFrom(reader, &c); err != nil {
		t.Fatal(err)
	}
	if fmt.Sprintf("%q", &c) != "&{\"fullchainfile.pem\" \"privkeyfile.pem\" {\"someName\" \"/tmp\" \"type\"}}" {
	if fmt.Sprintf("%q", &c) != "&{\"full-chainfile.pem\" \"priv-keyfile.pem\" {\"someName\" \"/tmp\" \"type\"}}" {
		t.Fatal("Not what I expected")
	}
}