From 4915dbdc3461927d328df7a51f927c7d2e027592 Mon Sep 17 00:00:00 2001 From: rasta-rocket Date: Sun, 15 Jan 2023 21:52:54 +0100 Subject: [PATCH 1/2] chore(cmd): allow passing options from cmd flags to convert functions --- cmd/jsontoml/main.go | 2 +- cmd/jsontoml/main_test.go | 4 +++- cmd/tomljson/main.go | 2 +- cmd/tomljson/main_test.go | 4 +++- cmd/tomll/main.go | 2 +- cmd/tomll/main_test.go | 4 +++- internal/cli/cli.go | 9 ++++++--- internal/cli/cli_test.go | 14 +++++++------- 8 files changed, 25 insertions(+), 16 deletions(-) diff --git a/cmd/jsontoml/main.go b/cmd/jsontoml/main.go index 5a1fbe4a..964fafc0 100644 --- a/cmd/jsontoml/main.go +++ b/cmd/jsontoml/main.go @@ -41,7 +41,7 @@ func main() { p.Execute() } -func convert(r io.Reader, w io.Writer) error { +func convert(r io.Reader, w io.Writer, o cli.Options) error { var v interface{} d := json.NewDecoder(r) diff --git a/cmd/jsontoml/main_test.go b/cmd/jsontoml/main_test.go index 4a4ad072..99daee70 100644 --- a/cmd/jsontoml/main_test.go +++ b/cmd/jsontoml/main_test.go @@ -5,6 +5,8 @@ import ( "strings" "testing" + "github.com/pelletier/go-toml/v2/internal/cli" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -37,7 +39,7 @@ a = 42.0 for _, e := range examples { b := new(bytes.Buffer) - err := convert(strings.NewReader(e.input), b) + err := convert(strings.NewReader(e.input), b, cli.Options{}) if e.errors { require.Error(t, err) } else { diff --git a/cmd/tomljson/main.go b/cmd/tomljson/main.go index 297b0a58..6b581e8e 100644 --- a/cmd/tomljson/main.go +++ b/cmd/tomljson/main.go @@ -43,7 +43,7 @@ func main() { p.Execute() } -func convert(r io.Reader, w io.Writer) error { +func convert(r io.Reader, w io.Writer, o cli.Options) error { var v interface{} d := toml.NewDecoder(r) diff --git a/cmd/tomljson/main_test.go b/cmd/tomljson/main_test.go index 8e787518..7e82c21c 100644 --- a/cmd/tomljson/main_test.go +++ b/cmd/tomljson/main_test.go @@ -7,6 +7,8 @@ import ( "strings" "testing" + "github.com/pelletier/go-toml/v2/internal/cli" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -44,7 +46,7 @@ a = 42`), for _, e := range examples { b := new(bytes.Buffer) - err := convert(e.input, b) + err := convert(e.input, b, cli.Options{}) if e.errors { require.Error(t, err) } else { diff --git a/cmd/tomll/main.go b/cmd/tomll/main.go index 2ae3a71f..f84aef3f 100644 --- a/cmd/tomll/main.go +++ b/cmd/tomll/main.go @@ -44,7 +44,7 @@ func main() { p.Execute() } -func convert(r io.Reader, w io.Writer) error { +func convert(r io.Reader, w io.Writer, o cli.Options) error { var v interface{} d := toml.NewDecoder(r) diff --git a/cmd/tomll/main_test.go b/cmd/tomll/main_test.go index 553bccad..20ce8b6b 100644 --- a/cmd/tomll/main_test.go +++ b/cmd/tomll/main_test.go @@ -5,6 +5,8 @@ import ( "strings" "testing" + "github.com/pelletier/go-toml/v2/internal/cli" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -34,7 +36,7 @@ a = 42.0 for _, e := range examples { b := new(bytes.Buffer) - err := convert(strings.NewReader(e.input), b) + err := convert(strings.NewReader(e.input), b, cli.Options{}) if e.errors { require.Error(t, err) } else { diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 4d50800e..3ebaaa9c 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -12,7 +12,9 @@ import ( "github.com/pelletier/go-toml/v2" ) -type ConvertFn func(r io.Reader, w io.Writer) error +type Options map[string]interface{} + +type ConvertFn func(r io.Reader, w io.Writer, options Options) error type Program struct { Usage string @@ -20,6 +22,7 @@ type Program struct { // Inplace allows the command to take more than one file as argument and // perform convertion in place on each provided file. Inplace bool + Opts Options } func (p *Program) Execute() { @@ -58,7 +61,7 @@ func (p *Program) run(files []string, input io.Reader, output io.Writer) error { defer f.Close() input = f } - return p.Fn(input, output) + return p.Fn(input, output, p.Opts) } func (p *Program) runAllFilesInPlace(files []string) error { @@ -79,7 +82,7 @@ func (p *Program) runFileInPlace(path string) error { out := new(bytes.Buffer) - err = p.Fn(bytes.NewReader(in), out) + err = p.Fn(bytes.NewReader(in), out, p.Opts) if err != nil { return err } diff --git a/internal/cli/cli_test.go b/internal/cli/cli_test.go index 429fa730..1c132143 100644 --- a/internal/cli/cli_test.go +++ b/internal/cli/cli_test.go @@ -25,7 +25,7 @@ func TestProcessMainStdin(t *testing.T) { stderr := new(bytes.Buffer) input := strings.NewReader("this is the input") - exit := processMain([]string{}, input, stdout, stderr, func(r io.Reader, w io.Writer) error { + exit := processMain([]string{}, input, stdout, stderr, func(r io.Reader, w io.Writer, o Options) error { return nil }) @@ -39,7 +39,7 @@ func TestProcessMainStdinErr(t *testing.T) { stderr := new(bytes.Buffer) input := strings.NewReader("this is the input") - exit := processMain([]string{}, input, stdout, stderr, func(r io.Reader, w io.Writer) error { + exit := processMain([]string{}, input, stdout, stderr, func(r io.Reader, w io.Writer, o Options) error { return fmt.Errorf("something bad") }) @@ -53,7 +53,7 @@ func TestProcessMainStdinDecodeErr(t *testing.T) { stderr := new(bytes.Buffer) input := strings.NewReader("this is the input") - exit := processMain([]string{}, input, stdout, stderr, func(r io.Reader, w io.Writer) error { + exit := processMain([]string{}, input, stdout, stderr, func(r io.Reader, w io.Writer, o Options) error { var v interface{} return toml.Unmarshal([]byte(`qwe = 001`), &v) }) @@ -73,7 +73,7 @@ func TestProcessMainFileExists(t *testing.T) { stdout := new(bytes.Buffer) stderr := new(bytes.Buffer) - exit := processMain([]string{tmpfile.Name()}, nil, stdout, stderr, func(r io.Reader, w io.Writer) error { + exit := processMain([]string{tmpfile.Name()}, nil, stdout, stderr, func(r io.Reader, w io.Writer, o Options) error { return nil }) @@ -86,7 +86,7 @@ func TestProcessMainFileDoesNotExist(t *testing.T) { stdout := new(bytes.Buffer) stderr := new(bytes.Buffer) - exit := processMain([]string{"/lets/hope/this/does/not/exist"}, nil, stdout, stderr, func(r io.Reader, w io.Writer) error { + exit := processMain([]string{"/lets/hope/this/does/not/exist"}, nil, stdout, stderr, func(r io.Reader, w io.Writer, o Options) error { return nil }) @@ -148,7 +148,7 @@ func TestProcessMainFilesInPlaceFailFn(t *testing.T) { require.NoError(t, err) p := Program{ - Fn: func(io.Reader, io.Writer) error { return fmt.Errorf("oh no") }, + Fn: func(io.Reader, io.Writer, Options) error { return fmt.Errorf("oh no") }, Inplace: true, } @@ -161,7 +161,7 @@ func TestProcessMainFilesInPlaceFailFn(t *testing.T) { require.Equal(t, "content 1", string(v1)) } -func dummyFileFn(r io.Reader, w io.Writer) error { +func dummyFileFn(r io.Reader, w io.Writer, o Options) error { b, err := ioutil.ReadAll(r) if err != nil { return err From 233798dcfd3dfc24792a93a3b2db272021e67dc3 Mon Sep 17 00:00:00 2001 From: rasta-rocket Date: Sun, 15 Jan 2023 21:53:54 +0100 Subject: [PATCH 2/2] feat(tomll): add multiLineArray flag to linter --- cmd/tomll/main.go | 10 +++++++++- cmd/tomll/main_test.go | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cmd/tomll/main.go b/cmd/tomll/main.go index f84aef3f..c52506ba 100644 --- a/cmd/tomll/main.go +++ b/cmd/tomll/main.go @@ -18,6 +18,7 @@ package main import ( + "flag" "io" "github.com/pelletier/go-toml/v2" @@ -33,13 +34,18 @@ Reading and updating a list of files in place: tomll a.toml b.toml c.toml When given a list of files, tomll will modify all files in place without asking. + +Flags: +-multiLineArray sets up the linter to encode arrays with more than one element on multiple lines instead of one. ` func main() { + multiLineArray := flag.Bool("multiLineArray", false, "sets up the linter to encode arrays with more than one element on multiple lines insteadof one.") p := cli.Program{ Usage: usage, Fn: convert, Inplace: true, + Opts: cli.Options{"multiLineArray": multiLineArray}, } p.Execute() } @@ -47,6 +53,8 @@ func main() { func convert(r io.Reader, w io.Writer, o cli.Options) error { var v interface{} + multiLineArray := o["multiLineArray"].(bool) + d := toml.NewDecoder(r) err := d.Decode(&v) if err != nil { @@ -54,5 +62,5 @@ func convert(r io.Reader, w io.Writer, o cli.Options) error { } e := toml.NewEncoder(w) - return e.Encode(v) + return e.SetArraysMultiline(multiLineArray).Encode(v) } diff --git a/cmd/tomll/main_test.go b/cmd/tomll/main_test.go index 20ce8b6b..b0c85871 100644 --- a/cmd/tomll/main_test.go +++ b/cmd/tomll/main_test.go @@ -36,7 +36,7 @@ a = 42.0 for _, e := range examples { b := new(bytes.Buffer) - err := convert(strings.NewReader(e.input), b, cli.Options{}) + err := convert(strings.NewReader(e.input), b, cli.Options{"multiLineArray": false}) if e.errors { require.Error(t, err) } else {