func BackupFolder()

in pkg/backup/backup.go [16:46]


func BackupFolder(dir string) error {
	terraformFile, err := afero.Glob(filesystem.Fs, filepath.Join(dir, "*.tf"))
	if err != nil {
		return fmt.Errorf("cannot list terraform files in %s:%+v", dir, err)
	}
	for _, file := range terraformFile {
		backupFile := file + BackupExtension
		exist, err := afero.Exists(filesystem.Fs, backupFile)
		if err != nil {
			return fmt.Errorf("cannot check backup file %s:%+v", backupFile, err)
		}
		if exist {
			continue
		}
		// create the backup file, then copy the content of the terraform file to the backup file, with the same permission
		content, err := afero.ReadFile(filesystem.Fs, file)
		if err != nil {
			return fmt.Errorf("cannot read terraform file %s:%+v", file, err)
		}
		// get permission of the terraform file
		info, err := filesystem.Fs.Stat(file)
		if err != nil {
			return fmt.Errorf("cannot get permission of terraform file %s:%+v", file, err)
		}
		// write the content to the backup file
		if err = afero.WriteFile(filesystem.Fs, backupFile, content, info.Mode()); err != nil {
			return fmt.Errorf("cannot write backup file %s:%+v", backupFile, err)
		}
	}
	return nil
}