mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
env get,set,unset
This commit is contained in:
parent
9afc24cbb6
commit
7685f95fc8
@ -58,8 +58,7 @@ usage: jaws env get <stage> <region> <key>
|
||||
* @returns {Promise}
|
||||
*/
|
||||
envGet() {
|
||||
let _this = this,
|
||||
stageRegionKey = Array.prototype.slice.call(arguments, 0);
|
||||
let stageRegionKey = Array.prototype.slice.call(arguments, 0);
|
||||
|
||||
JawsUtils.jawsDebug('stage and region param', stageRegionKey);
|
||||
|
||||
|
||||
102
lib/defaults/actions/EnvSet.js
Normal file
102
lib/defaults/actions/EnvSet.js
Normal file
@ -0,0 +1,102 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Action: EnvSet
|
||||
*/
|
||||
|
||||
const JawsPlugin = require('../../JawsPlugin'),
|
||||
JawsError = require('../../jaws-error'),
|
||||
JawsCLI = require('../../utils/cli'),
|
||||
path = require('path'),
|
||||
JawsUtils = require('../../utils');
|
||||
|
||||
/**
|
||||
* EnvSet Class
|
||||
*/
|
||||
|
||||
class EnvSet extends JawsPlugin {
|
||||
|
||||
/**
|
||||
* @param Jaws class object
|
||||
* @param config object
|
||||
*/
|
||||
|
||||
constructor(Jaws, config) {
|
||||
super(Jaws, config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Define your plugins name
|
||||
*
|
||||
* @returns {string}
|
||||
*/
|
||||
static getName() {
|
||||
return 'jaws.core.' + EnvSet.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {Promise} upon completion of all registrations
|
||||
*/
|
||||
|
||||
registerActions() {
|
||||
this.Jaws.action(this.envSet.bind(this), {
|
||||
handler: 'envSet',
|
||||
description: `set var value for stage and region. Region can be 'all'
|
||||
usage: jaws env set <stage> <region> <key> <val>
|
||||
|
||||
\t Ex: jaws env set prod us-east-1 TABLE_NAME users`,
|
||||
context: 'env',
|
||||
contextAction: 'set',
|
||||
options: [],
|
||||
});
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param stageRegionKey <array> stage region key val
|
||||
* @returns {Promise}
|
||||
*/
|
||||
envSet() {
|
||||
let stageRegionKeyVal = Array.prototype.slice.call(arguments, 0);
|
||||
|
||||
JawsUtils.jawsDebug('stage and region param', stageRegionKeyVal);
|
||||
|
||||
if (!stageRegionKeyVal || stageRegionKeyVal.length !== 4) {
|
||||
return Promise.reject(new JawsError('Must specify a stage, region, key and val'), JawsError.errorCodes.UNKNOWN);
|
||||
}
|
||||
|
||||
let stage = stageRegionKeyVal[0],
|
||||
region = stageRegionKeyVal[1],
|
||||
key = stageRegionKeyVal[2],
|
||||
val = stageRegionKeyVal[3];
|
||||
|
||||
return this.Jaws.getEnvFiles(region, stage)
|
||||
.then(envMapsByRegion => {
|
||||
let putEnvQ = [];
|
||||
|
||||
envMapsByRegion.forEach(mapForRegion => {
|
||||
if (!mapForRegion.vars) { //someone could have del the .env file..
|
||||
mapForRegion.vars = {};
|
||||
}
|
||||
|
||||
mapForRegion.vars[key] = val;
|
||||
|
||||
let contents = '';
|
||||
Object.keys(mapForRegion.vars).forEach(newKey => {
|
||||
contents += [newKey, mapForRegion.vars[newKey]].join('=') + '\n';
|
||||
});
|
||||
|
||||
if (stage == 'local') {
|
||||
putEnvQ.push(utils.writeFile(path.join(this.Jaws._projectRootPath, '.env'), contents));
|
||||
} else {
|
||||
putEnvQ.push(this.Jaws.putEnvFile(mapForRegion.regionName, stage, contents));
|
||||
}
|
||||
});
|
||||
|
||||
return Promise.all(putEnvQ);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = EnvSet;
|
||||
101
lib/defaults/actions/EnvUnset.js
Normal file
101
lib/defaults/actions/EnvUnset.js
Normal file
@ -0,0 +1,101 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Action: EnvUnset
|
||||
*/
|
||||
|
||||
const JawsPlugin = require('../../JawsPlugin'),
|
||||
JawsError = require('../../jaws-error'),
|
||||
JawsCLI = require('../../utils/cli'),
|
||||
path = require('path'),
|
||||
JawsUtils = require('../../utils');
|
||||
|
||||
/**
|
||||
* EnvUnset Class
|
||||
*/
|
||||
|
||||
class EnvUnset extends JawsPlugin {
|
||||
|
||||
/**
|
||||
* @param Jaws class object
|
||||
* @param config object
|
||||
*/
|
||||
|
||||
constructor(Jaws, config) {
|
||||
super(Jaws, config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Define your plugins name
|
||||
*
|
||||
* @returns {string}
|
||||
*/
|
||||
static getName() {
|
||||
return 'jaws.core.' + EnvUnset.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {Promise} upon completion of all registrations
|
||||
*/
|
||||
|
||||
registerActions() {
|
||||
this.Jaws.action(this.envUnset.bind(this), {
|
||||
handler: 'envUnset',
|
||||
description: `unset var value for stage and region. Region can be 'all'
|
||||
usage: jaws env unset <stage> <region> <key>
|
||||
|
||||
\t Ex: jaws env unset prod us-east-1 TABLE_NAME`,
|
||||
context: 'env',
|
||||
contextAction: 'unset',
|
||||
options: [],
|
||||
});
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param stageRegionKey <array> stage region key
|
||||
* @returns {Promise}
|
||||
*/
|
||||
envUnset() {
|
||||
let stageRegionKey = Array.prototype.slice.call(arguments, 0);
|
||||
|
||||
JawsUtils.jawsDebug('stage and region param', stageRegionKey);
|
||||
|
||||
if (!stageRegionKey || stageRegionKey.length !== 3) {
|
||||
return Promise.reject(new JawsError('Must specify a stage, region and key'), JawsError.errorCodes.UNKNOWN);
|
||||
}
|
||||
|
||||
let stage = stageRegionKey[0],
|
||||
region = stageRegionKey[1],
|
||||
key = stageRegionKey[2];
|
||||
|
||||
return this.Jaws.getEnvFiles(region, stage)
|
||||
.then(envMapsByRegion => {
|
||||
let putEnvQ = [];
|
||||
|
||||
envMapsByRegion.forEach(mapForRegion => {
|
||||
if (!mapForRegion.vars) { //someone could have del the .env file..
|
||||
mapForRegion.vars = {};
|
||||
}
|
||||
|
||||
delete mapForRegion.vars[key];
|
||||
|
||||
let contents = '';
|
||||
Object.keys(mapForRegion.vars).forEach(newKey => {
|
||||
contents += [newKey, mapForRegion.vars[newKey]].join('=') + '\n';
|
||||
});
|
||||
|
||||
if (stage == 'local') {
|
||||
putEnvQ.push(utils.writeFile(path.join(this.Jaws._projectRootPath, '.env'), contents));
|
||||
} else {
|
||||
putEnvQ.push(this.Jaws.putEnvFile(mapForRegion.regionName, stage, contents));
|
||||
}
|
||||
});
|
||||
|
||||
return Promise.all(putEnvQ);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = EnvUnset;
|
||||
@ -27,6 +27,14 @@
|
||||
{
|
||||
"path": "./defaults/actions/EnvGet.js",
|
||||
"config": {}
|
||||
},
|
||||
{
|
||||
"path": "./defaults/actions/EnvSet.js",
|
||||
"config": {}
|
||||
},
|
||||
{
|
||||
"path": "./defaults/actions/EnvUnset.js",
|
||||
"config": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user