util.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package command
  14. import (
  15. "errors"
  16. "io"
  17. "io/ioutil"
  18. "strings"
  19. )
  20. var (
  21. ErrNoAvailSrc = errors.New("no available argument and stdin")
  22. )
  23. // trimsplit slices s into all substrings separated by sep and returns a
  24. // slice of the substrings between the separator with all leading and trailing
  25. // white space removed, as defined by Unicode.
  26. func trimsplit(s, sep string) []string {
  27. raw := strings.Split(s, ",")
  28. trimmed := make([]string, 0)
  29. for _, r := range raw {
  30. trimmed = append(trimmed, strings.TrimSpace(r))
  31. }
  32. return trimmed
  33. }
  34. func argOrStdin(args []string, stdin io.Reader, i int) (string, error) {
  35. if i < len(args) {
  36. return args[i], nil
  37. }
  38. bytes, err := ioutil.ReadAll(stdin)
  39. if string(bytes) == "" || err != nil {
  40. return "", ErrNoAvailSrc
  41. }
  42. return string(bytes), nil
  43. }