Merge pull request #38683 from bruceauyeung/k8s-branch-fix-possible-nil-error
Automatic merge from submit-queue avoid repeated length calculation and some other code improvements **What this PR does / why we need it**: 1. in function `ParsePairs`, calculating `invalidBuf`'s length over and over again brings performance penalty. a `invalidBufNonEmpty` bool value can fix this. 2. pairArg is not a string template and also there is no other arguments for `fmt.Sprintf`, so i remove `fmt.Sprintf` 3. in function `DumpReaderToFile`, we must check nil error first before defer statement, otherwise there maybe a potential nil error on `f.Close()` 4. add nil checks into `GetWideFlag` function 5. some other minor code improvements for better readability. Signed-off-by: bruceauyeung <ouyang.qinhua@zte.com.cn>
This commit is contained in:
		@@ -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
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user