util.go 792 B

1234567891011121314151617181920212223242526272829303132333435
  1. package command
  2. import (
  3. "errors"
  4. "io"
  5. "io/ioutil"
  6. "strings"
  7. )
  8. var (
  9. ErrNoAvailSrc = errors.New("no available argument and stdin")
  10. )
  11. // trimsplit slices s into all substrings separated by sep and returns a
  12. // slice of the substrings between the separator with all leading and trailing
  13. // white space removed, as defined by Unicode.
  14. func trimsplit(s, sep string) []string {
  15. raw := strings.Split(s, ",")
  16. trimmed := make([]string, 0)
  17. for _, r := range raw {
  18. trimmed = append(trimmed, strings.TrimSpace(r))
  19. }
  20. return trimmed
  21. }
  22. func argOrStdin(args []string, stdin io.Reader, i int) (string, error) {
  23. if i < len(args) {
  24. return args[i], nil
  25. }
  26. bytes, err := ioutil.ReadAll(stdin)
  27. if string(bytes) == "" || err != nil {
  28. return "", ErrNoAvailSrc
  29. }
  30. return string(bytes), nil
  31. }