main.go 682 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package main
  2. import (
  3. "errors"
  4. "flag"
  5. "io/ioutil"
  6. "os"
  7. "github.com/golang/snappy"
  8. )
  9. var (
  10. decode = flag.Bool("d", false, "decode")
  11. encode = flag.Bool("e", false, "encode")
  12. )
  13. func run() error {
  14. flag.Parse()
  15. if *decode == *encode {
  16. return errors.New("exactly one of -d or -e must be given")
  17. }
  18. in, err := ioutil.ReadAll(os.Stdin)
  19. if err != nil {
  20. return err
  21. }
  22. out := []byte(nil)
  23. if *decode {
  24. out, err = snappy.Decode(nil, in)
  25. if err != nil {
  26. return err
  27. }
  28. } else {
  29. out = snappy.Encode(nil, in)
  30. }
  31. _, err = os.Stdout.Write(out)
  32. return err
  33. }
  34. func main() {
  35. if err := run(); err != nil {
  36. os.Stderr.WriteString(err.Error() + "\n")
  37. os.Exit(1)
  38. }
  39. }