ini-loader.js
2.98 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var AWS = require('../core');
var os = require('os');
var path = require('path');
function parseFile(filename, isConfig) {
var content = AWS.util.ini.parse(AWS.util.readFileSync(filename));
var tmpContent = {};
Object.keys(content).forEach(function(profileName) {
var profileContent = content[profileName];
profileName = isConfig ? profileName.replace(/^profile\s/, '') : profileName;
Object.defineProperty(tmpContent, profileName, {
value: profileContent,
enumerable: true
});
});
return tmpContent;
}
/**
* Ini file loader class the same as that used in the SDK. It loads and
* parses config and credentials files in .ini format and cache the content
* to assure files are only read once.
* Note that calling operations on the instance instantiated from this class
* won't affect the behavior of SDK since SDK uses an internal singleton of
* this class.
* @!macro nobrowser
*/
AWS.IniLoader = AWS.util.inherit({
constructor: function IniLoader() {
this.resolvedProfiles = {};
},
/** Remove all cached files. Used after config files are updated. */
clearCachedFiles: function clearCachedFiles() {
this.resolvedProfiles = {};
},
/**
* Load configurations from config/credentials files and cache them
* for later use. If no file is specified it will try to load default
* files.
* @param options [map] information describing the file
* @option options filename [String] ('~/.aws/credentials' or defined by
* AWS_SHARED_CREDENTIALS_FILE process env var or '~/.aws/config' if
* isConfig is set to true)
* path to the file to be read.
* @option options isConfig [Boolean] (false) True to read config file.
* @return [map<String,String>] object containing contents from file in key-value
* pairs.
*/
loadFrom: function loadFrom(options) {
options = options || {};
var isConfig = options.isConfig === true;
var filename = options.filename || this.getDefaultFilePath(isConfig);
if (!this.resolvedProfiles[filename]) {
var fileContent = this.parseFile(filename, isConfig);
Object.defineProperty(this.resolvedProfiles, filename, { value: fileContent });
}
return this.resolvedProfiles[filename];
},
/**
* @api private
*/
parseFile: parseFile,
/**
* @api private
*/
getDefaultFilePath: function getDefaultFilePath(isConfig) {
return path.join(
this.getHomeDir(),
'.aws',
isConfig ? 'config' : 'credentials'
);
},
/**
* @api private
*/
getHomeDir: function getHomeDir() {
var env = process.env;
var home = env.HOME ||
env.USERPROFILE ||
(env.HOMEPATH ? ((env.HOMEDRIVE || 'C:/') + env.HOMEPATH) : null);
if (home) {
return home;
}
if (typeof os.homedir === 'function') {
return os.homedir();
}
throw AWS.util.error(
new Error('Cannot load credentials, HOME path not set')
);
}
});
var IniLoader = AWS.IniLoader;
module.exports = {
IniLoader: IniLoader,
parseFile: parseFile,
};