Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi line array options #838

Open
wants to merge 2 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/jsontoml/main.go
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion cmd/jsontoml/main_test.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/tomljson/main.go
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion cmd/tomljson/main_test.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions cmd/tomll/main.go
Expand Up @@ -18,6 +18,7 @@
package main

import (
"flag"
"io"

"github.com/pelletier/go-toml/v2"
Expand All @@ -33,26 +34,33 @@ 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()
}

func convert(r io.Reader, w io.Writer) error {
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 {
return err
}

e := toml.NewEncoder(w)
return e.Encode(v)
return e.SetArraysMultiline(multiLineArray).Encode(v)
}
4 changes: 3 additions & 1 deletion cmd/tomll/main_test.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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{"multiLineArray": false})
if e.errors {
require.Error(t, err)
} else {
Expand Down
9 changes: 6 additions & 3 deletions internal/cli/cli.go
Expand Up @@ -12,14 +12,17 @@ import (
"github.com/pelletier/go-toml/v2"
)

type ConvertFn func(r io.Reader, w io.Writer) error
type Options map[string]interface{}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now I'd like this to be a struct, to lean on go's default values to avoid exists/empty checks and casting, and make it easier to understand which options are supported.

Because Program is used by multiple programs, a single Options struct may be unlikely to work with all of them. I think the easiest way around this is to have convert() take an options struct, and return a function, creating a closure around the provided options. That way each program can build its own options an generate its own processing function, without the need for a one-size-fits-all options type.


type ConvertFn func(r io.Reader, w io.Writer, options Options) error

type Program struct {
Usage string
Fn ConvertFn
// 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() {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions internal/cli/cli_test.go
Expand Up @@ -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
})

Expand All @@ -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")
})

Expand All @@ -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)
})
Expand All @@ -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
})

Expand All @@ -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
})

Expand Down Expand Up @@ -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,
}

Expand All @@ -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
Expand Down