Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 64ff1ea4d5 | |||
| b39f6ef1f9 | |||
| c187221f01 |
@@ -0,0 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/pelletier/go-toml"
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintln(os.Stderr, `tomll can be used in two ways:
|
||||
Writing to STDIN and reading from STDOUT:
|
||||
cat file.toml | tomll > file.toml
|
||||
|
||||
Reading and updating a list of files:
|
||||
tomll a.toml b.toml c.toml
|
||||
|
||||
When given a list of files, tomll will modify all files in place without asking.
|
||||
`)
|
||||
}
|
||||
flag.Parse()
|
||||
// read from stdin and print to stdout
|
||||
if flag.NArg() == 0 {
|
||||
s, err := lintReader(os.Stdin)
|
||||
if err != nil {
|
||||
io.WriteString(os.Stderr, err.Error())
|
||||
os.Exit(-1)
|
||||
}
|
||||
io.WriteString(os.Stdout, s)
|
||||
} else {
|
||||
// otherwise modify a list of files
|
||||
for _, filename := range flag.Args() {
|
||||
s, err := lintFile(filename)
|
||||
if err != nil {
|
||||
io.WriteString(os.Stderr, err.Error())
|
||||
os.Exit(-1)
|
||||
}
|
||||
ioutil.WriteFile(filename, []byte(s), 0644)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func lintFile(filename string) (string, error) {
|
||||
tree, err := toml.LoadFile(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return tree.String(), nil
|
||||
}
|
||||
|
||||
func lintReader(r io.Reader) (string, error) {
|
||||
tree, err := toml.LoadReader(r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return tree.String(), nil
|
||||
}
|
||||
@@ -234,6 +234,7 @@ func (l *tomlLexer) lexRvalue() tomlLexStateFn {
|
||||
return l.lexKey
|
||||
}
|
||||
|
||||
return l.errorf("no value can start with %c", next)
|
||||
}
|
||||
|
||||
l.emit(tokenEOF)
|
||||
|
||||
@@ -692,3 +692,17 @@ func TestInvalidFloat(t *testing.T) {
|
||||
token{Position{1, 7}, tokenEOF, ""},
|
||||
})
|
||||
}
|
||||
|
||||
func TestLexUnknownRvalue(t *testing.T) {
|
||||
testFlow(t, `a = !b`, []token{
|
||||
token{Position{1, 1}, tokenKey, "a"},
|
||||
token{Position{1, 3}, tokenEqual, "="},
|
||||
token{Position{1, 5}, tokenError, "no value can start with !"},
|
||||
})
|
||||
|
||||
testFlow(t, `a = \b`, []token{
|
||||
token{Position{1, 1}, tokenKey, "a"},
|
||||
token{Position{1, 3}, tokenEqual, "="},
|
||||
token{Position{1, 5}, tokenError, `no value can start with \`},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -299,6 +299,18 @@ func TestArrayNestedStrings(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestParseUnknownRvalue(t *testing.T) {
|
||||
_, err := Load("a = !bssss")
|
||||
if err == nil {
|
||||
t.Error("Expecting a parse error")
|
||||
}
|
||||
|
||||
_, err = Load("a = /b")
|
||||
if err == nil {
|
||||
t.Error("Expecting a parse error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMissingValue(t *testing.T) {
|
||||
_, err := Load("a = ")
|
||||
if err.Error() != "(1, 5): expecting a value" {
|
||||
|
||||
@@ -108,9 +108,14 @@ func (t *TomlTree) toToml(indent, keyspace string) string {
|
||||
return result
|
||||
}
|
||||
|
||||
// ToString is an alias for String
|
||||
func (t *TomlTree) ToString() string {
|
||||
return t.String()
|
||||
}
|
||||
|
||||
// ToString generates a human-readable representation of the current tree.
|
||||
// Output spans multiple lines, and is suitable for ingest by a TOML parser
|
||||
func (t *TomlTree) ToString() string {
|
||||
func (t *TomlTree) String() string {
|
||||
return t.toToml("", "")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user