|
1 | | -check_extension_approval <- function( |
| 1 | +check_approval <- function( |
2 | 2 | no_prompt = FALSE, |
3 | 3 | what = "Something", |
4 | | - see_more_at = NULL |
| 4 | + not_action = "approved", |
| 5 | + see_more_at = NULL, |
| 6 | + prompt_message = NULL, |
| 7 | + interactive_info = NULL, # could use `{ what }` as used in `cli_inform()` |
| 8 | + .call = rlang::caller_env() |
5 | 9 | ) { |
6 | 10 | if (no_prompt) return(TRUE) |
7 | 11 |
|
8 | | - if (!rlang::is_interactive()) { |
9 | | - cli::cli_abort(c( |
10 | | - "{ what } requires explicit approval.", |
11 | | - ">" = "Set {.arg no_prompt = TRUE} if you agree.", |
12 | | - if (!is.null(see_more_at)) { |
13 | | - c(i = "See more at {.url {see_more_at}}") |
14 | | - } |
15 | | - )) |
| 12 | + if (!is_interactive()) { |
| 13 | + cli::cli_abort( |
| 14 | + c( |
| 15 | + "{ what } requires explicit approval.", |
| 16 | + ">" = "Set {.arg no_prompt = TRUE} if you agree.", |
| 17 | + if (!is.null(see_more_at)) { |
| 18 | + c(i = "See more at {.url {see_more_at}}") |
| 19 | + } |
| 20 | + ), |
| 21 | + call = .call |
| 22 | + ) |
16 | 23 | } else { |
17 | | - cli::cli_inform(c( |
18 | | - "{what} may execute code when documents are rendered. ", |
19 | | - "*" = "If you do not trust the author(s) of this {what}, we recommend that you do not install or use this {what}." |
20 | | - )) |
21 | | - prompt_value <- tolower(readline(sprintf( |
22 | | - "Do you trust the authors of this %s (Y/n)? ", |
23 | | - what |
24 | | - ))) |
25 | | - if (!prompt_value %in% "y") { |
26 | | - cli::cli_inform("{what} not installed.") |
| 24 | + if (!is.null(interactive_info)) { |
| 25 | + cli::cli_inform(interactive_info) |
| 26 | + } |
| 27 | + prompt_value <- tolower(readline(prompt_message)) |
| 28 | + if (!prompt_value %in% c("", "y")) { |
| 29 | + cli::cli_alert_info(paste0(what, " not {not_action}")) |
27 | 30 | return(invisible(FALSE)) |
28 | | - } else { |
29 | | - return(invisible(TRUE)) |
30 | 31 | } |
31 | 32 | } |
| 33 | + return(invisible(TRUE)) |
| 34 | +} |
| 35 | + |
| 36 | +check_extension_approval <- function( |
| 37 | + no_prompt = FALSE, |
| 38 | + what = "Something", |
| 39 | + see_more_at = NULL |
| 40 | +) { |
| 41 | + interactive_info <- c( |
| 42 | + "{what} may execute code when documents are rendered. ", |
| 43 | + "*" = "If you do not trust the author(s) of this {what}, we recommend that you do not install or use this {what}." |
| 44 | + ) |
| 45 | + |
| 46 | + prompt_message <- sprintf( |
| 47 | + "Do you trust the authors of this %s (Y/n)? ", |
| 48 | + what |
| 49 | + ) |
| 50 | + |
| 51 | + check_approval( |
| 52 | + no_prompt = no_prompt, |
| 53 | + what = what, |
| 54 | + not_action = "installed", |
| 55 | + see_more_at = see_more_at, |
| 56 | + prompt_message = prompt_message, |
| 57 | + interactive_info = interactive_info |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +check_removal_approval <- function( |
| 62 | + no_prompt = FALSE, |
| 63 | + what = "Something", |
| 64 | + see_more_at = NULL |
| 65 | +) { |
| 66 | + prompt_message <- sprintf( |
| 67 | + "Are you sure you'd like to remove %s (Y/n)? ", |
| 68 | + what |
| 69 | + ) |
| 70 | + |
| 71 | + check_approval( |
| 72 | + no_prompt = no_prompt, |
| 73 | + what = what, |
| 74 | + not_action = "removed", |
| 75 | + see_more_at = see_more_at, |
| 76 | + prompt_message = prompt_message, |
| 77 | + interactive_info = NULL |
| 78 | + ) |
32 | 79 | } |
| 80 | + |
| 81 | +# Needed for testthat to mock base function |
| 82 | +readline <- NULL |
0 commit comments