Skip to content

Commit

Permalink
refactor: encapsulate yesno() for clarity and easier testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyaZar committed Aug 3, 2023
1 parent 3917328 commit f3e6cb2
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ guess_where_config <- function(
CONFIG_DFLT_MISSNG <- !CONFIG_DFLT_EXISTS
CONFIG_USER_EXISTS <- !is.null(ret_pth_usr) && (!identical(ret_pth_usr, ret_pth_def))
CONFIG_USER_MISSNG <- !CONFIG_USER_EXISTS

# Case I.
# The default config exists AND "R/app_config.R" does not provide any
# information about a possible user config (this one should be correct in 99%
Expand All @@ -33,6 +34,7 @@ guess_where_config <- function(
if (CONFIG_DFLT_EXISTS && CONFIG_USER_MISSNG) {
return(fs_path_abs(ret_pth_def))
}

# Case II.
# The default config does not exists AND "R/app_config.R" provides information
# about a possible user config (this one should be correct if the user changed
Expand All @@ -43,6 +45,7 @@ guess_where_config <- function(
if (CONFIG_DFLT_MISSNG && CONFIG_USER_EXISTS) {
return(fs_path_abs(ret_pth_usr))
}

# Case III.
# The default config does exists AND "R/app_config.R" provides information
# about a possible user config which is found! (this occurs if the user
Expand Down Expand Up @@ -171,17 +174,10 @@ get_current_config <- function(path = getwd()) {
}

if (!fs_file_exists(path_conf)) {
if (interactive()) {
ask <- yesno(
sprintf(
"The %s file doesn't exist.\nIt's possible that you might not be in a {golem} based project.\n Do you want to create the {golem} files?",
basename(path_conf)
)
)
if (rlang::is_interactive()) {
ask <- ask_golem_creation_upon_config(path_conf)
# Return early if the user doesn't allow
if (!ask) {
return(NULL)
}
if (!ask) return(NULL)

fs_file_copy(
path = golem_sys("shinyexample/inst/golem-config.yml"),
Expand Down Expand Up @@ -221,6 +217,13 @@ get_current_config <- function(path = getwd()) {
)
}

ask_golem_creation_upon_config <- function(pth) {
msg <- paste0(
"The %s file doesn't exist.",
"\nIt's possible that you might not be in a {golem} based project.\n",
"Do you want to create the {golem} files?")
yesno(sprintf(msg, basename(pth)))
}
# This function changes the name of the
# package in app_config when you need to
# set the {golem} name
Expand Down

0 comments on commit f3e6cb2

Please sign in to comment.