Skip to content

feat: install add-on for unknown command if available, fixes #6352 - #6353

Closed
stasadev wants to merge 5 commits into
ddev:masterfrom
stasadev:20240626_stasadev_install_addon
Closed

stasadev wants to merge 5 commits into
ddev:masterfrom
stasadev:20240626_stasadev_install_addon

Conversation

@stasadev

@stasadev stasadev commented Jun 26, 2024 •

Copy link
Copy Markdown
Member

The Issue

How This PR Solves The Issue

Tries to find a matching official add-on for unknown command and installs it.

Manual Testing Instructions

Try things from ddev get --list in the DDEV project folder:

  • ddev phpmyadmin
  • ddev redis
  • ddev adminer
  • ddev redis-7

After installing the add-on, repeat the same command, i.e. install the Redis add-on using ddev redis and then run ddev redis again to check.

Automated Testing Overview

Release/Deployment Notes

@github-actions github-actions Bot added dependencies Pull requests that update a dependency file enhancement labels Jun 26, 2024
@stasadev stasadev mentioned this pull request Jun 26, 2024
1 task done
@github-actions

github-actions Bot commented Jun 26, 2024 •

Copy link
Copy Markdown

@nlighteneddesign

Copy link
Copy Markdown
Contributor

Just want to make sure this is allow listing only official addons otherwise this can be a security concern. The advantage of the one off approach was that each one gets an intentional addon.

@stasadev

Copy link
Copy Markdown
Member Author

Yes, official add-ons only, I updated the PR description.

@rfay

rfay commented Jun 26, 2024

Copy link
Copy Markdown
Member

How would this map custom commands to add-ons? It wasn't clear to me in a quick read. For phpmyadmin the custom command happens to be phpmyadmin, but just looking through ddev get --list I'm not sure there are many actual hits like this.

@stasadev

Copy link
Copy Markdown
Member Author

It doesn't search by commands, that add-ons might have, it looks at the add-on name. It's just a simpler approach.

The name of the phpMyAdmin repo is ddev-phpmyadmin, which partially matches with phpmyadmin (using the library suggested in #6352 (comment)).

Comment thread cmd/ddev/cmd/root.go
suggestAddonForUnknownCommand(os.Args[1])
}

