Skip to content

Commit

Permalink
Merge pull request #21 from entando-k8s/ENG-1285_fix-cr-interactive-s…
Browse files Browse the repository at this point in the history
…ession-not-working

Eng 1285 fix custom resource interactive session not working
  • Loading branch information
ffleandro authored Sep 18, 2020
2 parents 463be18 + 4ebd8db commit 5b6feba
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 182 deletions.
4 changes: 2 additions & 2 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const program = require('commander');

const { generateFromNpm, generateFromGit, generateFromEnv, generateInteractive } = require('../lib/actions');
const { generateFromNpm, generateFromGit, generateFromEnv, generateInteractivly } = require('../lib/actions');
const version = require('../package.json').version;

program.storeOptionsAsProperties(false);
Expand Down Expand Up @@ -46,5 +46,5 @@ program
if (process.argv.length > 2) {
program.parse(process.argv);
} else {
generateInteractive();
generateInteractivly();
}
66 changes: 54 additions & 12 deletions lib/actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const yaml = require('js-yaml');
const inquirer = require('inquirer');
const highlight = require('cli-highlight').highlight;

const npmBundler = require('./npm-bundler');
const gitBundler = require('./git-bundler');
Expand All @@ -7,21 +9,44 @@ const { convertNpmModuleToEntandoDeBundle, convertGitRepositoryToEntandoDeBundle

const k8s = require('./k8s');

const generateInteractive = require('./interactive-session').interactiveSession;
function generateInteractively () {
const mainQuestion = [
{
type: 'list',
name: 'action',
message: 'What do you want to do?',
choices: [
{ value: 'from-git', name: 'Generate a custom-resource based on an existing bundle project' },
{ value: 'from-env', name: 'Create a new bundle using components from an environment' },
],
},
];

inquirer
.prompt(mainQuestion)
.then((answers) => {
switch (answers.action) {
case 'from-git':
gitBundler.interactiveSession().then(answers => generateFromGit(answers));
break;
case 'from-env':
envBundler.interactiveSession().then(answers => generateFromEnv(answers));
break;
default:
console.error(`Illegal command: ${answers.action}`);
throw new Error(`Illegal command ${answers.action}`);
}
})
.catch(error => {
console.error('An error occurred while running interactive session', error);
});
}

function generate (obj, opts) {
const output = yaml.safeDump(obj);
if (opts.dryRun) {
console.log(output);
print(obj);
} else {
k8s
.apply(obj, opts)
.then((data) => {
console.log(data);
})
.catch((err) => {
console.error(err);
});
deploy(obj, opts);
}
}

Expand Down Expand Up @@ -62,9 +87,26 @@ function generateFromEnv (options) {
});
}

function print (obj) {
const output = `---\n${highlight(yaml.safeDump(obj), { language: 'yaml' })}`;
console.log(output);
}

function deploy (obj, opts) {
console.log('\nDeploying the bundle on kuberentes\n\n');
k8s
.apply(obj, opts)
.then((data) => {
console.log(data);
})
.catch((err) => {
console.error(err);
});
}

module.exports = {
generateFromNpm,
generateFromGit,
generateFromEnv,
generateInteractive,
generateInteractivly: generateInteractively,
};
Loading

0 comments on commit 5b6feba

Please sign in to comment.