func convertFiles[C convertible]()

in cmd/alzlibtool/command/convert/convert.go [46:96]


func convertFiles[C convertible](src, dst string, cmd *cobra.Command, valid checker.Validator) error {
	if _, err := os.ReadDir(dst); err != nil {
		if errors.Is(err, os.ErrNotExist) {
			if err := os.MkdirAll(dst, 0755); err != nil {
				return fmt.Errorf("convert: error creating destination directory: %w", err)
			}
		} else {
			return fmt.Errorf("convert: error reading destination directory: %w", err)
		}
	}
	return filepath.WalkDir(src, func(path string, d os.DirEntry, err error) error {
		if err != nil {
			return err
		}
		if d.IsDir() {
			return nil
		}
		if filepath.Ext(path) != ".json" {
			return nil
		}

		base := filepath.Base(path)
		baseNoExt := strings.TrimSuffix(base, filepath.Ext(base))
		if sovCloud := filepath.Ext(baseNoExt); sovCloud == ".AzureChinaCloud" || sovCloud == ".AzureUSGovernment" {
			return nil
		}

		bytes, err := os.ReadFile(path)
		if err != nil {
			return fmt.Errorf("readFile error: '%s': %w", path, err)
		}
		resource := new(C)
		if err := json.Unmarshal(bytes, resource); err != nil {
			return fmt.Errorf("json.Ummarshal error: '%s', %w", path, err)
		}

		if err := valid.Validate(resource); err != nil {
			return fmt.Errorf("validation error: '%s', %w", path, err)
		}
		processedBytes := processResource(resource)
		destination := filepath.Join(dst, libraryFileName(resource))
		cmd.Printf("Processing %s => %s\n", path, destination)
		if _, err := os.Stat(destination); err == nil && !convertCmdOverwrite {
			return fmt.Errorf("destination file already exists and overwrite not set: '%s'", destination)
		}
		if err := os.WriteFile(destination, processedBytes, 0644); err != nil {
			return fmt.Errorf("error writing %s: %w", destination, err)
		}
		return nil
	})
}