refactor function in order to support --dry-run