limit stream of bytes
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.

32 lines
529 B

  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "github.com/dustin/go-humanize"
  7. )
  8. const usage = "lim <number of bytes, default 1MB>"
  9. func main() {
  10. size := int64(1 * 1024 * 1024)
  11. switch len(os.Args) {
  12. case 1:
  13. case 2:
  14. dest, err := humanize.ParseBytes(os.Args[1])
  15. if err != nil {
  16. fmt.Fprintf(os.Stderr, "unable to parse quantity: %v\n%v\n", err, usage)
  17. os.Exit(1)
  18. }
  19. size = int64(dest)
  20. default:
  21. fmt.Fprintf(os.Stderr, "%v\n", usage)
  22. os.Exit(1)
  23. }
  24. lr := io.LimitReader(os.Stdin, size)
  25. io.Copy(os.Stdout, lr)
  26. }