R/parseCredentialFile.R

#' This isn't a super robust file parser, but will work well if AWS credential|config file guidelines are followed.
#' read lines of the credential or config file then iterate over forming a list of profiles.
#'
#' @family credential management functions
#'
parseCredentialFile <- function(filePath = NULL) {
    tmpFile <- readLines(filePath, warn = F)
    profiles <- list()
    
    for (currentLine in tmpFile){
        if (!grepl('[[:alnum:]]', currentLine)){
            next
        }
        
        if (grepl("^\\[", currentLine)) {
            currentProfile <- trimws(gsub("\\[|\\]|profile", "", currentLine, ignore.case = TRUE))
            profiles[[currentProfile]] <- list()
            next
        } else {
            tmpPair <- sapply(strsplit(currentLine, '='), trimws)
            profiles[[currentProfile]][[toupper(tmpPair[1])]] = tmpPair[2]
        }
    }
    
    return(profiles)
}
AlonzoBarnett/RcurlAWS documentation built on May 15, 2019, 10:47 a.m.