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

feat(tomll): add multiLineArray flag to linter #578

Merged
merged 1 commit into from Sep 6, 2021
Merged
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
30 changes: 24 additions & 6 deletions cmd/tomll/main.go
Expand Up @@ -6,6 +6,7 @@
package main

import (
"bytes"
"flag"
"fmt"
"io"
Expand All @@ -16,6 +17,7 @@ import (
)

func main() {
multiLineArray := flag.Bool("multiLineArray", false, "sets up the linter to encode arrays with more than one element on multiple lines instead of one.")
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "tomll can be used in two ways:")
fmt.Fprintln(os.Stderr, "Writing to STDIN and reading from STDOUT:")
Expand All @@ -25,11 +27,16 @@ func main() {
fmt.Fprintln(os.Stderr, " tomll a.toml b.toml c.toml")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "When given a list of files, tomll will modify all files in place without asking.")
fmt.Fprintln(os.Stderr, "When given a list of files, tomll will modify all files in place without asking.")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Flags:")
fmt.Fprintln(os.Stderr, "-multiLineArray sets up the linter to encode arrays with more than one element on multiple lines instead of one.")
}
flag.Parse()

// read from stdin and print to stdout
if flag.NArg() == 0 {
s, err := lintReader(os.Stdin)
s, err := lintReader(os.Stdin, *multiLineArray)
if err != nil {
io.WriteString(os.Stderr, err.Error())
os.Exit(-1)
Expand All @@ -38,7 +45,7 @@ func main() {
} else {
// otherwise modify a list of files
for _, filename := range flag.Args() {
s, err := lintFile(filename)
s, err := lintFile(filename, *multiLineArray)
if err != nil {
io.WriteString(os.Stderr, err.Error())
os.Exit(-1)
Expand All @@ -48,18 +55,29 @@ func main() {
}
}

func lintFile(filename string) (string, error) {
func lintFile(filename string, multiLineArray bool) (string, error) {
tree, err := toml.LoadFile(filename)
if err != nil {
return "", err
}
return tree.String(), nil

buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).ArraysWithOneElementPerLine(multiLineArray).Encode(tree); err != nil {
panic(err)
}

return buf.String(), nil
}

func lintReader(r io.Reader) (string, error) {
func lintReader(r io.Reader, multiLineArray bool) (string, error) {
tree, err := toml.LoadReader(r)
if err != nil {
return "", err
}
return tree.String(), nil

buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).ArraysWithOneElementPerLine(multiLineArray).Encode(tree); err != nil {
panic(err)
}
return buf.String(), nil
}