mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
/*
|
|
Copyright 2014 CoreOS, Inc.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package command
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"io/ioutil"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
ErrNoAvailSrc = errors.New("no available argument and stdin")
|
|
)
|
|
|
|
// trimsplit slices s into all substrings separated by sep and returns a
|
|
// slice of the substrings between the separator with all leading and trailing
|
|
// white space removed, as defined by Unicode.
|
|
func trimsplit(s, sep string) []string {
|
|
raw := strings.Split(s, ",")
|
|
trimmed := make([]string, 0)
|
|
for _, r := range raw {
|
|
trimmed = append(trimmed, strings.TrimSpace(r))
|
|
}
|
|
return trimmed
|
|
}
|
|
|
|
func argOrStdin(args []string, stdin io.Reader, i int) (string, error) {
|
|
if i < len(args) {
|
|
return args[i], nil
|
|
}
|
|
bytes, err := ioutil.ReadAll(stdin)
|
|
if string(bytes) == "" || err != nil {
|
|
return "", ErrNoAvailSrc
|
|
}
|
|
return string(bytes), nil
|
|
}
|