func CopyDirWithTemplates()

in pkg/osutil/osutil.go [164:214]


func CopyDirWithTemplates(
	fileSys fs.FS,
	src, dest string,
	draftConfig *config.DraftConfig,
	templateWriter templatewriter.TemplateWriter) error {

	files, err := fs.ReadDir(fileSys, src)
	if err != nil {
		return err
	}

	for _, f := range files {

		if f.Name() == configFileName {
			continue
		}

		fileName := f.Name()
		if overrideName, ok := draftConfig.FileNameOverrideMap[f.Name()]; ok {
			fileName = overrideName
		}

		srcPath := path.Join(src, f.Name())
		destPath := path.Join(dest, fileName)
		log.Debugf("Source path: %s Dest path: %s", srcPath, destPath)

		variableMap := draftConfig.GetVariableMap()
		if len(variableMap) == 0 {
			return fmt.Errorf("variable map is empty, unable to replace template variables")
		}

		if f.IsDir() {
			if err = templateWriter.EnsureDirectory(destPath); err != nil {
				return err
			}
			if err = CopyDirWithTemplates(fileSys, srcPath, destPath, draftConfig, templateWriter); err != nil {
				return err
			}
		} else {
			fileContent, err := replaceGoTemplateVariables(fileSys, srcPath, variableMap)
			if err != nil {
				return err
			}

			if err = templateWriter.WriteFile(destPath, fileContent); err != nil {
				return err
			}
		}
	}
	return nil
}