Skip to content

Commit

Permalink
wip interactive mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jbudz committed Nov 10, 2020
1 parent 84dfc02 commit 6240c79
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
26 changes: 5 additions & 21 deletions src/cli_encryption_key/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,21 @@
*/

import { safeDump } from 'js-yaml';
import { appendFileSync } from 'fs';
import { isEmpty } from 'lodash';
import { join } from 'path';

import { getConfigDirectory } from '@kbn/utils';

import { confirm } from '../cli_keystore/utils';
import { interactive } from './interactive';
import { Logger } from '../cli_plugin/lib/logger';

export async function generate(encryptionConfig, command) {
const logger = new Logger();
const keys = encryptionConfig.generate({ force: command.force });
if (!command.quiet) {
if (command.force) {
logger.log('Regenerating all encryption keys. Add these to kibana.yml:');
} else {
logger.log('Generating missing encryption keys. Add these to kibana.yml:');
}
}
if (isEmpty(keys)) {
logger.log('No keys to write. Use the --force flag to generate new keys.');
} else {
logger.log(safeDump(keys));

if (command.interactive) {
const write = await confirm('Write to kibana.yml?');
if (write) {
const kibanaYML = join(getConfigDirectory(), 'kibana.yml');
appendFileSync(kibanaYML, safeDump(keys));
logger.log(`Wrote ${Object.keys(keys).length} settings to kibana.yml.`);
}
await interactive(keys, logger);
} else {
if (!command.quiet) logger.log('Generating encryption keys.');
logger.log(safeDump(keys));
}
}
if (command.force && !command.quiet) {
Expand Down
45 changes: 45 additions & 0 deletions src/cli_encryption_key/interactive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { appendFileSync } from 'fs';
import { join } from 'path';
import { confirm } from '../cli_keystore/utils';
import { getConfigDirectory } from '@kbn/utils';
import { safeDump } from 'js-yaml';

export async function interactive(keys, logger) {
const settings = Object.keys(keys);
logger.log('Generating settings for:');
logger.log(settings.join('\n'));
logger.log('');
const setKeys = {};
for (let i = 0; i < settings.length; i++) {
const setting = settings[i];
const include = await confirm(`Set ${setting}?`);
if (include) setKeys[setting] = keys[setting];
}
const count = Object.keys(setKeys).length;
const plural = count > 1 ? 's' : '';
const write = await confirm(
`Write ${Object.keys(setKeys).length} setting${plural} to kibana.yml?`
);
if (write) {
const kibanaYML = join(getConfigDirectory(), 'kibana.yml');
appendFileSync(kibanaYML, safeDump(setKeys));
}
}

0 comments on commit 6240c79

Please sign in to comment.