Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support template-path and name CLI options for interactive CLI #9471

Merged
merged 5 commits into from May 14, 2021

Conversation

pgrzesik
Copy link
Contributor

Addresses: #9367

@pgrzesik pgrzesik added cat/cli-onboarding Interactive CLI onboarding enhancement labels May 13, 2021
@pgrzesik pgrzesik force-pushed the support-template-path-and-path-in-interactive-cli branch from 5c25c7a to 2b7ba82 Compare May 13, 2021 08:57
if (options['template-path']) {
projectName = await resolveProjectNameInput(options, workingDir);
projectDir = join(workingDir, projectName);
createFromLocalTemplate({
Copy link
Contributor Author

@pgrzesik pgrzesik May 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've went with the approach of throwing errors in cases where the invalid options were supplied (name and template-path), but I'm not 100% sure about this approach of presenting errors to users

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO good call, I think we should validate CLI options with real thrown errors, as those are forced settings (we're not re-asking user to pass fixed CLI option), and via CLI options we also want to support non interactive usage, and in such scenario it's very important that processes exit with non zero code.

@codecov
Copy link

codecov bot commented May 13, 2021

Codecov Report

Merging #9471 (f876719) into master (105807a) will increase coverage by 0.03%.
The diff coverage is 97.87%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #9471      +/-   ##
==========================================
+ Coverage   86.77%   86.80%   +0.03%     
==========================================
  Files         327      328       +1     
  Lines       11985    12022      +37     
==========================================
+ Hits        10400    10436      +36     
- Misses       1585     1586       +1     
Impacted Files Coverage Δ
lib/cli/commands-schema/no-service.js 100.00% <ø> (ø)
lib/utils/create-from-local-template.js 93.33% <93.33%> (ø)
lib/cli/interactive-setup/service.js 100.00% <100.00%> (ø)
lib/Serverless.js 91.51% <0.00%> (ø)
lib/cli/handle-error.js 88.75% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 105807a...f876719. Read the comment docs.

@pgrzesik pgrzesik requested a review from medikoo May 13, 2021 09:09
Copy link
Contributor

@medikoo medikoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pgrzesik looks great ! I have just few minor suggestions, and we should be good to go

module.exports = {
isApplicable({ serviceDir }) {
return !serviceDir;
},
async run(context) {
const workingDir = context.cwd || process.cwd();
const options = context.options || {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Options are assumed to be provided unconditionally here

const sourcePath = untildify(templatePath);

try {
copyDirContentsSync(sourcePath, projectDir, { noLinks: true });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use fse.copy instead (we also consider *Sync functions as deprecated, and they're scheduled to be replaced, and such utils removed -> #8604)

'INVALID_TEMPLATE_PATH'
);
}
throw err;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I this case I'd also throw ServerlessError, with message as Cannot copy template path: ${error.message}

module.exports = {
isApplicable({ serviceDir }) {
return !serviceDir;
},
async run(context) {
const workingDir = context.cwd || process.cwd();
const options = context.options || {};
const isConfirmed = await confirm('No project detected. Do you want to create a new one?', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this question should be skipped if any of those option is passed (it's a clear indication, that intention is to create a new project)

if (options['template-path']) {
projectName = await resolveProjectNameInput(options, workingDir);
projectDir = join(workingDir, projectName);
createFromLocalTemplate({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO good call, I think we should validate CLI options with real thrown errors, as those are forced settings (we're not re-asking user to pass fixed CLI option), and via CLI options we also want to support non interactive usage, and in such scenario it's very important that processes exit with non zero code.


return projectNameInput(workingDir);
};

module.exports = {
isApplicable({ serviceDir }) {
return !serviceDir;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also throw here errors, if given options were passed but user is in service context (we should not silently ignore them)

@pgrzesik
Copy link
Contributor Author

Thanks for review @medikoo - thanks for raising the point about serviceDir as I wanted to bring this topic up as well - I think that in cases where these options are provided, we should no longer consider it to be a fully interactive setup that detects if a project exists in current directory but rather already assume that we're going to create a new project vs "enhancing" the existing one. I'll make the adjustments 👍

@pgrzesik pgrzesik force-pushed the support-template-path-and-path-in-interactive-cli branch 2 times, most recently from 8d386a8 to af0007e Compare May 13, 2021 11:13
@pgrzesik pgrzesik requested a review from medikoo May 13, 2021 12:04
Copy link
Contributor

@medikoo medikoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great 👍 I've just suggested changes to error messaging, let me know what you think

isApplicable({ options, serviceDir }) {
if (serviceDir && (options.name || options['template-path'])) {
throw new ServerlessError(
'Project already exists in current directory. If you want to continue its setup, run "serverless" without `--name`, `--template-path` options. Otherwise, if you want to setup a new project, run the command in a non-service directory.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd simplify the message to:

Cannot setup a new service being in context of the other service ("--name" and "--template-path" options cannot be applied)

What do you think?

if (serviceDir && (options.name || options['template-path'])) {
throw new ServerlessError(
'Project already exists in current directory. If you want to continue its setup, run "serverless" without `--name`, `--template-path` options. Otherwise, if you want to setup a new project, run the command in a non-service directory.',
'INTERACTIVE_SERVICE_ALREADY_EXISTS'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not do INTERACTIVE_ prefix in case of other errors here, so I would probably not set it here.

Maybe let's name it simply NOT_APPLICABLE_SERVICE_OPTIONS (?) (when processing errors we have information of command being used, so that doesn't have to leak to error codes)

@pgrzesik pgrzesik force-pushed the support-template-path-and-path-in-interactive-cli branch from af0007e to f876719 Compare May 13, 2021 12:33
@pgrzesik pgrzesik requested a review from medikoo May 13, 2021 12:38
Copy link
Contributor

@medikoo medikoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great 👍

@pgrzesik pgrzesik merged commit 7e8e1b6 into master May 14, 2021
@pgrzesik pgrzesik deleted the support-template-path-and-path-in-interactive-cli branch May 14, 2021 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cat/cli-onboarding Interactive CLI onboarding enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants