-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration-manager.mjs
102 lines (85 loc) · 3.21 KB
/
configuration-manager.mjs
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
import ConfigIniParser from 'config-ini-parser'
import fs from 'fs-extra'
import _ from 'lodash'
import __ from './attempt.mjs'
export default class ConfigurationManager {
constructor (mainConfigFile, localConfigFile) {
this.mainConfigFile = mainConfigFile
this.localConfigFile = localConfigFile
this.delimiter = '\n'
}
async returnAvailableConfigFilePath () {
const [localConfigError, localConfigResult] = await __(fs.pathExists(this.localConfigFile))
if (localConfigError || !localConfigResult) {
console.log('Info: Config file not found in local folder. Trying /etc/s3-folder-sync/ ...')
const [mainConfigError, mainConfigResult] = await __(fs.pathExists(this.mainConfigFile))
if (mainConfigError || !mainConfigResult) {
throw new Error('Error: No config file found.')
}
return this.mainConfigFile
}
return this.localConfigFile
}
openFile (filePath) {
return new Promise(async (resolve, reject) => {
let configFileContents = ''
const file = fs.createReadStream(filePath, { encoding: 'utf8' })
file.on('error', function (error) {
throw new Error('Error: Problem reading config file: ' + filePath + ', error was: ' + error)
})
file.on('data', function (data) {
configFileContents = data.toString()
})
file.on('close', function () {
return resolve(configFileContents)
})
file.on('end', () => {
file.destroy()
})
})
}
async getConfig () {
const parser = new ConfigIniParser.ConfigIniParser(this.delimiter)
const [configFilePathError, configFilePath] = await __(this.returnAvailableConfigFilePath())
if (configFilePathError) {
throw new Error(configFilePathError)
}
console.log('Info: Using config file: ' + configFilePath)
const [error, result] = await __(this.openFile(configFilePath))
if (error) {
throw new Error(error)
}
try{
parser.parse(result)
} catch (error) {
throw new Error(error.message)
}
const pgpPassphrase = parser.getOptionFromDefaultSection('pgpPassphrase', 'null')
const bucketSecretKey = parser.getOptionFromDefaultSection('bucketSecretKey', 'null')
const bucketAccessKey = parser.getOptionFromDefaultSection('bucketAccessKey', 'null')
const bucketEndpoint = parser.getOptionFromDefaultSection('bucketEndpoint', 'null')
const bucketRegion = parser.getOptionFromDefaultSection('bucketRegion', 'null')
const pgpPrivateKeyArmored = parser.getOptionFromDefaultSection('pgpPrivateKeyArmored', 'null')
const pgpPublicKeyArmored = parser.getOptionFromDefaultSection('pgpPublicKeyArmored', 'null')
const finalConfig = {
pgpPassphrase,
bucketSecretKey,
bucketAccessKey,
bucketEndpoint,
bucketRegion,
pgpPrivateKeyArmored,
pgpPublicKeyArmored
}
return finalConfig
}
setConfigValue (parser, keyValues) {
_.forIn(keyValues, (value, key) => {
parser.setOptionInDefaultSection(key, value)
})
return parser.stringify(this.delimiter)
}
writeConfigFile (configFile, configData, callback) {
const options = { flags: 'wx+', encoding: 'utf8' }
fs.writeFile(configFile, configData, options, callback)
}
}