Skip to content

Allow user to decline to overwrite existing file, from use_template() #348

Closed

Description

@boshek

Currently use_template throws an error when one chooses not to overwrite an existing file:

usethis::use_template(template = "CONTRIBUTING.md")
Overwrite pre-existing file 'CONTRIBUTING.md'?
1: Absolutely
2: No
3: Not now
 
Selection: 3
Error: 'CONTRIBUTING.md' already exists.

Is there any appetite for a PR that would add a allow_existing argument (or whatever the name) to use_template? So that is, if a file already exists and the allow_existing argument is set to TRUE, and a user selects to not overwrite (at the prompt), use_template simply outputs a 'CONTRIBUTING.md' already exists' warning rather than an error?

The context is that we have a series of templates (CoC, README, etc) where a user, when setting up a project or package, may accept the template for some and keep existing files for others. Since use_template via write_over throws an error, this type of process where sequential asking which files need to be overwritten is currently not possible.

Activity

  1. jennybc commented on May 11, 2018

    @jennybc
    Member

    I agree it makes sense to be able to opt-out of overwrite and not throw an error. However, my mind is very much elsewhere right now.

    Before making a PR, will you give a few bullet points re: what you plan to do so @hadley can also give a quick 👍?

    The objective is to make sure the change really needs to happen in usethis vs. in your package (without forcing you to do some awkward tryCatch() thing). Let's make sure the accommodations are made in the most sensible place(s).

  2. boshek commented on May 11, 2018

    @boshek
    ContributorAuthor

    awkward tryCatch()

    Definitely where I am at right now

    Ok here is a shot at what I would try:

    • Add this into write_over here:
    if (!can_overwrite(full_path)){
      ## Keep existing file AND allow_existing = FALSE (default)
      if(!allow_existing) stop(value(path), " already exists.", call. = FALSE)
      
      ## Keep existing file AND allow_existing = TRUE
      if(allow_existing) warning(value(path), " already exists.")
    }
    
    • Then add a allow_existing argument to write_over and use_template
    • Add param documentation to allow_existing for use_template
  3. jennybc commented on May 12, 2018

    @jennybc
    Member

    I don't see why this needs to stop() if interactive(), i.e. a human was presented with and made a choice. I propose you make a PR with no new argument. If !can_overwrite() and interactive() then warn or message and return FALSE from write_over().

  4. added a commit that references this issue on May 14, 2018
    75a4f41
  5. added this to the v1.4.0 milestone on May 25, 2018
  6. added
    featurea feature request or enhancement
    wipwork in progress
    on May 28, 2018
  7. changed the title [-]Feature request - argument to allow use_template warning on existing files[/-] [+]Allow user to decline to overwrite existing file, from use_template()[/+] on Aug 12, 2018
  8. added a commit that references this issue on Aug 13, 2018
    3046fde
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancementwipwork in progress

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions