Skip to content

Commit a49d296

Browse files
authored
Release/0.0.12 (#6)
1 parent 4ddce14 commit a49d296

File tree

5 files changed

+87
-11
lines changed

5 files changed

+87
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "hlambda-cli",
33
"type": "module",
4-
"version": "0.0.11",
4+
"version": "0.0.12",
55
"description": "CLI for hlambda server.",
66
"main": "src/index.js",
77
"bin": {

src/commands/metadata.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,49 @@ export const metadataExport = async (options, program) => {
247247
.then(() => {})
248248
.catch(CLIErrorHandler(program));
249249
};
250+
251+
export const metadataSync = async (options, program) => {
252+
await (async () => {
253+
const cwd = path.resolve(process.cwd());
254+
console.log('Executing in cwd:'.green, `${cwd}`.yellow);
255+
256+
// Load yaml configuration
257+
const configuration = await loadConfigFromYAML(options);
258+
259+
const endpoint = configuration?.endpoint ?? 'http://localhost:8081';
260+
const adminSecret = options?.adminSecret ?? configuration?.admin_secret ?? '';
261+
262+
const headers = {
263+
'x-hlambda-admin-secret': adminSecret,
264+
};
265+
const response = await fetch(`${endpoint}/console/api/v1/metadata/sync`, {
266+
method: 'GET',
267+
// body: formData,
268+
headers,
269+
});
270+
271+
if (response.status === 200) {
272+
console.log('Metadata synced!'.green);
273+
}
274+
console.log(response.status);
275+
276+
if (response.status !== 200) {
277+
throw new Error(errors.ERROR_INVALID_HLAMBDA_ADMIN_SECRET);
278+
}
279+
280+
// This is magic from commander, the real flag was --no-auto-reload but we get positive logic transformation to autoReload
281+
if (options?.autoReload) {
282+
const responseRestart = await fetch(`${endpoint}/console/api/v1/trigger-restart`, {
283+
method: 'GET',
284+
// body: formData,
285+
headers,
286+
});
287+
if (responseRestart.status === 200) {
288+
console.log('Metadata reloaded after clearing!'.green);
289+
}
290+
console.log(responseRestart.status);
291+
}
292+
})()
293+
.then(() => {})
294+
.catch(CLIErrorHandler(program));
295+
};

src/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { config } from './commands/config.js';
1616
import { save } from './commands/save.js';
1717
import { requests } from './commands/requests.js';
1818
import { startConsole } from './commands/console.js';
19-
import { serverReload, serverClearMetadata, metadataApply, metadataExport } from './commands/metadata.js';
19+
import { serverReload, serverClearMetadata, metadataApply, metadataExport, metadataSync } from './commands/metadata.js';
2020
import { checkForNewVersion, checkWhatIsNewInCurrentVersion } from './commands/update.js';
2121
import {
2222
serverGetLogs,
@@ -254,6 +254,16 @@ metadata
254254
.option('-s, --admin-secret <secret>', 'Admin secret used for auth.')
255255
.action(metadataExport);
256256

257+
metadata
258+
.command('sync')
259+
.alias('s')
260+
.description('Sync metadata from the remote repository.')
261+
.option('-e, --env <env_name>', 'Select environment.', '')
262+
.option('-c, --config <path>', 'Path to config.yaml file.', '')
263+
.option('-s, --admin-secret <secret>', 'Admin secret used for auth.')
264+
.option('--no-auto-reload', 'should metadata apply skip auto reload.')
265+
.action(metadataSync);
266+
257267
// --- Server sub-program ---
258268
const serverProgram = program.command('server').alias('s').description('Do basic server request.');
259269

src/templates/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
export const configTemplate = `version: 1
2-
endpoint: http://localhost:8081
32
metadata_directory: metadata
3+
endpoint: "{{ENV_LOCAL_HLAMBDA_ENDPOINT}}"
4+
admin_secret: "{{ENV_LOCAL_HLAMBDA_ADMIN_SECRET}}"
5+
6+
# endpoint: "http://localhost:8081"
47
# admin_secret: demo
58
69
# metadata_post_apply_script:
@@ -16,7 +19,12 @@ metadata_directory: metadata
1619
1720
`;
1821

19-
export const rootDotenvTemplate = `
22+
export const rootDotenvTemplate = `# Remove "#" to uncomment the env values.
23+
ENV_LOCAL_HLAMBDA_ENDPOINT="http://localhost:8081"
24+
ENV_LOCAL_HLAMBDA_ADMIN_SECRET="demo"
25+
26+
# ENV_DEV_HLAMBDA_ENDPOINT="http://dev-server:8081"
27+
# ENV_DEV_HLAMBDA_ADMIN_SECRET="demo-dev"
2028
`;
2129

2230
export const rootGitIgnoreTemplate = `.env

src/utils/loadConfigFromYAML.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,29 @@ import { errors } from './../errors/index.js';
77

88
const replaceValueWithProcessEnv = (_newConf, key) => {
99
const strValue = _newConf?.[key] ?? '';
10+
1011
const tSecret = strValue.match(/^{{(.+)}}$/m);
12+
const tempOne = {};
1113
if (tSecret) {
1214
console.log(`Override value for key: ${key} with value from process.env[${tSecret[1]}]`.yellow);
13-
const temp = {};
14-
temp[key] = process.env?.[tSecret[1]] ?? '';
15-
return {
16-
..._newConf,
17-
...temp,
18-
};
15+
tempOne[key] = process.env?.[tSecret[1]] ?? '';
1916
}
20-
return _newConf;
17+
18+
// Idea to have namespacing for replacing values from different scopes.
19+
// Example; env.ENV_VARIABLE_VALUE, but for now it is good as is.
20+
21+
// const tSecretEnv = strValue.match(/^{{env\.(.+)}}$/m);
22+
// const tempTwo = {};
23+
// if (tSecretEnv) {
24+
// console.log(`Override value for key: env.${key} with value from process.env[${tSecretEnv[1]}]`.yellow);
25+
// tempTwo[key] = process.env?.[tSecretEnv[1]] ?? '';
26+
// }
27+
28+
return {
29+
..._newConf,
30+
...tempOne,
31+
// ...tempTwo,
32+
};
2133
};
2234

2335
export const loadConfigFromYAML = async (options) => {

0 commit comments

Comments
 (0)