if err := RootCmd.Execute(); err != nil {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should it perhaps try to execute first, and only do the fuzzy search if there's an error? Or is this just an unnecessary micro-optimization?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's because RootCmd.Execute() throws some error message that I can't prevent from being shown without intercepting it, which would make the code more complex.

$ ddev foobar
Error: unknown command "foobar" for "ddev"
Run 'ddev --help' for usage.

The above happens before the code goes inside the if you're asking about.

@nickchomey

nickchomey commented Jun 26, 2024 •

Copy link
Copy Markdown
Collaborator

Could it perhaps do a search of all addons, but add some indication when a result is not an official addon? Or perhaps this is something that could be user-configured, either globally or per-project? People might like the discovery-aspect of something like this.

Or, alternatively, perhaps a separate fuzzy search could be implemented to do a manual search within ddev get --list or ddev get --list --all. Particularly when you include --all, it can be difficult to visually scan and parse them all, so a filtered and ranked result list would be helpful.

I tried Aqua out the other day, and it allows you to instantly search and install from its 1000+ binaries. https://aquaproj.github.io/. Very nice.

In fact, i could see it being very useful to integrate into DDEV, so as to be able to easily install dependencies (I just wasted a lot of time writing bash scripts to install cloudflared (for cf tunnels) and flarectl (for managing dns records for the tunnels). Likewise I needed to do some workaround for yq not being available outside the containers. So, having the ability to install binaries into ~/.ddev/bin would have been exceptionally useful.

I'm happy to open two separate issues (1. fuzzy search ddev get -list -all and 2. install binaries via aqua) and elaborate on this if there is interest.

@tyler36

tyler36 commented Jun 27, 2024

Copy link
Copy Markdown
Collaborator

Thank you for working on this @stasadev
Some thoughts:

  1. I support limiting scope to official addons only. See @nlighteneddesign comment above.

  2. I tried browsersync

    $ ddev browsersync                   
    Do you want to install ddev/ddev-browsersync add-on? [Y/n] (yes): n
    Exiting, no permission given to install
    

    I think it would be better to include a "unknown command" warning to let people know why we are offering to install an addon.

  3. "Exiting, no permission given to install" I think this needs re-wording before release. I'm assuming its placeholder text while in draft.

@tyler36

tyler36 commented Jun 27, 2024

Copy link
Copy Markdown
Collaborator

Tried:

✅ ddev browsersync - offers to install
✅ ddev xhgui - offers to install
✅ ddev cypress - does NOT offer to install, as expected beacuse its unofficial
✅ ddev cron - offers to install
✅ ddev storybook - does NOT offer to install, as expected beacuse its unofficial

Comment thread cmd/ddev/cmd/root.go Resolved
@stasadev

Copy link
Copy Markdown
Member Author

@nickchomey

Could it perhaps do a search of all addons, but add some indication when a result is not an official addon? Or perhaps this is something that could be user-configured, either globally or per-project?

I think we can start with official for now, see how it goes, and maybe expand it later.

I'm happy to open two separate issues (1. fuzzy search ddev get --list --all and 2. install binaries via aqua) and elaborate on this if there is interest.

Yes, these are good ideas, I'm not sure about the second one with aqua (since it adds another dependency for DDEV), but you can open new issues to discuss them.

@tyler36

I think it would be better to include a "unknown command" warning

Yeah, I thought about that too, I'll add something for that. (Not a normal DDEV warning, as it contains the Run 'ddev --help' for usage. line, which we don't need here.)

I should also add:

Executing command [ddev get addon-name]

I'm assuming its placeholder text while in draft.

That's correct.

@stasadev

Copy link
Copy Markdown
Member Author

One side effect I noticed is that when you type ddev ddev, it offers to install one of the official add-ons.

Should it be here (this can be considered a feature)?

@nickchomey

Copy link
Copy Markdown
Collaborator

Maybe you can exclude the common ddev- prefix in each name?

@stasadev

Copy link
Copy Markdown
Member Author

Maybe you can exclude the common ddev- prefix in each name?

Yes, I'll remove ddev- from the search, as the logic is not obvious.

@tyler36

tyler36 commented Jul 2, 2024

Copy link
Copy Markdown
Collaborator

Ha. After playing with docker-compose up/down commands for the last hour, I switch context to a DDEV project and this happened:

$ ddev down
Do you want to install ddev/ddev-browsersync add-on? [Y/n] (yes): 

@stasadev

stasadev commented Jul 2, 2024

Copy link
Copy Markdown
Member Author

I guess we don't need a fuzzy search with such confusing results, Ddev-brOWsersyNc, but a partial match would be fine.

@stasadev
stasadev force-pushed the 20240626_stasadev_install_addon branch from f3bca85 to bc26fa2 Compare July 3, 2024 21:50
@github-actions github-actions Bot removed the dependencies Pull requests that update a dependency file label Jul 3, 2024
@stasadev

stasadev commented Jul 3, 2024

Copy link
Copy Markdown
Member Author

I refactored the logic:

  1. It won't ask to install the add-on if it's already installed.
  2. Added more comments to explain what's going on.
  3. I decided to only look for a full match, e.g. foo would try to install ddev/ddev-foo, because a partial match can be confusing, e.g. ddev red or ddev admin should not suggest installing ddev/ddev-redis or ddev/ddev-phpmyadmin.
  4. Added the original error from Cobra to the output.

@stasadev
stasadev force-pushed the 20240626_stasadev_install_addon branch from bc26fa2 to dc81f5d Compare July 4, 2024 18:17
@stasadev
stasadev marked this pull request as ready for review July 4, 2024 20:50
@stasadev
stasadev requested a review from a team as a code owner July 4, 2024 20:50
@nickchomey

Copy link
Copy Markdown
Collaborator

i think the utility of this would be quite minimal without fuzzy search. There's all sorts of things that could be done to address the issue you found.

some ideas:

@stasadev

stasadev commented Jul 5, 2024

Copy link
Copy Markdown
Member Author

@nickchomey, thanks for the suggestion, but after much thought, I wouldn't add it.

at the very least you could do some sort of wildcard match such that a partial word like redi matches redis

I don't think it's necessary, on a large scale it only adds complexity for small benefits.

For example, ddev phpm will prompt you to install ddev/ddev-phpmyadmin, but users can simply make a mistake, and if we have a partial match, we lose the useful "Did you mean this?" suggestion that we have:

$ ddev phpm
Error: unknown command "phpm" for "ddev"

Did you mean this?
        npm
        php

Run 'ddev --help' for usage.

Of course, this applies to all the commands we have and adds even more confusion.

there's other packages that offer a very nice UX

This is completely customizable on the user side. If a user needs fzf, they should make it work for the entire shell to get 100% benefit. Personally, I use this plugin https://github.com/Aloxaf/fzf-tab for Zsh.

And we have an open issue about UI:

@nlighteneddesign

Copy link
Copy Markdown
Contributor

I'm wondering if my minimal approach has more utility in the end.

The approach I have does require someone adding the list of commands for each addon, but you know for sure which addon is being requested for a given command because it is defined. The approach I had works even when the common command does not match the addon name which this approach does not cover.

This still gives me a nagging concern for security, especially if fuzzy searching gets added back in. It just makes it easier for an unintended addon to be added.

There is more work to be done to match all of the current official addons with my approach, but it is finite and you can make adding the global command part of the adoption process it becomes more maintainable. Also my PR covers the most popular addon at the moment so it's a good test if it is something the community will like. I worry this approach will accidentally request installs of things more often and become annoying.

If I mistype a command now it just says unrecognized, where this approach increases the likely hood I have to answer a prompt instead of just fixing my typo.

There is also a clean MR to reference to add or modify these install commands if you need to enhance or modify a specific one whereas I do not see a hook to modify behavior in this option.

@rfay

rfay commented Jul 5, 2024

Copy link
Copy Markdown
Member

A related experiment was abandoned because of unpredictable results. Here we were trying to let people automatically run any command that existed inside the web container:

@stasadev

stasadev commented Jul 5, 2024

Copy link
Copy Markdown
Member Author

I wasn't aware about that experiment.

After reading it, I see that I'm trying to get it to work for an unknown scope, and it's undocumented.

It works fine, but maybe it should be in the scope of ddev-get.

Something like ddev get --search foobar - where we can implement fuzzy search easily.

@nlighteneddesign

Copy link
Copy Markdown
Contributor

That definitely scope it, but it defeats the purpose of the original PR.

I think both could work. The ddev get --search foobar could help find a given addon and install it when you're trying to install one but don't remember the exact name, then the PR I wrote would still intercept the local command when the addon does not exist and install the phpmyadmin addon for you.

So you have a more convenient install command and a more convenient fail state for running an addon command when the addon does not exist.

@stasadev

stasadev commented Jul 8, 2024

Copy link
Copy Markdown
Member Author

suggest addon if command not found

This is probably too fragile, and if we have it, it could have bad consequences when implementing commands in the future.


ddev get --search foobar

We already have a lot of flags for ddev get and increasing them won't do us any favors because we already have some "search" with grep:

ddev get --list | grep phpmyadmin
ddev get --list --all | grep cypress

The idea presented in the PR for the phpmyadmin global command is quite flexible, and users can create their own global commands to install their favorite add-ons (or even create an add-on that will have these global commands).


Perhaps a better option that we should focus on is an Add-on Registry where people can find what they need.

Closing this in favor of:

@tyler36, if you agree, please also close:

@stasadev stasadev closed this Jul 8, 2024
@stasadev
stasadev deleted the 20240626_stasadev_install_addon branch July 8, 2024 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants