R/token.R

Defines functions read_token_from_json read_token_from_env read_token

#'@importFrom jsonlite fromJSON
read_token_from_json <- function() {
  configPath <- file.path(path.expand('~'), '.thank-you-stars.json')
  if (!file.exists(configPath)) {
    return(NULL)
  }

  token <- jsonlite::fromJSON(configPath)[['token']]
  if (is.null(token)) {
    return(NULL)
  }
  return(token)
}

read_token_from_env <- function() {
  token <- Sys.getenv('GITHUB_PAT', '')
  if (token == '')
    token <- Sys.getenv('GITHUB_TOKEN', '')
  if (token == '')
    NULL
  else
    token
}

read_token <- function(token=NULL) {
  if (is.null(token)) {
    token <- read_token_from_json()
  }
  if (is.null(token)) {
    token <- read_token_from_env()
  }
  if (is.null(token)) stop("Required GitHub token.")
  return(token)
}

Try the ThankYouStars package in your browser

Any scripts or data that you put into this service are public.

ThankYouStars documentation built on May 2, 2019, 2:56 p.m.