|
2398 | 2398 | .check_set_caller(".check_linkage_method")
|
2399 | 2399 | .check_that(linkage %in% .conf("dendro_linkage"))
|
2400 | 2400 | }
|
| 2401 | +#' @title Check netrc file |
| 2402 | +#' @description |
| 2403 | +#' Check if netrc file exists and if its content is correct |
| 2404 | +#' @param attributes Attributes required from the netrc file |
| 2405 | +#' @return Called for side effects |
| 2406 | +#' @keywords internal |
| 2407 | +#' @noRd |
| 2408 | +.check_netrc_gdal <- function(attributes) { |
| 2409 | + .check_set_caller(".check_netrc_gdal") |
| 2410 | + # define if the current GDAL version is reading netrc from env variable |
| 2411 | + is_gdal_reading_netrc <- .gdal_version() >= "3.7.0" |
| 2412 | + # define from where `netrc` file must be loaded |
| 2413 | + # case 1 - gdal environment variable (requires GDAL >= 3.7.0) |
| 2414 | + netrc_from_var <- ifelse( |
| 2415 | + is_gdal_reading_netrc, |
| 2416 | + Sys.getenv("GDAL_HTTP_NETRC_FILE", unset = NA), |
| 2417 | + NA |
| 2418 | + ) |
| 2419 | + # case 2 - netrc file stored in user home directory |
| 2420 | + netrc_from_home <- ifelse( |
| 2421 | + .Platform[["OS.type"]] == "windows", |
| 2422 | + .conf("gdal_netrc_file_path_win"), |
| 2423 | + .conf("gdal_netrc_file_path") |
| 2424 | + ) |
| 2425 | + # define which netrc file will be used |
| 2426 | + netrc_file <- ifelse( |
| 2427 | + is.na(netrc_from_var), |
| 2428 | + netrc_from_home, |
| 2429 | + netrc_from_var |
| 2430 | + ) |
| 2431 | + # if the env variable is used, then, warning users not to set the GDAL |
| 2432 | + # variable using `Sys.setenv` |
| 2433 | + if (!is.na(netrc_from_var)) { |
| 2434 | + warning(.conf("messages", ".check_netrc_gdal_var")) |
| 2435 | + } |
| 2436 | + # check if file exist |
| 2437 | + .check_that(file.exists(netrc_file)) |
| 2438 | + # load netrc content |
| 2439 | + netrc_content <- readLines(netrc_file) |
| 2440 | + # check netrc file content |
| 2441 | + .check_that( |
| 2442 | + any( |
| 2443 | + purrr::map_lgl(netrc_content, function(x) { |
| 2444 | + stringr::str_detect(x, attributes) |
| 2445 | + }) |
| 2446 | + ) |
| 2447 | + ) |
| 2448 | +} |
0 commit comments