jsontoml: port to v2 (#726)

Fixes #719
This commit is contained in:
Thomas Pelletier
2021-12-31 14:40:20 -05:00
committed by GitHub
parent 82f8dad811
commit d8ddc00c61
7 changed files with 254 additions and 175 deletions
+37
View File
@@ -0,0 +1,37 @@
package cli
import (
"flag"
"fmt"
"io"
"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) }
flag.Parse()
os.Exit(processMain(flag.Args(), os.Stdin, os.Stdout, os.Stderr, fn))
}
func processMain(files []string, input io.Reader, output, error io.Writer, f ConvertFn) int {
err := run(files, input, output, f)
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 {
if len(files) > 0 {
f, err := os.Open(files[0])
if err != nil {
return err
}
defer f.Close()
input = f
}
return convert(input, output)
}