Skip to content

Commit

Permalink
Fix config
Browse files Browse the repository at this point in the history
  • Loading branch information
ShixiangWang committed Sep 2, 2019
1 parent 04e1bf8 commit a9f0718
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 9 deletions.
33 changes: 32 additions & 1 deletion R/config.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
# config
.yuque_config_path = "~/.yuque.json"

#' Configure YuQue
#'
#' Ask some questions to user and save configuration to local file.
#'
#' More details see [yq_whoami], [yq_token].
#'
#' @return Nothing.
#' @export
#'
#' @examples
#' \dontrun{
#' if (interactive())
#' yq_config()
#' }
yq_config = function() {
config = list()
config$login = ""
Expand Down Expand Up @@ -44,8 +57,26 @@ yq_config = function() {
}

message("Saving info to ", .yuque_config_path)
jsonlite::write_json(config, path = .yuque_config_path)
update_config(config, path = .yuque_config_path)

message("Done. Enjoy this tool.\n- Shixiang")
}

update_config = function(x, path) {
if (!file.exists(path)) {
jsonlite::write_json(x, path = path)
} else {
up_config = jsonlite::read_json(path, simplifyVector = TRUE)
up_names = names(up_config)
for (i in names(x)) {
ifelse(i %in% up_names,
message("Updating ", i),
message("Adding ", i))
up_config[[i]] = x[[i]]
}
jsonlite::write_json(up_config, path = path)
}

}

#jsonlite::read_json("test.json", simplifyVector = TRUE)
3 changes: 3 additions & 0 deletions R/token.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
yq_token <- function() {
if (file.exists(.yuque_config_path)) {
token = jsonlite::read_json(.yuque_config_path, simplifyVector = TRUE)$token
} else {
token = ""
}

if (token != "") {
return(token)
} else {
Expand Down
19 changes: 14 additions & 5 deletions R/whoami.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,20 @@ yq_whoami <- function(.token = NULL, .api_url = NULL, .send_headers = NULL) {
"For more on what to do with the PAT, see ?yq_whoami.",
"\n==========================================="
)
login = jsonlite::read_json(.yuque_config_path, simplifyVector = TRUE)$login
res <- yq(
endpoint = "/users/:login", login = login, .token = .token,
.api_url = .api_url, .send_headers = .send_headers
)
if (file.exists(.yuque_config_path)) {
login = jsonlite::read_json(.yuque_config_path, simplifyVector = TRUE)$login
res <- yq(
endpoint = "/users/:login", login = login, .token = .token,
.api_url = .api_url, .send_headers = .send_headers
)
} else {
message(
"Also no configure file for YuQue available.\n",
"Please either specify .token argument or \n",
"set configuration via yq_config()"
)
return(invisible(NULL))
}
} else {
res <- yq(
endpoint = "/user", .token = .token,
Expand Down
2 changes: 1 addition & 1 deletion R/yq.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ NULL
#' existing file. Defaults to FALSE.
#' @param .token Authentication token. Default to YUQUE_TOKEN
#' environment variables, in this order if any is set.
#' @param .api_url Github API url (default: \url{https://www.yuque.com/api/v2}). Used
#' @param .api_url YuQue API url (default: \url{https://www.yuque.com/api/v2}). Used
#' if \code{endpoint} just contains a path. Default to YuQue_API_URL
#' environment variable if set.
#' @param .method HTTP method to use if not explicitly supplied in the
Expand Down
2 changes: 1 addition & 1 deletion man/yq.Rd

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

23 changes: 23 additions & 0 deletions man/yq_config.Rd

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

2 changes: 1 addition & 1 deletion man/yq_whoami.Rd

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

0 comments on commit a9f0718

Please sign in to comment.