-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #721
- Loading branch information
Showing
7 changed files
with
244 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Tomll is a linter for TOML | ||
// | ||
// Usage: | ||
// cat file.toml | tomll > file_linted.toml | ||
// tomll file1.toml file2.toml # lint the two files in place | ||
package main | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/pelletier/go-toml/v2" | ||
"github.com/pelletier/go-toml/v2/internal/cli" | ||
) | ||
|
||
const usage = `tomll can be used in two ways: | ||
Reading from stdin, writing to stdout: | ||
cat file.toml | tomll > file.toml | ||
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. | ||
` | ||
|
||
func main() { | ||
p := cli.Program{ | ||
Usage: usage, | ||
Fn: convert, | ||
Inplace: true, | ||
} | ||
p.Execute() | ||
} | ||
|
||
func convert(r io.Reader, w io.Writer) error { | ||
var v interface{} | ||
|
||
d := toml.NewDecoder(r) | ||
err := d.Decode(&v) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
e := toml.NewEncoder(w) | ||
return e.Encode(v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConvert(t *testing.T) { | ||
examples := []struct { | ||
name string | ||
input string | ||
expected string | ||
errors bool | ||
}{ | ||
{ | ||
name: "valid toml", | ||
input: ` | ||
mytoml.a = 42.0 | ||
`, | ||
expected: `[mytoml] | ||
a = 42.0 | ||
`, | ||
}, | ||
{ | ||
name: "invalid toml", | ||
input: `[what`, | ||
errors: true, | ||
}, | ||
} | ||
|
||
for _, e := range examples { | ||
b := new(bytes.Buffer) | ||
err := convert(strings.NewReader(e.input), b) | ||
if e.errors { | ||
require.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, e.expected, b.String()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,76 @@ | ||
package cli | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
type ConvertFn func(r io.Reader, w io.Writer) error | ||
|
||
func Execute(usage string, fn ConvertFn) { | ||
flag.Usage = func() { fmt.Fprintf(os.Stderr, usage) } | ||
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 | ||
} | ||
|
||
func (p *Program) Execute() { | ||
flag.Usage = func() { fmt.Fprintf(os.Stderr, p.Usage) } | ||
flag.Parse() | ||
os.Exit(processMain(flag.Args(), os.Stdin, os.Stdout, os.Stderr, fn)) | ||
os.Exit(p.main(flag.Args(), os.Stdin, os.Stdout, os.Stderr)) | ||
} | ||
|
||
func processMain(files []string, input io.Reader, output, error io.Writer, f ConvertFn) int { | ||
err := run(files, input, output, f) | ||
func (p *Program) main(files []string, input io.Reader, output, error io.Writer) int { | ||
err := p.run(files, input, output) | ||
if err != nil { | ||
fmt.Fprintln(error, err.Error()) | ||
return -1 | ||
} | ||
return 0 | ||
} | ||
|
||
func run(files []string, input io.Reader, output io.Writer, convert ConvertFn) error { | ||
func (p *Program) run(files []string, input io.Reader, output io.Writer) error { | ||
if len(files) > 0 { | ||
if p.Inplace { | ||
return p.runAllFilesInPlace(files) | ||
} | ||
f, err := os.Open(files[0]) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
input = f | ||
} | ||
return convert(input, output) | ||
return p.Fn(input, output) | ||
} | ||
|
||
func (p *Program) runAllFilesInPlace(files []string) error { | ||
for _, path := range files { | ||
err := p.runFileInPlace(path) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (p *Program) runFileInPlace(path string) error { | ||
in, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
out := new(bytes.Buffer) | ||
|
||
err = p.Fn(bytes.NewReader(in), out) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ioutil.WriteFile(path, out.Bytes(), 0600) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters