-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from OHDSI/develop
Update to v0.0.2
- Loading branch information
Showing
28 changed files
with
885 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Package: Ulysses | ||
Title: Automate OHDSI Study Setup | ||
Version: 0.0.1 | ||
Version: 0.0.2 | ||
Authors@R: | ||
person("Martin", "Lavallee", , "[email protected]", role = c("aut", "cre")) | ||
Description: Automates setup of OHDSI study and provides functions to assist on improving organization | ||
|
@@ -13,10 +13,14 @@ Imports: | |
cli, | ||
crayon, | ||
fs, | ||
gert, | ||
gh, | ||
glue, | ||
keyring, | ||
lifecycle, | ||
lubridate, | ||
magrittr, | ||
purrr, | ||
rlang, | ||
rstudioapi, | ||
scales, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Functions for git related tasks ------------ | ||
|
||
has_git <- function(proj) { | ||
repo <- tryCatch(gert::git_find(proj), error = function(e) NULL) | ||
!is.null(repo) | ||
} | ||
|
||
#' Publish Study to Repo | ||
#' @param repoUrl an https url for the repo remote. Example: https://github.com/Org/repo.git | ||
#' @param message the commit message for your OHDSI study | ||
#' @export | ||
publishStudyToRepository <- function(repoUrl, | ||
message = "Initial commit for OHDSI study") { | ||
|
||
proj <- usethis::proj_get() | ||
check <- !has_git(proj) | ||
if (check) { | ||
#Step1: initialize git | ||
gert::git_init(proj) | ||
# Step 2: add all files | ||
stg <- gert::git_add(files = ".") | ||
#step 3: commit all files | ||
sha <- gert::git_commit_all(message = message) | ||
#step 4: setup remote | ||
gert::git_remote_add(url = repoUrl) | ||
# Step 5: push | ||
gert::git_push(remote = "origin") | ||
} else { | ||
remoteLs <- gert::git_remote_list()$url | ||
has_remote <- remoteLs %in% repoUrl | ||
if (!has_remote) { | ||
#step 4: setup remote | ||
gert::git_remote_add(url = repoUrl) | ||
# Step 5: push | ||
gert::git_push(remote = "origin") | ||
} | ||
} | ||
if(check) { | ||
#restart R session | ||
cli::cat_bullet("Restarting RStudio to setup Git Pane in RStudio", bullet = "info", bullet_col = "blue") | ||
rstudioapi::openProject(proj) | ||
} | ||
invisible(TRUE) | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
# Keyring Functions ---------------------- | ||
|
||
## Helpers ------------ | ||
set_cred <- function(cred, db, keyringName) { | ||
|
||
key_name <- paste(db, cred, sep = "_") | ||
prompt_txt <- glue::glue("Set {key_name}: ") | ||
|
||
keyring::key_set( | ||
service = key_name, | ||
keyring = keyringName, | ||
prompt = prompt_txt | ||
) | ||
invisible(key_name) | ||
} | ||
|
||
blurCreds <- function(item, keyringName) { | ||
cred <- keyring::key_get(service = item, keyring = keyringName) | ||
txt <- glue::glue(item, ": ", crayon::blurred(cred)) | ||
cli::cat_bullet(txt, bullet = "info", bullet_col = "blue") | ||
invisible(item) | ||
} | ||
|
||
|
||
checkKeyring <- function(keyringName, keyringPassword) { | ||
allKeyrings <- keyring::keyring_list() | ||
keyringName %in% allKeyrings$keyring | ||
} | ||
|
||
|
||
dropKeyring <- function(keyringName, keyringPassword) { | ||
|
||
if (keyring::keyring_is_locked(keyring = keyringName)) { | ||
keyring::keyring_unlock(keyring = keyringName, password = keyringPassword) | ||
} | ||
# Delete all keys from the keyring so we can delete it | ||
cli::cat_bullet("Delete existing keyring: ", keyringName, | ||
bullet = "warning", bullet_col = "yellow") | ||
keys <- keyring::key_list(keyring = keyringName) | ||
if (nrow(keys) > 0) { | ||
for (i in 1:nrow(keys)) { | ||
# drop keys | ||
keyring::key_delete(keys$service[i], keyring = keyringName) | ||
} | ||
} | ||
# drop keyring | ||
keyring::keyring_delete(keyring = keyringName) | ||
|
||
invisible(keyringName) | ||
|
||
} | ||
|
||
|
||
setKeyring <- function(keyringName, keyringPassword) { | ||
|
||
cli::cat_bullet( | ||
"Creating a study keyring for: ", crayon::cyan(keyringName), | ||
bullet = "info", bullet_col = "blue" | ||
) | ||
# create keyring | ||
keyring::keyring_create(keyring = keyringName, password = keyringPassword) | ||
|
||
invisible(keyringName) | ||
} | ||
|
||
## UI ------------ | ||
|
||
#' Function to list default credentials | ||
#' @description | ||
#' This function builds the standard credential set needed for most connections. If | ||
#' another credential is needed use `c()` to bind the character vector. | ||
#' @export | ||
defaultCredentials <- function() { | ||
creds <- c( | ||
"dbms", # the database dialect | ||
"user", # the user name for the db | ||
"password", # the password for the db | ||
"connectionString", # the connection string to access the db | ||
"cdmDatabaseSchema", # the database + schema (or just schema) hosting the cdm | ||
"vocabDatabaseSchema", # the database + schema (or just schema) hosting the vocabulary, usually same as cdm | ||
"workDatabaseSchema" # the database + schema (or just schema) hosting the work or scratch | ||
) | ||
return(creds) | ||
} | ||
|
||
#' Function to check the database credential | ||
#' @param cred the credential to set (i.e dbms, user, connectionString) | ||
#' @param db the database prefix for the credential | ||
#' @param keyringName the name of the keyringName for the credential check, this will be the keyring namec | ||
#' @param verbose toggle option to print console message | ||
#' @export | ||
checkDatabaseCredential <- function(cred, db, keyringName, verbose = TRUE) { | ||
|
||
#paste name to set full credential | ||
key_name <- paste(db, cred, sep = "_") | ||
|
||
if (verbose) { | ||
cli::cat_bullet("Check that credential ", crayon::green(key_name), " is correct.", | ||
bullet = "warning", bullet_col = "yellow") | ||
} | ||
|
||
#print credential | ||
blurCreds(item = key_name, keyringName = keyringName) | ||
|
||
invisible(key_name) | ||
} | ||
|
||
#' Function to set single credential | ||
#' @param cred the credential to set (i.e dbms, user, connectionString) | ||
#' @param db the database prefix for the credential | ||
#' @param keyringName the name of the keyringName for the credential set, this will be the keyring namec | ||
#' @param keyringPassword the password for the keyring. | ||
#' @param forceCheck a toggle that will print blurred credentials to check credential | ||
#' @export | ||
setCredential <- function(cred, db, keyringName, keyringPassword, forceCheck = TRUE) { | ||
|
||
if (keyring::keyring_is_locked(keyring = keyringName)) { | ||
keyring::keyring_unlock(keyring = keyringName, password = keyringPassword) | ||
} | ||
|
||
cli::cat_bullet("Input your credentials in the dialog box", | ||
bullet = "warning", bullet_col = "yellow") | ||
|
||
set_cred(cred = cred, db = db, keyringName = keyringName) | ||
|
||
if (forceCheck) { | ||
checkDatabaseCredential(cred = cred, db = db, keyringName = keyringName) | ||
} | ||
invisible(cred) | ||
} | ||
|
||
#' Function to set multi credentials | ||
#' @param creds a vector of credentials to set (i.e dbms, user, connectionString). See defaultCredentials on building set | ||
#' @param db the database prefix for the credential | ||
#' @param keyringName the name of the keyringName where the credential will be set | ||
#' @param keyringPassword the password for the keyring. | ||
#' @param forceCheck a toggle that will print blurred credentials to check credential | ||
#' @export | ||
setMultipleCredentials <- function(creds, db, keyringName, keyringPassword, forceCheck = TRUE) { | ||
|
||
if (keyring::keyring_is_locked(keyring = keyringName)) { | ||
keyring::keyring_unlock(keyring = keyringName, password = keyringPassword) | ||
} | ||
|
||
cli::cat_bullet("Input your credentials in the dialog box", | ||
bullet = "warning", bullet_col = "yellow") | ||
purrr::walk( | ||
creds, | ||
~set_cred(cred = .x, db = db, keyringName = keyringName) | ||
) | ||
|
||
if (forceCheck) { | ||
purrr::walk(creds, ~checkDatabaseCredential(cred = .x, db = db, keyringName = keyringName, verbose = FALSE)) | ||
} | ||
invisible(creds) | ||
|
||
} | ||
|
||
|
||
|
||
#' Function to set study keyring | ||
#' @param keyringName the name of the keyring, this should be the reponame for the study | ||
#' @param keyringPassword a password to access the study keyring | ||
#' @export | ||
setStudyKeyring <- function(keyringName, keyringPassword) { | ||
|
||
# check if keyring exists | ||
check <- checkKeyring(keyringName = keyringName, keyringPassword = keyringPassword) | ||
|
||
if (check) { | ||
ask1 <- usethis::ui_yeah( | ||
"This keyring already exists. Do you want to drop the keyring and its contents?" | ||
) | ||
if (ask1) { | ||
dropKeyring(keyringName = keyringName, keyringPassword = keyringPassword) | ||
#Set keyring | ||
setKeyring(keyringName = keyringName, keyringPassword = keyringPassword) | ||
} else{ | ||
cli::cat_bullet("Keeping keyring ", crayon::cyan(keyringName), ". ") | ||
} | ||
} else{ | ||
#Set keyring | ||
setKeyring(keyringName = keyringName, keyringPassword = keyringPassword) | ||
} | ||
invisible(keyringName) | ||
} | ||
|
Oops, something went wrong.