File: envvars.R

package info (click to toggle)
r-cran-rsconnect 1.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,044 kB
  • sloc: python: 185; sh: 13; makefile: 5
file content (75 lines) | stat: -rw-r--r-- 2,486 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#' Maintain environment variables across multiple applications
#'
#' @description
#' * `listAccountEnvVars()` lists the environment variables used by
#'   every application published to the specified account.
#' * `updateAccountEnvVars()` updates the specified environment variables with
#'   their current values for every app that uses them.
#'
#' Secure environment variable are currently only supported by Posit Connect
#' so other server types will generate an error.
#'
#' @inheritParams deployApp
#' @export
#' @return `listAccountEnvVars()` returns a data frame with one row
#'   for each data frame. It has variables `id`, `guid`, `name`, and
#'   `envVars`. `envVars` is a list-column.
listAccountEnvVars <- function(server = NULL, account = NULL) {
  accountDetails <- accountInfo(account, server)
  checkServerHasEnvVars(accountDetails$server)

  apps <- applications(
    account = accountDetails$name,
    server = accountDetails$server
  )
  apps <- apps[c("id", "guid", "name")]

  client <- clientForAccount(accountDetails)
  envVars <- lapply(apps$guid, client$getEnvVars)
  apps$envVars <- envVars
  apps
}

#' @export
#' @rdname listAccountEnvVars
#' @param envVars Names of environment variables to update. Their
#'   values will be automatically retrieved from the current process.
#'
#'   If you specify multiple environment variables, any application that
#'   uses any of them will be updated with all of them.
updateAccountEnvVars <- function(envVars, server = NULL, account = NULL) {
  check_character(envVars)

  accountDetails <- accountInfo(account, server)
  checkServerHasEnvVars(accountDetails$server)

  apps <- listAccountEnvVars(
    account = accountDetails$name,
    server = accountDetails$server
  )
  uses_vars <- vapply(apps$envVars, function(x) any(envVars %in% x), logical(1))
  if (!any(uses_vars)) {
    cli::cli_abort("No applications use environment variable{?s} {.arg {envVars}}")
  }

  guids <- apps$guid[uses_vars]
  cli::cli_progress_bar("Updating application...", total = length(guids))

  client <- clientForAccount(accountDetails)
  for (guid in guids) {
    client$setEnvVars(guid, envVars)
    cli::cli_progress_update()
  }
}

# Helpers -----------------------------------------------------------------

checkServerHasEnvVars <- function(server, error_call = caller_env()) {
  if (isConnectServer(server)) {
    return()
  }

  cli::cli_abort(
    "The {.arg server} {.str server} does not support environment variables"
  )
}