-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(features): add experimental features management
- Loading branch information
Showing
4 changed files
with
121 additions
and
7 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,47 @@ | ||
'use strict'; | ||
|
||
const { getFeatures, setFeature } = require('../models/configuration.js'); | ||
const { AVAILABLE_FEATURES } = require('../models/features.js'); | ||
const Logger = require('../logger.js'); | ||
|
||
async function list (params) { | ||
const { format } = params.options; | ||
const features = await getFeatures(); | ||
|
||
switch (format) { | ||
case 'json': { | ||
Logger.printJson(features); | ||
break; | ||
} | ||
case 'human': | ||
default: { | ||
for (const feature in features) { | ||
console.log(`- ${feature}: ${features[feature]}`); | ||
} | ||
} | ||
} | ||
} | ||
|
||
async function enable (params) { | ||
const { 'feature-name': featureName } = params.namedArgs; | ||
|
||
if (!AVAILABLE_FEATURES.includes(featureName)) { | ||
throw new Error(`Feature '${featureName}' is not available`); | ||
} | ||
|
||
await setFeature(featureName, true); | ||
Logger.println(`Experimental feature '${featureName}' enabled`); | ||
} | ||
|
||
async function disable (params) { | ||
const { 'feature-name': featureName } = params.namedArgs; | ||
|
||
if (!AVAILABLE_FEATURES.includes(featureName)) { | ||
throw new Error(`Feature '${featureName}' is not available`); | ||
} | ||
|
||
await setFeature(featureName, false); | ||
Logger.println(`Experimental feature '${featureName}' disabled`); | ||
} | ||
|
||
module.exports = { disable, enable, list }; |
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ const env = commonEnv(Logger); | |
|
||
const CONFIG_FILES = { | ||
MAIN: 'clever-tools.json', | ||
FEATURES: 'clever-tools-features.json', | ||
IDS_CACHE: 'ids-cache.json', | ||
}; | ||
|
||
|
@@ -61,6 +62,40 @@ async function writeOAuthConf (oauthData) { | |
} | ||
} | ||
|
||
async function loadFeaturesConf () { | ||
Logger.debug('Load features configuration from ' + conf.FEATURES_FILE); | ||
try { | ||
const rawFile = await fs.readFile(conf.FEATURES_FILE); | ||
return JSON.parse(rawFile); | ||
} | ||
catch (error) { | ||
Logger.info(`Cannot load experimental features configuration from ${conf.FEATURES_FILE}`); | ||
return {}; | ||
} | ||
} | ||
|
||
async function getFeatures () { | ||
Logger.debug('Get features configuration from ' + conf.FEATURES_FILE); | ||
try { | ||
const rawFile = await fs.readFile(conf.FEATURES_FILE); | ||
return JSON.parse(rawFile); | ||
} | ||
catch (error) { | ||
throw new Error(`Cannot get experimental features configuration from ${conf.FEATURES_FILE}`); | ||
} | ||
} | ||
|
||
async function setFeature (feature, value) { | ||
const currentFeatures = await getFeatures(); | ||
const newFeatures = { ...currentFeatures, ...{ [feature]: value } }; | ||
try { | ||
await fs.writeFile(conf.FEATURES_FILE, JSON.stringify(newFeatures, null, 2)); | ||
} | ||
catch (error) { | ||
throw new Error(`Cannot write experimental features configuration to ${conf.FEATURES_FILE}`); | ||
} | ||
} | ||
|
||
async function loadIdsCache () { | ||
const cachePath = getConfigPath(CONFIG_FILES.IDS_CACHE); | ||
try { | ||
|
@@ -100,11 +135,12 @@ const conf = env.getOrElseAll({ | |
SSH_GATEWAY: '[email protected]', | ||
|
||
CONFIGURATION_FILE: getConfigPath(CONFIG_FILES.MAIN), | ||
FEATURES_FILE: getConfigPath(CONFIG_FILES.FEATURES), | ||
CONSOLE_TOKEN_URL: 'https://console.clever-cloud.com/cli-oauth', | ||
// CONSOLE_TOKEN_URL: 'https://next-console.cleverapps.io/cli-oauth', | ||
|
||
CLEVER_CONFIGURATION_DIR: path.resolve('.', 'clevercloud'), | ||
APP_CONFIGURATION_FILE: path.resolve('.', '.clever.json'), | ||
}); | ||
|
||
module.exports = { conf, loadOAuthConf, writeOAuthConf, loadIdsCache, writeIdsCache }; | ||
module.exports = { conf, loadOAuthConf, writeOAuthConf, loadFeaturesConf, getFeatures, setFeature, loadIdsCache, writeIdsCache }; |
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,5 @@ | ||
'use strict'; | ||
|
||
const AVAILABLE_FEATURES = ['faas', 'kv', 'ng']; | ||
|
||
module.exports = { AVAILABLE_FEATURES }; |