You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.1 KiB

  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "os"
  7. . "github.com/tendermint/tendermint/common"
  8. )
  9. const Version = "0.0.1"
  10. const readBufferSize = 1024
  11. // Parse command-line options
  12. func parseFlags() (outpath string, version bool) {
  13. flag.StringVar(&outpath, "outpath", "stdinwriter.out", "Output file name")
  14. flag.BoolVar(&version, "version", false, "Version")
  15. flag.Parse()
  16. return
  17. }
  18. func main() {
  19. // Read options
  20. outpath, version := parseFlags()
  21. if version {
  22. fmt.Println(Fmt("stdinwriter version %v", Version))
  23. return
  24. }
  25. outfile, err := OpenAutoFile(outpath)
  26. if err != nil {
  27. fmt.Println(Fmt("stdinwriter couldn't create outfile %v", outfile))
  28. os.Exit(1)
  29. }
  30. go writeToOutfile(outfile)
  31. // Trap signal
  32. TrapSignal(func() {
  33. outfile.Close()
  34. fmt.Println("stdinwriter shutting down")
  35. })
  36. }
  37. func writeToOutfile(outfile *AutoFile) {
  38. // Forever, read from stdin and write to AutoFile.
  39. buf := make([]byte, readBufferSize)
  40. for {
  41. n, err := os.Stdin.Read(buf)
  42. outfile.Write(buf[:n])
  43. if err != nil {
  44. outfile.Close()
  45. if err == io.EOF {
  46. os.Exit(0)
  47. } else {
  48. fmt.Println("stdinwriter errored")
  49. os.Exit(1)
  50. }
  51. }
  52. }
  53. }