-
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.
- Loading branch information
Showing
4 changed files
with
176 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,77 @@ | ||
import { getFeatures, setFeature } from '../models/configuration.js'; | ||
import { EXPERIMENTAL_FEATURES } from '../experimental-features.js'; | ||
import { formatTable } from '../format-table.js'; | ||
import { Logger } from '../logger.js'; | ||
|
||
function getPropertyMaxWidth (array, propertyName) { | ||
return Math.max(...array.map((o) => o[propertyName].length)); | ||
} | ||
|
||
export async function list (params) { | ||
const { format } = params.options; | ||
|
||
const features_conf = await getFeatures(); | ||
const features = EXPERIMENTAL_FEATURES.map((feature) => { | ||
const enabled = features_conf[feature.id] === true; | ||
return { ...feature, 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']; | ||
const columnWidths = [ | ||
getPropertyMaxWidth(features, 'id'), | ||
getPropertyMaxWidth(features, 'status'), | ||
getPropertyMaxWidth(features, 'description'), | ||
getPropertyMaxWidth(features, 'enabled'), | ||
]; | ||
|
||
// We calculate the maximum width of each column to format the table | ||
const formatTableWithColumnWidth = formatTable(columnWidths); | ||
|
||
Logger.println(formatTableWithColumnWidth([ | ||
headers, | ||
...features.map((feature) => [ | ||
feature.id, | ||
feature.status, | ||
feature.description, | ||
feature.enabled, | ||
]), | ||
])); | ||
} | ||
} | ||
} | ||
|
||
export async function enable (params) { | ||
const { features } = params.namedArgs; | ||
const availableFeatures = EXPERIMENTAL_FEATURES.map((feature) => feature.id); | ||
|
||
for (const featureName of features) { | ||
if (!availableFeatures.includes(featureName)) { | ||
Logger.printErrorLine(`- Feature '${featureName}' is not available`); | ||
continue; | ||
} | ||
await setFeature(featureName, true); | ||
Logger.println(`- Experimental feature '${featureName}' enabled`); | ||
} | ||
} | ||
|
||
export async function disable (params) { | ||
const { features } = params.namedArgs; | ||
const availableFeatures = EXPERIMENTAL_FEATURES.map((feature) => feature.id); | ||
|
||
for (const featureName of features) { | ||
if (!availableFeatures.includes(featureName)) { | ||
Logger.printErrorLine(`- Feature '${featureName}' is not available`); | ||
continue; | ||
} | ||
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,12 @@ | ||
export const EXPERIMENTAL_FEATURES = [ | ||
{ | ||
id: 'kv', | ||
status: 'alpha', | ||
description: 'Send commands to Materia KV directly from Clever Tools, without other dependencies', | ||
}, | ||
{ | ||
id: 'ng', | ||
status: 'beta', | ||
description: 'Manage Network Groups to link applications, add-ons, external peers in a Wireguard® network', | ||
}, | ||
]; |
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,48 @@ export async function writeIdsCache (ids) { | |
} | ||
} | ||
|
||
export async function loadFeaturesConf () { | ||
Logger.debug('Load 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') { | ||
Logger.info(`Cannot load experimental features configuration from ${conf.EXPERIMENTAL_FEATURES_FILE}`); | ||
} | ||
|
||
return {}; | ||
} | ||
} | ||
|
||
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 +141,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', | ||
|
||
|