Add no-write option to jlp.

This commit is contained in:
Kyle Isom 2016-07-01 15:56:52 -07:00
parent ce9604b4e1
commit c8cdd70a08
1 changed files with 14 additions and 4 deletions

View File

@ -10,7 +10,7 @@ import (
"github.com/kisom/goutils/lib" "github.com/kisom/goutils/lib"
) )
func prettify(file string) error { func prettify(file string, validateOnly bool) error {
in, err := ioutil.ReadFile(file) in, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
lib.Warn(err, "ReadFile") lib.Warn(err, "ReadFile")
@ -24,6 +24,10 @@ func prettify(file string) error {
return err return err
} }
if validateOnly {
return nil
}
err = ioutil.WriteFile(file, buf.Bytes(), 0644) err = ioutil.WriteFile(file, buf.Bytes(), 0644)
if err != nil { if err != nil {
lib.Warn(err, "WriteFile") lib.Warn(err, "WriteFile")
@ -32,7 +36,7 @@ func prettify(file string) error {
return err return err
} }
func compact(file string) error { func compact(file string, validateOnly bool) error {
in, err := ioutil.ReadFile(file) in, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
lib.Warn(err, "ReadFile") lib.Warn(err, "ReadFile")
@ -46,6 +50,10 @@ func compact(file string) error {
return err return err
} }
if validateOnly {
return nil
}
err = ioutil.WriteFile(file, buf.Bytes(), 0644) err = ioutil.WriteFile(file, buf.Bytes(), 0644)
if err != nil { if err != nil {
lib.Warn(err, "WriteFile") lib.Warn(err, "WriteFile")
@ -63,6 +71,7 @@ func usage() {
Flags: Flags:
-c Compact files. -c Compact files.
-h Print this help message. -h Print this help message.
-n Don't prettify; only perform validation.
`, progname, progname) `, progname, progname)
} }
@ -72,8 +81,9 @@ func init() {
} }
func main() { func main() {
var shouldCompact bool var shouldCompact, validateOnly bool
flag.BoolVar(&shouldCompact, "c", false, "Compact files instead of prettifying.") flag.BoolVar(&shouldCompact, "c", false, "Compact files instead of prettifying.")
flag.BoolVar(&validateOnly, "n", false, "Don't write changes; only perform validation.")
flag.Parse() flag.Parse()
action := prettify action := prettify
@ -83,7 +93,7 @@ func main() {
var errCount int var errCount int
for _, fileName := range flag.Args() { for _, fileName := range flag.Args() {
err := action(fileName) err := action(fileName, validateOnly)
if err != nil { if err != nil {
errCount++ errCount++
} }