-
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.
Merge pull request #785 from CleverCloud/davlgd-feature-flags
feat(features): add experimental features management
- Loading branch information
Showing
4 changed files
with
154 additions
and
3 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,93 @@ | ||
import { getFeatures, setFeature } from '../models/configuration.js'; | ||
import { EXPERIMENTAL_FEATURES } from '../experimental-features.js'; | ||
import { formatTable as initFormatTable } from '../format-table.js'; | ||
import { Logger } from '../logger.js'; | ||
|
||
const formatTable = initFormatTable(); | ||
|
||
export async function list (params) { | ||
const { format } = params.options; | ||
|
||
const featuresConf = await getFeatures(); | ||
// Add status from configuration file and remove instructions | ||
const features = Object.entries(EXPERIMENTAL_FEATURES).map(([id, feature]) => { | ||
const enabled = featuresConf[id] === true; | ||
return { | ||
id, | ||
status: feature.status, | ||
description: feature.description, | ||
enabled, | ||
}; | ||
}); | ||
|
||
// For each feature, print the object with the id, status, description and enabled | ||
switch (format) { | ||
case 'json': { | ||
Logger.printJson(features); | ||
break; | ||
} | ||
case 'human': | ||
default: { | ||
const headers = ['ID', 'STATUS', 'DESCRIPTION', 'ENABLED']; | ||
|
||
Logger.println(formatTable([ | ||
headers, | ||
...features.map((feature) => [ | ||
feature.id, | ||
feature.status, | ||
feature.description, | ||
feature.enabled, | ||
]), | ||
])); | ||
} | ||
} | ||
} | ||
|
||
export async function info (params) { | ||
const { feature } = params.namedArgs; | ||
const availableFeatures = Object.keys(EXPERIMENTAL_FEATURES); | ||
|
||
if (!availableFeatures.includes(feature)) { | ||
throw new Error(`Unavailable feature: ${feature}`); | ||
} | ||
|
||
Logger.println(EXPERIMENTAL_FEATURES[feature].instructions); | ||
} | ||
|
||
export async function enable (params) { | ||
const { features } = params.namedArgs; | ||
const availableFeatures = Object.keys(EXPERIMENTAL_FEATURES); | ||
|
||
const unknownFeatures = features.filter((feature) => !availableFeatures.includes(feature)); | ||
if (unknownFeatures.length > 0) { | ||
throw new Error(`Unavailable feature(s): ${unknownFeatures.join(', ')}`); | ||
} | ||
|
||
for (const featureName of features) { | ||
await setFeature(featureName, true); | ||
Logger.println(`Experimental feature '${featureName}' enabled`); | ||
} | ||
|
||
if (features.length === 1) { | ||
Logger.println(EXPERIMENTAL_FEATURES[features[0]].instructions); | ||
} | ||
else { | ||
Logger.println(); | ||
Logger.println("To learn more about these experimental features, use 'clever features info FEATURE_NAME'"); | ||
} | ||
} | ||
|
||
export async function disable (params) { | ||
const { features } = params.namedArgs; | ||
const availableFeatures = Object.keys(EXPERIMENTAL_FEATURES); | ||
|
||
const unknownFeatures = features.filter((feature) => !availableFeatures.includes(feature)); | ||
if (unknownFeatures.length > 0) { | ||
throw new Error(`Unavailable feature(s): ${unknownFeatures.join(', ')}`); | ||
} | ||
|
||
for (const featureName of features) { | ||
await setFeature(featureName, false); | ||
Logger.println(`Experimental feature '${featureName}' disabled`); | ||
} | ||
} |
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 @@ | ||
export const EXPERIMENTAL_FEATURES = {}; |
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ const env = commonEnv(Logger); | |
const CONFIG_FILES = { | ||
MAIN: 'clever-tools.json', | ||
IDS_CACHE: 'ids-cache.json', | ||
EXPERIMENTAL_FEATURES_FILE: 'clever-tools-experimental-features.json', | ||
}; | ||
|
||
function getConfigPath (configFile) { | ||
|
@@ -85,6 +86,32 @@ export async function writeIdsCache (ids) { | |
} | ||
} | ||
|
||
export async function getFeatures () { | ||
Logger.debug('Get features configuration from ' + conf.EXPERIMENTAL_FEATURES_FILE); | ||
try { | ||
const rawFile = await fs.readFile(conf.EXPERIMENTAL_FEATURES_FILE); | ||
return JSON.parse(rawFile); | ||
} | ||
catch (error) { | ||
if (error.code !== 'ENOENT') { | ||
throw new Error(`Cannot get experimental features configuration from ${conf.EXPERIMENTAL_FEATURES_FILE}`); | ||
} | ||
return {}; | ||
} | ||
} | ||
|
||
export async function setFeature (feature, value) { | ||
const currentFeatures = await getFeatures(); | ||
const newFeatures = { ...currentFeatures, ...{ [feature]: value } }; | ||
|
||
try { | ||
await fs.writeFile(conf.EXPERIMENTAL_FEATURES_FILE, JSON.stringify(newFeatures, null, 2)); | ||
} | ||
catch (error) { | ||
throw new Error(`Cannot write experimental features configuration to ${conf.EXPERIMENTAL_FEATURES_FILE}`); | ||
} | ||
} | ||
|
||
export const conf = env.getOrElseAll({ | ||
API_HOST: 'https://api.clever-cloud.com', | ||
// API_HOST: 'https://ccapi-preprod.cleverapps.io', | ||
|
@@ -98,6 +125,7 @@ export const conf = env.getOrElseAll({ | |
SSH_GATEWAY: '[email protected]', | ||
|
||
CONFIGURATION_FILE: getConfigPath(CONFIG_FILES.MAIN), | ||
EXPERIMENTAL_FEATURES_FILE: getConfigPath(CONFIG_FILES.EXPERIMENTAL_FEATURES_FILE), | ||
CONSOLE_TOKEN_URL: 'https://console.clever-cloud.com/cli-oauth', | ||
// CONSOLE_TOKEN_URL: 'https://next-console.cleverapps.io/cli-oauth', | ||
|
||
|