2020-07-02 20:31:26 +02:00
|
|
|
const fs = require("fs")
|
|
|
|
|
|
|
|
const ENV_FILE_PATH = ".budibase/.env"
|
2020-07-02 17:53:09 +02:00
|
|
|
|
|
|
|
exports.fetch = async function (ctx) {
|
2020-07-02 20:31:26 +02:00
|
|
|
// Check if structure of call makes sense, if not, return error
|
|
|
|
|
|
|
|
|
|
|
|
// Read File
|
|
|
|
const fileContent = await getEnvironmentVariables()
|
|
|
|
const keys = await extractKeys(fileContent)
|
|
|
|
|
2020-07-02 17:53:09 +02:00
|
|
|
// Temporary while "real" infrastructure to store keys is created
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.message = "API Keys"
|
|
|
|
ctx.body = {
|
|
|
|
budibase: 'testFromBackEnd',
|
|
|
|
sendgrid: 'testFromBackEnd'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.update = async function (ctx) {
|
2020-07-02 21:01:34 +02:00
|
|
|
// Set process.env
|
|
|
|
const envKeyName = `${ctx.params.key.toUpperCase()}_API_KEY`
|
|
|
|
process.env[envKeyName] = ctx.request.body.value
|
|
|
|
|
|
|
|
// Write to file
|
|
|
|
// TODO
|
|
|
|
|
2020-07-02 17:53:09 +02:00
|
|
|
ctx.status = 200
|
2020-07-02 18:38:00 +02:00
|
|
|
ctx.message = `Updated ${ctx.params.key} API key succesfully.`
|
|
|
|
ctx.body = { [ctx.params.key]: ctx.request.body.value }
|
2020-07-02 17:53:09 +02:00
|
|
|
}
|
|
|
|
|
2020-07-02 20:31:26 +02:00
|
|
|
async function getEnvironmentVariables() {
|
|
|
|
const home = require('os').homedir();
|
|
|
|
const filePath = `${home}/${ENV_FILE_PATH}`
|
|
|
|
|
|
|
|
return data = fs.readFileSync(filePath, 'utf8');
|
2020-07-02 17:53:09 +02:00
|
|
|
}
|
2020-07-02 20:31:26 +02:00
|
|
|
|
2020-07-02 21:01:34 +02:00
|
|
|
async function extractKeys(content) {
|
|
|
|
const lines = content.split(/\r?\n/)
|
2020-07-02 20:31:26 +02:00
|
|
|
// Extract keys here
|
|
|
|
return []
|
|
|
|
}
|