+47
-8
@@ -1,22 +1,32 @@
|
||||
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) }
|
||||
flag.Parse()
|
||||
os.Exit(processMain(flag.Args(), os.Stdin, os.Stdout, os.Stderr, fn))
|
||||
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 processMain(files []string, input io.Reader, output, error io.Writer, f ConvertFn) int {
|
||||
err := run(files, input, output, f)
|
||||
func (p *Program) Execute() {
|
||||
flag.Usage = func() { fmt.Fprintf(os.Stderr, p.Usage) }
|
||||
flag.Parse()
|
||||
os.Exit(p.main(flag.Args(), os.Stdin, os.Stdout, os.Stderr))
|
||||
}
|
||||
|
||||
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
|
||||
@@ -24,8 +34,11 @@ func processMain(files []string, input io.Reader, output, error io.Writer, f Con
|
||||
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
|
||||
@@ -33,5 +46,31 @@ func run(files []string, input io.Reader, output io.Writer, convert ConvertFn) e
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user