-
Notifications
You must be signed in to change notification settings - Fork 19
/
environment.R
76 lines (64 loc) · 1.89 KB
/
environment.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Functions related to loading configuriation based upon the environment
# the code is being run under.
#' Get the current environment name
#'
#' Get the current environment name.
#'
#' @return (character) The environment name.
#'
env_get <- function() {
env <- Sys.getenv("ARCTICDATA_ENV")
if (env == "") {
env <- "development"
}
env
}
#' Load environmental variables from a YAML-formatted environment file
#'
#' Load environmental variables from a YAML-formatted environment file.
#'
#' This file should be formatted in the following way:
#'
#' some_environment:
#' var_one: some value
#' var_two: some value
#' var_three: some value
#'
#' @param name (character) Optional. The environment name.
#' @param path (character) Optional. Path to an environment file.
#' @param skip_mn (logical) Optional. Skip contacting the MNode and filling in the $mn element of the environment.
#'
#' @return (list) A list of name-value pairs.
#'
#' @noRd
env_load <- function(name=NULL, path=NULL, skip_mn=FALSE) {
if (!requireNamespace("yaml")) {
stop(call. = FALSE,
"The package 'yaml' must be installed to run this function. ",
"Please install it and try again.")
}
# Determine the environment to load
if (is.null(name)) {
name <- env_get()
}
# If ARCTICDATA_ENV is set but a
# Determine the file to load the environment from
if (!is.null(path)) {
stopifnot(file.exists(path))
file <- path
} else {
file <- file.path(system.file(package = "arcticdatautils"), "environment.yml")
}
stopifnot(file.exists(file))
# Pull out the content from the YAML file
yaml_content <- yaml::yaml.load_file(file)
stopifnot(length(yaml_content) == 3)
stopifnot(name %in% names(yaml_content))
env <- yaml_content[name][[1]]
# Load up the MN
if (!skip_mn) {
env$mn <- dataone::MNode(env$mn_base_url)
}
# Return the list of environmental variables
env
}