Skip to content

Commit

Permalink
refactor(CLI): Modern logs for interactive setup
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Sep 29, 2021
1 parent cd03f55 commit 07aed34
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 57 deletions.
70 changes: 52 additions & 18 deletions lib/cli/interactive-setup/aws-credentials.js
@@ -1,6 +1,7 @@
'use strict';

const chalk = require('chalk');
const { legacy, log, style, progress } = require('@serverless/utils/log');
const _ = require('lodash');
const inquirer = require('@serverless/utils/inquirer');
const promptWithHistory = require('@serverless/utils/inquirer/prompt-with-history');
Expand Down Expand Up @@ -156,10 +157,18 @@ const credentialsSetupChoice = async (context, providers) => {
};

const steps = {
writeOnSetupSkip: () =>
process.stdout.write(`\nYou can setup your AWS account later. More details available here:
writeOnSetupSkip: () => {
legacy.write(`\nYou can setup your AWS account later. More details available here:
http://slss.io/aws-creds-setup\n`),
http://slss.io/aws-creds-setup\n`);

log.notice();
log.notice.skip(
`You can setup your AWS account later. More details available here: ${style.link(
'http://slss.io/aws-creds-setup'
)}`
);
},

ensureAwsAccount: async ({ stepHistory }) => {
if (await confirm('Do you have an AWS account?', { name: 'hasAwsAccount' })) return;
Expand All @@ -185,21 +194,29 @@ const steps = {
const accessKeyId = await awsAccessKeyIdInput(context);
const secretAccessKey = await awsSecretAccessKeyInput(context);
await awsCredentials.saveFileProfiles(new Map([['default', { accessKeyId, secretAccessKey }]]));
process.stdout.write(
legacy.write(
`\n${chalk.green(
`AWS credentials saved on your machine at ${chalk.bold(
process.platform === 'win32' ? '%userprofile%\\.aws\\credentials' : '~/.aws/credentials'
)}. Go there to change them at any time.`
)}\n`
);
log.notice();
log.notice.success(
`AWS credentials saved on your machine at "${
process.platform === 'win32' ? '%userprofile%\\.aws\\credentials' : '~/.aws/credentials'
}". Go there to change them at any time.`
);
},
handleProviderCreation: async ({ configuration: { org: orgName }, stepHistory }) => {
const providersUrl = `https://app.serverless.com/${orgName}/settings/providers?source=cli&providerId=new&provider=aws`;
openBrowser(chalk.bold.white(providersUrl));
process.stdout.write(
'To learn more about providers, visit: http://slss.io/add-providers-dashboard\n'
);
process.stdout.write('\nWaiting for creation of AWS Access Role provider...\n');
legacy.write('To learn more about providers, visit: http://slss.io/add-providers-dashboard\n');
log.notice('To learn more about providers, visit: http://slss.io/add-providers-dashboard');
legacy.write('\nWaiting for creation of AWS Access Role provider...\n');

const providerProgress = progress.get('provider');
providerProgress.notice('Waiting for creation of AWS Access Role provider');

let onEvent;
let showSkipPromptTimeout;
Expand Down Expand Up @@ -264,16 +281,21 @@ const steps = {
sdk.disconnect();
}

providerProgress.remove();

log.notice();
if (maybeEvent) {
process.stdout.write(
`\n${chalk.green('AWS Access Role provider was successfully created')}\n`
);
legacy.write(`\n${chalk.green('AWS Access Role provider was successfully created')}\n`);
log.notice.success('AWS Access Role provider was successfully created');
return maybeEvent.data.object.provider_uid;
}

process.stdout.write(
legacy.write(
'\nSkipping credentials provider setup. You can still setup credentials provider later.\n'
);
log.notice.skip(
'Skipping credentials provider setup. You can still setup credentials provider later.'
);
return null;
},
linkProviderToServiceInstance: async ({ providerUid, configuration, options }) => {
Expand All @@ -288,7 +310,9 @@ const steps = {
orgUid = await getOrgUidByName(org);
} catch (err) {
if (err.code === 'DASHBOARD_UNAVAILABLE') {
process.stdout.write(`\n${chalk.yellow(err.message)}\n`);
legacy.write(`\n${chalk.yellow(err.message)}\n`);
log.error();
log.error(err.message);
return false;
}
throw err;
Expand All @@ -298,11 +322,13 @@ const steps = {
return true;
} catch (err) {
if (err.statusCode && err.statusCode >= 500) {
process.stdout.write(
legacy.write(
`\n${chalk.yellow(
'Dashboard service is currently unavailable, please try again later'
)}\n`
);
log.error();
log.error('Dashboard service is currently unavailable, please try again later');
return false;
}
throw err;
Expand Down Expand Up @@ -342,7 +368,9 @@ module.exports = {
providers = await getProviders(orgName);
} catch (err) {
if (err.code === 'DASHBOARD_UNAVAILABLE') {
process.stdout.write(`\n${chalk.yellow(err.message)}\n`);
legacy.write(`\n${chalk.yellow(err.message)}\n`);
log.error();
log.error(err.message);
return false;
}
throw err;
Expand Down Expand Up @@ -378,7 +406,9 @@ module.exports = {
providers = await getProviders(context.configuration.org);
} catch (err) {
if (err.code === 'DASHBOARD_UNAVAILABLE') {
process.stdout.write(`\n${chalk.yellow(err.message)}\n`);
legacy.write(`\n${chalk.yellow(err.message)}\n`);
log.error();
log.error(err.message);
return;
}
throw err;
Expand All @@ -404,7 +434,9 @@ module.exports = {
return;
} catch (err) {
if (err.code === 'DASHBOARD_UNAVAILABLE') {
process.stdout.write(`\n${chalk.yellow(err.message)}\n`);
legacy.write(`\n${chalk.yellow(err.message)}\n`);
log.error();
log.error(err.message);
return;
}
throw err;
Expand All @@ -427,9 +459,11 @@ module.exports = {
});

if (linked) {
process.stdout.write(
legacy.write(
`\n${chalk.green('Selected provider was successfully linked to your service')}\n`
);
log.notice();
log.notice.success('Selected provider was successfully linked to your service');
}
},
steps,
Expand Down
8 changes: 7 additions & 1 deletion lib/cli/interactive-setup/dashboard-login.js
@@ -1,10 +1,12 @@
'use strict';

const _ = require('lodash');
const { legacy } = require('@serverless/utils/log');
const { ServerlessSDK } = require('@serverless/platform-client');
const login = require('@serverless/dashboard-plugin/lib/login');
const configUtils = require('@serverless/utils/config');
const promptWithHistory = require('@serverless/utils/inquirer/prompt-with-history');
const { showOnboardingWelcome } = require('./utils');

const loginOrRegisterQuestion = async (stepHistory) =>
promptWithHistory({
Expand Down Expand Up @@ -64,9 +66,13 @@ module.exports = {
return !isLoggedIn;
},
async run(context) {
process.stdout.write(
legacy.write(
'Enable Serverless Dashboard to get enhanced monitoring, logs and secrets management: https://serverless.com/monitoring\n\n'
);
if (context.initial.isInServiceContext && !context.initial.isDashboardEnabled) {
showOnboardingWelcome(context);
}

return steps.loginOrRegister(context);
},
steps,
Expand Down
37 changes: 30 additions & 7 deletions lib/cli/interactive-setup/dashboard-set-org.js
Expand Up @@ -2,11 +2,12 @@

const _ = require('lodash');
const chalk = require('chalk');
const { legacy, log } = require('@serverless/utils/log');
const accountUtils = require('@serverless/utils/account');
const configUtils = require('@serverless/utils/config');
const { ServerlessSDK } = require('@serverless/platform-client');
const promptWithHistory = require('@serverless/utils/inquirer/prompt-with-history');
const { writeOrgAndApp } = require('./utils');
const { writeOrgAndApp, showOnboardingWelcome } = require('./utils');
const {
getPlatformClientWithAccessKey,
getOrCreateAccessKeyForOrg,
Expand All @@ -15,9 +16,13 @@ const {
const isValidAppName = RegExp.prototype.test.bind(/^[a-z0-9](?:[a-z0-9-]{0,126}[a-z0-9])?$/);

const orgUpdateConfirm = async (stepHistory) => {
process.stdout.write(
legacy.write(
"Service has monitoring enabled, but it is configured with the 'org' to which you do not have access\n\n"
);
log.notice(
'Service has monitoring enabled, but it is configured with the "org" to which you do not have access'
);
log.notice();
const shouldUpdateOrg = await promptWithHistory({
message: 'Would you like to update it?',
type: 'confirm',
Expand All @@ -28,9 +33,13 @@ const orgUpdateConfirm = async (stepHistory) => {
};

const appUpdateConfirm = async (appName, orgName, stepHistory) => {
process.stdout.write(
legacy.write(
"Service seems to have monitoring enabled, but configured app doesn't seem to exist in an organization.\n\n"
);
log.notice(
"Service seems to have monitoring enabled, but configured app doesn't seem to exist in an organization."
);
log.notice();

const appUpdateType = await promptWithHistory({
message: 'What would you like to do?',
Expand Down Expand Up @@ -187,11 +196,13 @@ const steps = {
return;
}
}
process.stdout.write(
legacy.write(
`\n${chalk.green('Your project has been setup with org ')}${chalk.white.bold(
orgName
)}${chalk.green(' and app ')}${chalk.white.bold(appName)}\n`
);
log.notice();
log.notice.success(`Your project has been setup with org "${orgName}" and app "${appName}"`);
await writeOrgAndApp(orgName, appName, context);
return;
},
Expand Down Expand Up @@ -285,11 +296,13 @@ module.exports = {
};
}
if (options.app) {
process.stdout.write(
legacy.write(
chalk.red(
"\nPassed value for `--app` doesn't seem to correspond to chosen organization.\n"
)
);
log.error();
log.error('Passed value for "--app" doesn\'t seem to correspond to chosen organization.');
}
return {
user,
Expand All @@ -313,15 +326,21 @@ module.exports = {
};
} else if (orgName) {
if (options.org) {
process.stdout.write(
legacy.write(
chalk.red(
"\nPassed value for `--org` doesn't seem to correspond to account with which you're logged in with.\n"
)
);
log.error();
log.error(
'Passed value for "--org" doesn\'t seem to correspond to account with which you\'re logged in with.'
);
} else {
process.stdout.write(
legacy.write(
chalk.red(`\nConfigured org '${orgName}' is not available in your account.\n`)
);
log.error();
log.error(`Configured org "${orgName}" is not available in your account.`);
}
}
return {
Expand All @@ -334,6 +353,10 @@ module.exports = {
},
async run(context, stepData) {
const { configuration, options, stepHistory } = context;
if (context.initial.isInServiceContext && !context.initial.isDashboardEnabled) {
showOnboardingWelcome(context);
}

if (!stepData.orgName) delete configuration.org;
if (!stepData.appName && !stepData.newAppName) delete configuration.app;
if (!options.org && !options.app) {
Expand Down

0 comments on commit 07aed34

Please sign in to comment.