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

Adds prompt to overwrite when rerunning scaffold init #61

Merged
merged 1 commit into from
Nov 12, 2021

Conversation

jcsackett
Copy link
Contributor

@jcsackett jcsackett commented Nov 8, 2021

Currently running scaffold init a second time results in an error
message being propogated to the user as the cli output. This is
harmless, as the error regards the files being created already existing,
but not a great UX.

This adds promptui to run an interactive prompt for overwriting the
files. If the user selects to overwite, the command behaves as though it
were run with the -force option. If the user doesn't, it ends the
command but doesn't return an error.

Other related changes introduced by this change:

  • The cli app is wrapped in a new VervetApp struct, allowing config
    options specific to vervet but not to the CLI interface to be set--
    one of these options is a prompting interface.
  • The vervet app is added to the context passed into the cli app, so
    that vervet specific info can be used in the commands called by the
    cli app.

Fixes #56

@jcsackett jcsackett requested a review from a team as a code owner November 8, 2021 16:25
@CLAassistant
Copy link

CLAassistant commented Nov 8, 2021

CLA assistant check
All committers have signed the CLA.

@jcsackett jcsackett force-pushed the feat/prompt-when-scaffold-yaml-exists branch 2 times, most recently from 1e9fc34 to ba4c401 Compare November 8, 2021 16:32
Copy link
Contributor

@cmars cmars left a comment

Choose a reason for hiding this comment

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

LGTM with a couple of minor things.

cmd/scaffold.go Outdated Show resolved Hide resolved
cmd/scaffold.go Outdated
return nil
},
} // Not using IsConfirm; it seems to have rendering issues in a few terminal cases.
if result, err := prompt.Run(); err == nil && result == "y" {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we propagate the error if err != nil ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can; I had thought that in the event where Prompt.Run failed something weird enough happened that we should just do nothing (e.g. return nil) but that does obfuscate that something has indeed gone wrong.

I'll update to return the error; when expanding the use of promptui maybe I can put together something better to handle Run errors than just returning it to the the user.

@jcsackett jcsackett force-pushed the feat/prompt-when-scaffold-yaml-exists branch from 8f5a32b to 2c11be5 Compare November 11, 2021 02:26
Copy link
Contributor

@cmars cmars left a comment

Choose a reason for hiding this comment

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

LGTM with a couple thoughts.

Comment on lines +41 to +43
vervetApp, ok := ctx.Context.Value(vervetKey).(*VervetApp)
if !ok {
return errors.New("could not retrieve vervet app from context")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

A func AppFromContext(ctx context.Context) *VervetApp helper might be nice for this. If not in this PR, please leave a TODO. vervet version new also needs a prompting capability, it can be refactored when we get to that one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm strongly in agreement with this idea, thanks! I've left the TODO for the follow up. A contextWithApp helper might be cleaner too where it's set up in cmd/cmd.go

cmd/scaffold.go Outdated
@@ -34,7 +35,27 @@ func ScaffoldInit(ctx *cli.Context) error {
}
err = sc.Organize()
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if os.IsExist would be a more precise test for whether we need to prompt. It'd be nice if we only prompt on a failure to overwrite, and not other things (like no file permissions) to avoid confusing behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

os.IsExist can't actually be used here; the error being returned doesn't unwrap to one that matches for that.

Instead I've added a custom error--ErrAlreadyExists--in internal/scaffold/scaffold.go, and added logic to check if the files already exist as we're copying them over and return that, which is then used as the key for running the prompt.

Currently running `scaffold init` a second time results in an error
message being propogated to the user as the cli output. This is
harmless, as the error regards the files being created already existing,
but not a great UX.

This adds promptui to run an interactive prompt for overwriting the
files. If the user selects to overwite, the command behaves as though it
were run with the `-force` option. If the user doesn't, it ends the
command but doesn't return an error.

Other related changes introduced by this change:

 * The cli app is wrapped in a new VervetApp struct, allowing config
   options specific to vervet but not to the CLI interface to be set--
   one of these options is a prompting interface.
 * The vervet app is added to the context passed into the cli app, so
   that vervet specific info can be used in the commands called by the
   cli app.

Fixes #56
Copy link
Contributor

@cmars cmars left a comment

Choose a reason for hiding this comment

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

Ship it!

@jcsackett jcsackett force-pushed the feat/prompt-when-scaffold-yaml-exists branch from 0589b6f to 8d20aec Compare November 12, 2021 17:16
@jcsackett jcsackett merged commit c7793e7 into main Nov 12, 2021
@jcsackett jcsackett deleted the feat/prompt-when-scaffold-yaml-exists branch November 12, 2021 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vervet scaffold init should display a more helpful message when a project is already initialized
3 participants