rstudio / rstudioapi

Safely access RStudio's API (when available)
http://rstudio.github.io/rstudioapi
Other
165 stars 35 forks source link

Environment detection helpers for Workbench and Connect #290

Open fh-mthomson opened 6 months ago

fh-mthomson commented 6 months ago

I'd love generalizable versions of rstudioapi::isAvailable() that can detect in which environment code is running to then gate logic (e.g., authentication patterns that differ b/w Workbench and local), to prevent ad hoc picking of env vars (that may drift over product versions).

A starting point translated from some wrappers I've used:

# duplicative of `rstudioapi::IsAvailable()` but avoids circular logic if `rstudioapi` is not installed
get_env_rstudio_ide <- function() {
  Sys.getenv("RSTUDIO_PROGRAM_MODE")
}

is_env_rstudio_ide_desktop <- function() {
  get_env_rstudio_ide() == "desktop"
}

is_env_rstudio_ide_workbench <- function() {
  get_env_rstudio_ide() == "server"
}

is_env_rstudio_ide <- function() {
  is_env_rstudio_ide_desktop() || is_env_rstudio_ide_workbench()
}

is_env_connect <- function() {
  # have also used `USER` or `USERNAME`
  Sys.getenv("LOGNAME") == "rstudio-connect"
}

# Processes executing on a server (Workbench or Connect) ----
is_env_server <- function() {
  is_env_rstudio_ide_workbench() || is_env_connect()
}

Related to https://github.com/r-lib/httr2/pull/410/files