func validateFiles()

in dubboctl/pkg/validate/validate.go [147:255]


func validateFiles(dubboNamespace *string, files []string, writer io.Writer) error {
	if len(files) == 0 {
		return errFiles
	}
	v := &validator{}
	var errs error
	var reader io.ReadCloser
	warningsByFilename := map[string]validation.Warning{}
	processFile := func(path string) {
		var err error
		if path == "-" {
			reader = io.NopCloser(os.Stdin)
		} else {
			reader, err = os.Open(path)
			if err != nil {
				errs = multierror.Append(errs, fmt.Errorf("cannot read file %q: %v", path, err))
				return
			}
		}
		warning, err := v.validateFile(path, dubboNamespace, reader, writer)
		if err != nil {
			errs = multierror.Append(errs, err)
		}
		err = reader.Close()
		if err != nil {
			fmt.Printf("file: %s is not closed: %v", path, err)
		}
		warningsByFilename[path] = warning
	}
	processDirectory := func(directory string, processFile func(string)) error {
		err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
			if err != nil {
				return err
			}

			if info.IsDir() {
				return nil
			}

			if isFileFormatValid(path) {
				processFile(path)
			}

			return nil
		})
		return err
	}

	processedFiles := map[string]bool{}
	for _, filename := range files {
		var isDir bool
		if filename != "-" {
			fi, err := os.Stat(filename)
			if err != nil {
				errs = multierror.Append(errs, fmt.Errorf("cannot stat file %q: %v", filename, err))
				continue
			}
			isDir = fi.IsDir()
		}

		if !isDir {
			processFile(filename)
			processedFiles[filename] = true
			continue
		}
		if err := processDirectory(filename, func(path string) {
			processFile(path)
			processedFiles[path] = true
		}); err != nil {
			errs = multierror.Append(errs, err)
		}
	}

	files = []string{}
	for p := range processedFiles {
		files = append(files, p)
	}

	if errs != nil {
		for _, fname := range files {
			if w := warningsByFilename[fname]; w != nil {
				if fname == "-" {
					_, _ = fmt.Fprint(writer, warningToString(w))
					break
				}
				_, _ = fmt.Fprintf(writer, "%q has warnings: %v\n", fname, warningToString(w))
			}
		}
		return errs
	}
	for _, fname := range files {
		if fname == "-" {
			if w := warningsByFilename[fname]; w != nil {
				_, _ = fmt.Fprint(writer, warningToString(w))
			} else {
				_, _ = fmt.Fprintf(writer, "validation successfully completed\n")
			}
			break
		}

		if w := warningsByFilename[fname]; w != nil {
			_, _ = fmt.Fprintf(writer, "%q has warnings: %v\n", fname, warningToString(w))
		} else {
			_, _ = fmt.Fprintf(writer, "%q is valid\n", fname)
		}
	}

	return nil
}