Skip to content

Commit

Permalink
Use new quiet idea
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz committed Oct 22, 2024
1 parent 7e55d50 commit 51ba606
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 49 deletions.
34 changes: 9 additions & 25 deletions R/location.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,14 @@
##' @param verify Logical, indicating if we should verify that the
##' location can be used before adding.
##'
##' @param quiet Logical, indicating if we should print information
##' while configuring and creating locations. If not given, we use
##' the option of `orderly.quiet`, defaulting to `TRUE`.
##'
##' @inheritParams orderly_metadata
##'
##' @return Nothing
##' @export
orderly_location_add <- function(name, type, args, verify = TRUE, quiet = NULL,
root = NULL) {
orderly_location_add <- function(name, type, args, verify = TRUE, root = NULL) {
root <- root_open(root, require_orderly = FALSE)
assert_scalar_character(name)
assert_scalar_logical(verify)
quiet <- orderly_quiet(quiet)

if (name %in% location_reserved_name) {
cli::cli_abort("Cannot add a location with reserved name '{name}'")
Expand Down Expand Up @@ -119,16 +113,12 @@ orderly_location_add <- function(name, type, args, verify = TRUE, quiet = NULL,
loc <- new_location_entry(name, type, args, call = environment())

if (verify) {
if (!quiet) {
cli::cli_alert_info("Testing location")
}
cli_alert_info("Testing location")
driver <- location_driver_create(type, args)
if (!is.null(driver$authorise)) {
driver$authorise()
}
if (!quiet) {
cli::cli_alert_success("Location configured successfully")
}
cli_alert_success("Location configured successfully")
}

config <- root$config
Expand All @@ -146,11 +136,9 @@ orderly_location_add <- function(name, type, args, verify = TRUE, quiet = NULL,
##' be unreliable.
##'
##' @export
orderly_location_add_path <- function(name, path, verify = TRUE, quiet = NULL,
root = NULL) {
orderly_location_add_path <- function(name, path, verify = TRUE, root = NULL) {
args <- list(path = path)
orderly_location_add(name, "path", args, verify = verify, quiet = quiet,
root = root)
orderly_location_add(name, "path", args, verify = verify, root = root)
}


Expand All @@ -160,11 +148,9 @@ orderly_location_add_path <- function(name, path, verify = TRUE, quiet = NULL,
##' example `http://example.com:8080`
##'
##' @export
orderly_location_add_http <- function(name, url, verify = TRUE, quiet = NULL,
root = NULL) {
orderly_location_add_http <- function(name, url, verify = TRUE, root = NULL) {
args <- list(url = url)
orderly_location_add(name, "http", args, verify = verify, quiet = quiet,
root = root)
orderly_location_add(name, "http", args, verify = verify, root = root)
}


Expand All @@ -182,11 +168,9 @@ orderly_location_add_http <- function(name, url, verify = TRUE, quiet = NULL,
##' @export
orderly_location_add_packit <- function(name, url, token = NULL,
save_token = NULL,
verify = TRUE, quiet = NULL,
root = NULL) {
verify = TRUE, root = NULL) {
args <- list(url = url, token = token, save_token = save_token)
orderly_location_add(name, "packit", args, verify = verify, quiet = quiet,
root = root)
orderly_location_add(name, "packit", args, verify = verify, root = root)
}


Expand Down
13 changes: 4 additions & 9 deletions R/location_packit.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,17 @@ do_oauth_device_flow <- function(base_url, cache_disk) {
# It also means the user cannot easily use two different identities on the same
# server from within the same session.
auth_cache <- new.env(parent = emptyenv())
packit_authorisation <- function(base_url, token, save_token, quiet = NULL) {
packit_authorisation <- function(base_url, token, save_token) {
# If a non-Github token is provided, we assume it is a native Packit token
# and use that directly.
if (!is.null(token) && !grepl("^gh._", token)) {
return(list("Authorization" = paste("Bearer", token)))
}

quiet <- orderly_quiet(quiet)
key <- rlang::hash(list(base_url = base_url, token = token))

if (is.null(auth_cache[[key]])) {
if (!quiet) {
cli::cli_alert_info("Logging in to {base_url}")
}
cli_alert_info("Logging in to {base_url}")
if (is.null(token)) {
token <- do_oauth_device_flow(base_url, cache_disk = save_token)
}
Expand All @@ -65,9 +62,7 @@ packit_authorisation <- function(base_url, token, save_token, quiet = NULL) {
login_url,
function(r) http_body_json(r, list(token = scalar(token))))

if (!quiet) {
cli::cli_alert_success("Logged in successfully")
}
cli_alert_success("Logged in successfully")

auth_cache[[key]] <- list("Authorization" = paste("Bearer", res$token))
}
Expand All @@ -94,5 +89,5 @@ orderly_location_packit <- function(url, token = NULL, save_token = TRUE) {

orderly_location_http$new(
paste0(url, "packit/api/outpack"),
function(quiet = NULL) packit_authorisation(url, token, save_token, quiet))
function() packit_authorisation(url, token, save_token))
}
18 changes: 3 additions & 15 deletions man/orderly_location_add.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 51ba606

Please sign in to comment.