Given vectors of directories, basenames, and suffices, combine them to find existing files.
find_config_files(
basenames = .basename.defaults(),
dirs = .dir.defaults(),
suffices = .suffix.defaults()
)
A vector of path specifications, or an empty vector if none are found.
A vector of file names (without directory or file type) to use in searching for configuration files.
A vector of directory names to use in searching for configuration files.
A vector of suffices (file "type"s) to use in searching for the configuration file.
This function is intended to support a variety of installation patterns, so
it attempts to be flexible in looking for configuration files. First,
environment variables of the form basename_CONFIG
, where
basename is the uppercase form of each candidate basename, are
examined to see whether any translate to a file path.
Following this, the path name parts supplied as arguments are used to
build potential file names. If dirs
is not specified, the
following directories are checked by default:
the user's $HOME
directory
the directory named .srcr
(no leading .
on Windows) under $HOME
the directory in which the executing script is located
the directory in which the calling function's calling function's
source file is located (typically an application-level library). For
example, if the function my_setup()
calls srcr()
, which in turn calls
find_config_files()
, then the directory of the file containing
my_setup()
will be tried.
the directory in which the calling function's source file is located
(typically a utility function, such as srcr()
)
Note that the current working directory is not part of the search by default. This is done to limit the potential for accidentally introducing (potentially harmful) configuration files by setting the working directory.
In each location, the file names given in basenames
are checked; if
none are specified, several default file names are tried:
the name of the calling function's source file
the name of the executing script
the directory in which the calling function's calling function's
source file is located (typically an application-level library). For
example, if the function my_setup()
calls srcr()
, which in turn calls
find_config_files()
, then the name of the file containing
my_setup()
will be tried.
The suffices (file "type"s) of .json
, .conf
, and nothing,
are tried with each candidate path; you may override this default by
using the suffices
parameter. Finally, in order to accommodate the Unix
tradition of "hidden" configuration files, each basename is prefixed with
a period before trying the basename alone.
if (FALSE) {
find_config_files() # All defaults
find_config_files(dirs = c(file.path(Sys.getenv('HOME'),'etc'),
'/usr/local/etc', '/etc'),
basenames = c('my_app'),
suffices = c('.conf', '.rc'))
}
Run the code above in your browser using DataLab