diff --git a/internal/cnf/common.go b/internal/cnf/common.go index e11fcfb..7e42c10 100644 --- a/internal/cnf/common.go +++ b/internal/cnf/common.go @@ -6,11 +6,13 @@ import ( // ConfigurationFormat is the common interface for all configuration parser type ConfigurationFormat interface { - // Read the given file - Read(io.Reader) error + // ReadFrom the given file + // implements io.ReaderFrom + ReadFrom(io.Reader) (int64, error) - // Write the given update to the file - Write(io.Writer) error + // WriteTo a file the updated content + // implements io.WriterTo + WriteTo(io.Writer) (int64, error) // Get the value of a field if it exists Get(string) (string, bool) diff --git a/internal/cnf/ini.go b/internal/cnf/ini.go index b671b7d..fca7111 100644 --- a/internal/cnf/ini.go +++ b/internal/cnf/ini.go @@ -13,26 +13,25 @@ type Ini struct { parsed bool } -// Read extract a reader as its JSON representation -func (d *Ini) Read(_reader io.Reader) error { +// ReadFrom implements io.ReaderFrom +func (d *Ini) ReadFrom(_reader io.Reader) (int64, error) { // 1. get json decoder file, err := ini.Load(ioutil.NopCloser(_reader)) if err != nil { - return err + return 0, err } d.data = file d.sections = file.SectionStrings() d.parsed = true - return nil + return 0, nil } -// Write the INI representation to a writer -func (d *Ini) Write(_writer io.Writer) error { - _, err := d.data.WriteTo(_writer) - return err +// WriteTo implements io.WriterTo +func (d *Ini) WriteTo(_writer io.Writer) (int64, error) { + return d.data.WriteTo(_writer) } // Get returns the value of a dot-separated path, and if it exists diff --git a/internal/cnf/ini_test.go b/internal/cnf/ini_test.go index a22a0f6..a6a489a 100644 --- a/internal/cnf/ini_test.go +++ b/internal/cnf/ini_test.go @@ -22,7 +22,7 @@ func TestIniGet(t *testing.T) { reader := bytes.NewBufferString(test.raw) // try to extract value - err := parser.Read(reader) + _, err := parser.ReadFrom(reader) if err != nil { t.Errorf("parse error: %s", err) continue @@ -62,7 +62,7 @@ func TestIniSetPathExists(t *testing.T) { reader := bytes.NewBufferString(test.raw) // try to extract value - err := parser.Read(reader) + _, err := parser.ReadFrom(reader) if err != nil { t.Errorf("parse error: %s", err) continue @@ -110,7 +110,7 @@ func TestIniSetCreatePath(t *testing.T) { reader := bytes.NewBufferString(test.raw) // try to extract value - err := parser.Read(reader) + _, err := parser.ReadFrom(reader) if err != nil { t.Errorf("[%d] parse error: %s", i, err) continue diff --git a/internal/cnf/json.go b/internal/cnf/json.go index 0244ac9..132ccd2 100644 --- a/internal/cnf/json.go +++ b/internal/cnf/json.go @@ -11,25 +11,26 @@ type Json struct { parsed bool } -// Read extract a reader as its JSON representation -func (d *Json) Read(_reader io.Reader) error { +// ReadFrom implements io.ReaderFrom +func (d *Json) ReadFrom(_reader io.Reader) (int64, error) { // 1. get json decoder decoder := json.NewDecoder(_reader) err := decoder.Decode(&d.data) if err != nil { - return err + return 0, err } d.parsed = true - return nil + return 0, nil } -// Write the JSON representation to a writer -func (d *Json) Write(_writer io.Writer) error { +// WriteTo implements io.WriterTo +func (d *Json) WriteTo(_writer io.Writer) (int64, error) { encoder := json.NewEncoder(_writer) - return encoder.Encode(&d.data) + encoder.SetIndent("", "\t") + return 0, encoder.Encode(&d.data) } // browse returns the target of a dot-separated path (as an interface{} chain where the last is the target if found) diff --git a/internal/cnf/json_test.go b/internal/cnf/json_test.go index c15a7d5..8c7d390 100644 --- a/internal/cnf/json_test.go +++ b/internal/cnf/json_test.go @@ -23,7 +23,7 @@ func TestJsonGet(t *testing.T) { reader := bytes.NewBufferString(test.raw) // try to extract value - err := parser.Read(reader) + _, err := parser.ReadFrom(reader) if err != nil { t.Errorf("parse error: %s", err) continue @@ -63,7 +63,7 @@ func TestJsonGetNotString(t *testing.T) { reader := bytes.NewBufferString(test.raw) // try to extract value - err := parser.Read(reader) + _, err := parser.ReadFrom(reader) if err != nil { t.Errorf("parse error: %s", err) continue @@ -99,7 +99,7 @@ func TestJsonSetPathExistsAndIsString(t *testing.T) { reader := bytes.NewBufferString(test.raw) // try to extract value - err := parser.Read(reader) + _, err := parser.ReadFrom(reader) if err != nil { t.Errorf("parse error: %s", err) continue @@ -150,7 +150,7 @@ func TestJsonSetCreatePath(t *testing.T) { reader := bytes.NewBufferString(test.raw) // try to extract value - err := parser.Read(reader) + _, err := parser.ReadFrom(reader) if err != nil { t.Errorf("[%d] parse error: %s", i, err) continue diff --git a/internal/cnf/loader.go b/internal/cnf/loader.go index 284435e..dadce5a 100644 --- a/internal/cnf/loader.go +++ b/internal/cnf/loader.go @@ -38,7 +38,7 @@ func Load(path string) (ConfigurationFormat, error) { defer file.Close() // parse - err = confFormat.Read(file) + _, err = confFormat.ReadFrom(file) if err != nil { return nil, fmt.Errorf("cannot parse file as '%s' | %s", extension, err) } diff --git a/internal/instruction/cnf.go b/internal/instruction/cnf.go index e5be4d3..c8069ea 100644 --- a/internal/instruction/cnf.go +++ b/internal/instruction/cnf.go @@ -72,7 +72,7 @@ func (d config) Exec(ctx ExecutionContext) ([]byte, error) { return nil, err } defer file.Close() - if err = format.Write(file); err != nil { + if _, err = format.WriteTo(file); err != nil { return nil, err }