mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-02 08:17:26 +00:00
avoid repeated length calculation and some other code improvements
Signed-off-by: bruceauyeung <ouyang.qinhua@zte.com.cn>
This commit is contained in:
parent
419d38a965
commit
eac48c2cd5
@ -301,10 +301,7 @@ func UsageError(cmd *cobra.Command, format string, args ...interface{}) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func IsFilenameEmpty(filenames []string) bool {
|
func IsFilenameEmpty(filenames []string) bool {
|
||||||
if len(filenames) == 0 {
|
return len(filenames) == 0
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Whether this cmd need watching objects.
|
// Whether this cmd need watching objects.
|
||||||
@ -349,7 +346,7 @@ func GetFlagStringArray(cmd *cobra.Command, flag string) []string {
|
|||||||
// GetWideFlag is used to determine if "-o wide" is used
|
// GetWideFlag is used to determine if "-o wide" is used
|
||||||
func GetWideFlag(cmd *cobra.Command) bool {
|
func GetWideFlag(cmd *cobra.Command) bool {
|
||||||
f := cmd.Flags().Lookup("output")
|
f := cmd.Flags().Lookup("output")
|
||||||
if f.Value.String() == "wide" {
|
if f != nil && f.Value != nil && f.Value.String() == "wide" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@ -452,10 +449,11 @@ func Merge(codec runtime.Codec, dst runtime.Object, fragment, kind string) (runt
|
|||||||
// (usually for temporary use).
|
// (usually for temporary use).
|
||||||
func DumpReaderToFile(reader io.Reader, filename string) error {
|
func DumpReaderToFile(reader io.Reader, filename string) error {
|
||||||
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||||
defer f.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
buffer := make([]byte, 1024)
|
buffer := make([]byte, 1024)
|
||||||
for {
|
for {
|
||||||
count, err := reader.Read(buffer)
|
count, err := reader.Read(buffer)
|
||||||
@ -604,28 +602,30 @@ func ParsePairs(pairArgs []string, pairType string, supportRemove bool) (newPair
|
|||||||
removePairs = []string{}
|
removePairs = []string{}
|
||||||
}
|
}
|
||||||
var invalidBuf bytes.Buffer
|
var invalidBuf bytes.Buffer
|
||||||
|
var invalidBufNonEmpty bool
|
||||||
for _, pairArg := range pairArgs {
|
for _, pairArg := range pairArgs {
|
||||||
if strings.Index(pairArg, "=") != -1 {
|
if strings.Contains(pairArg, "=") {
|
||||||
parts := strings.SplitN(pairArg, "=", 2)
|
parts := strings.SplitN(pairArg, "=", 2)
|
||||||
if len(parts) != 2 {
|
if len(parts) != 2 {
|
||||||
if invalidBuf.Len() > 0 {
|
if invalidBufNonEmpty {
|
||||||
invalidBuf.WriteString(", ")
|
invalidBuf.WriteString(", ")
|
||||||
}
|
}
|
||||||
invalidBuf.WriteString(fmt.Sprintf(pairArg))
|
invalidBuf.WriteString(pairArg)
|
||||||
|
invalidBufNonEmpty = true
|
||||||
} else {
|
} else {
|
||||||
newPairs[parts[0]] = parts[1]
|
newPairs[parts[0]] = parts[1]
|
||||||
}
|
}
|
||||||
} else if supportRemove && strings.HasSuffix(pairArg, "-") {
|
} else if supportRemove && strings.HasSuffix(pairArg, "-") {
|
||||||
removePairs = append(removePairs, pairArg[:len(pairArg)-1])
|
removePairs = append(removePairs, pairArg[:len(pairArg)-1])
|
||||||
} else {
|
} else {
|
||||||
if invalidBuf.Len() > 0 {
|
if invalidBufNonEmpty {
|
||||||
invalidBuf.WriteString(", ")
|
invalidBuf.WriteString(", ")
|
||||||
}
|
}
|
||||||
invalidBuf.WriteString(fmt.Sprintf(pairArg))
|
invalidBuf.WriteString(pairArg)
|
||||||
|
invalidBufNonEmpty = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if invalidBuf.Len() > 0 {
|
if invalidBufNonEmpty {
|
||||||
err = fmt.Errorf("invalid %s format: %s", pairType, invalidBuf.String())
|
err = fmt.Errorf("invalid %s format: %s", pairType, invalidBuf.String())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user