arthur-shaw / susoapi

R interface for Survey Solutions' APIs
https://arthur-shaw.github.io/susoapi/
Other
9 stars 5 forks source link

set_credentials() clears content in "~/.Renviron" #20

Closed petbrueck closed 2 years ago

petbrueck commented 2 years ago

Thanks a lot for this useful package!

Issue For already existing .Renviron-file in my home directory: If I set my credentials with set_credentials(), any other envars in file .Renviron are erased.

Location of issue I think the issue is with the following ifelse condition for new_env_vars.

    else if (file.exists(renv_path)) {

        old_env_vars <- readLines(con = renv_path) # ingest .Renviron file
        suso_env_entries <- grep(pattern = "SUSO_", x = old_env_vars)
        new_env_vars <- ifelse(
            test = length(suso_env_entries) > 0,
            yes = old_env_vars[-grep(pattern = "SUSO_", x = old_env_vars)],
            no = old_env_vars
        )
        writeLines(text = new_env_vars, con = renv_path)

    }

In case no "SUSO_*" envar existed before it will return new_env_vars==""

Potential(?) fix Just write new .Renviron at this stage in case there are "SUSO_*" envars:

  else if (file.exists(renv_path)) {

    old_env_vars <- readLines(con = renv_path) # ingest .Renviron file
    suso_env_entries <- grep(pattern = "SUSO_", x = old_env_vars)

    if ( length(suso_env_entries) > 0) {
      new_env_vars <- old_env_vars[-suso_env_entries]
      writeLines(text = new_env_vars, con = renv_path)
    }    
  }

Current workaround Set envars SUSO_* manually:

  1. file.edit("~/.Renviron")
  2. Write envars:
    SUSO_SERVER = 'https://xxxxx'
    SUSO_USER = 'YYYY'
    SUSO_PASSWORD = 'ZZZZZ'
arthur-shaw commented 2 years ago

@petbrueck, I'm so sorry my clumsy code scrubbed your .Renviron file. (Sidebar: one day, I'd love to hear what settings you have there 😉 )

Let me try to fix this this weekend.

arthur-shaw commented 2 years ago

@petbrueck , would you mind checking that this resolves the issue on your end? On my machine, this fix seems to work. Also, I wrote a few tests to check that this works in general. That said, I may have missed something. And I certaintly don't want this function deleting the contents of .REnviron files where they exist.

petbrueck commented 2 years ago

@arthur-shaw, just checked: Works perfectly now! Thanks a lot!