forked from watson-developer-cloud/node-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable reading credentials from ibm-credentials.env file
- Loading branch information
Showing
7 changed files
with
2,379 additions
and
2,224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import dotenv = require('dotenv'); | ||
import fs = require('fs'); | ||
import os = require('os'); | ||
import path = require('path'); | ||
|
||
const filename: string = 'ibm-credentials.env'; | ||
|
||
export function readCredentialsFile() { | ||
// first look for an env variable called IBM_CREDENTIALS_FILE | ||
// it should be the path to the file | ||
|
||
const givenFilepath: string = process.env['IBM_CREDENTIALS_FILE'] || ''; | ||
const homeDir: string = os.homedir(); | ||
const workingDir: string = process.cwd(); | ||
|
||
let filepathToUse: string; | ||
|
||
if (givenFilepath && fileExistsAtPath(givenFilepath)) { | ||
filepathToUse = givenFilepath; | ||
} else if (fileExistsAtPath(homeDir)) { | ||
filepathToUse = homeDir; | ||
} else if (fileExistsAtPath(workingDir)) { | ||
filepathToUse = workingDir; | ||
} else { | ||
// file does not exist anywhere, will not be used | ||
return {}; | ||
} | ||
|
||
const credsFile = fs.readFileSync(constructFilepath(filepathToUse)); | ||
|
||
return dotenv.parse(credsFile); | ||
} | ||
|
||
export function fileExistsAtPath(filepath): boolean { | ||
filepath = constructFilepath(filepath); | ||
return fs.existsSync(filepath); | ||
} | ||
|
||
export function constructFilepath(filepath): string { | ||
// ensure filepath includes the filename | ||
if (!filepath.endsWith(filename)) { | ||
filepath = path.join(filepath, filename); | ||
} | ||
|
||
return filepath; | ||
} |
Oops, something went wrong.