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

feat: allow global host commands to be run from anywhere, for #4416 #5702

Conversation

GuySartorelli
Copy link
Collaborator

@GuySartorelli GuySartorelli commented Jan 17, 2024

The Issue

Custom global host commands currently only be run inside a project directory - even if they don't need a project in order to do their stuff.

A great example of this is ddev self-upgrade which should be able to run from anywhere - but currently it has to be run from inside a project directory.

How This PR Solves The Issue

This PR adds a new CanRunGlobally annotation. If custom global host commands set this annotation to true, they will be added to the cobra command set, and can be run from anywhere, even outside project directories.

Manual Testing Instructions

  1. Run ddev help - you should see the self-upgrade command there.
  2. Run ddev self-upgrade - it should work from anywhere, even outside a project directory.
  3. Add ## CanRunGlobally: true to a custom global host command, then perform the first two steps again, but with your custom command rather than self-upgrade.

Automated Testing Overview

I've added checks for the following the following:

  • Check a global host command with CanRunGlobally: true shows and can be run from arbitrary working directories
  • Check a global host command with CanRunGlobally: true still shows and can be run from inside a project directory
  • Check commands without CanRunGlobally: true do not show from arbitrary working directories

This is my first time really looking at how to add tests and I've gone in with a fairly light touch - if you want more test coverage I'll be happy to add it.

Related Issue Link(s)

NOTE: This PR will be one of several iterative features that together will fulfil the requirements set forth in that issue. The issue should not be closed until all of its requirements are fulfilled.

Release/Deployment Notes

Anyone who maintains a DDEV extension or who has some custom commands in projects they manage should review their custom global host commands, and add the new CanRunGlobally annotation to any commands that should be allowed to run even outside of project directories.

Copy link

github-actions bot commented Jan 17, 2024

@GuySartorelli GuySartorelli force-pushed the 20240116_GuySartorelli_truly-global-commands branch 2 times, most recently from 48fe5a7 to 7304e4a Compare January 17, 2024 10:00
Comment on lines +40 to +46
if runtime.GOOS == "windows" {
windowsBashPath := util.FindBashPath()
if windowsBashPath == "" {
fmt.Println("Unable to find bash.exe in PATH, not loading custom commands")
return nil
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was further down, inside a loop for some reason - but it just needs to be run once and should be run early to avoid unnecessary processing

Comment on lines +98 to +99
// addCustomCommandsFromDir adds the custom commands from inside a given directory
func addCustomCommandsFromDir(rootCmd *cobra.Command, app *ddevapp.DdevApp, serviceDirOnHost string, commandFiles []string, isGlobalSet bool, commandsAdded map[string]int) error {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Most of the code in this function used to live directly in addCustomCommands() - but we need to be able to run it with different input depending on whether we're in a project directory or not and the easiest way to handle that seems to be refactoring this into its own function.

Comment on lines +305 to +307
} else {
_ = os.Setenv("DDEV_PROJECT_STATUS", "")
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure if we need this or not? I figured it's safer to have it and not need it, rather than not have it and find out later we do need it.

@GuySartorelli
Copy link
Collaborator Author

Docs CI failure seems to be for an unrelated broken link

@stasadev
Copy link
Member

Wow!

The rules for PR titles say that I must use fixes #4416 in the PR title because an issue exists

You can use for #4416 instead of fixes #4416 .

Docs CI failure seems to be for an unrelated broken link

Restarted failed jobs.

@GuySartorelli GuySartorelli changed the title feat: allow global host commands to be run from anywhere, fixes #4416 feat: allow global host commands to be run from anywhere, for #4416 Jan 17, 2024
@GuySartorelli GuySartorelli force-pushed the 20240116_GuySartorelli_truly-global-commands branch from 7304e4a to feaedb5 Compare January 20, 2024 07:13
@GuySartorelli GuySartorelli marked this pull request as ready for review January 20, 2024 07:15
@GuySartorelli
Copy link
Collaborator Author

I've added some tests and updated the "Automated Testing Overview" section of the PR description.
Let me know if you want anything changed, more test coverage, etc.

@GuySartorelli
Copy link
Collaborator Author

I tried to test this on gitpod using https://gitpod.io/#https://github.com/ddev/ddev/pulls/5702 but when I ran ddev -v it showed that it was actually running on commit 9bd9054 (i.e. the latest commit on the master branch)... so I'm not sure if I was doing something wrong there, or if something is misconfigured, or something?

Anyway, that was more trying that out for fun - I did run this locally and it worked as expected.

@rfay
Copy link
Member

rfay commented Jan 20, 2024

I just opened on gitpod and got

$ ddev --version
ddev version feaedb50

which is correct. I used the "open" button provided by the gitpod app,
image

It links to https://gitpod.io/?autostart=true#https://github.com/ddev/ddev/pull/5702 which should be the same as your result.

Would be interested to know what might have gone wrong for you. Gitpod is a great resource for these things.

I am interested to note that gitpod no longer adds a comment with the link. That was a nice feature.

I tried it with your link, https://gitpod.io/#https://github.com/ddev/ddev/pulls/5702 and sure enough, the code in /workspace/ddev was upstream/master instead of the PR.

Mine https://gitpod.io/?autostart=true#https://github.com/ddev/ddev/pull/5702 checks out the branch properly:

gitpod /workspace/ddev (20240116_GuySartorelli_truly-global-commands) $ git status
On branch 20240116_GuySartorelli_truly-global-commands
Your branch is up to date with 'origin/20240116_GuySartorelli_truly-global-commands'.

Yours, https://gitpod.io/#https://github.com/ddev/ddev/pulls/5702,

gitpod /workspace/ddev (master) $ git status
On branch master
Your branch is up to date with 'origin/master'.

I have to assume this is a new bug in gitpod? Or perhaps it's just

which never actually got fixed?

@rfay
Copy link
Member

rfay commented Jan 20, 2024

Ah, the problem is your URL had pulls in it, not pull. (And that's because the doc says pulls). (It works fine with pull). I'll do docs PR.

rfay added a commit that referenced this pull request Jan 20, 2024
Per #5702 (comment) we had a typo in the URL link for a PR, this fixes it
@rfay rfay mentioned this pull request Jan 20, 2024
@GuySartorelli GuySartorelli force-pushed the 20240116_GuySartorelli_truly-global-commands branch from feaedb5 to 7ed5340 Compare January 21, 2024 00:51
@GuySartorelli
Copy link
Collaborator Author

GuySartorelli commented Jan 21, 2024

I've renamed the RunsWithoutProject annotation to CanRunGlobally.

This is a more immediately clear and better descriptive name for what this annotation does, and leaves its usage open to the behaviour described in #4416 (comment) in the future.

Also ran this with the correct devpod URL this time (thanks Randy) and it works as expected.

@GuySartorelli
Copy link
Collaborator Author

GuySartorelli commented Jan 22, 2024

Buildkite ci failure looks unrelated

@rfay
Copy link
Member

rfay commented Jan 22, 2024

I invited you to buildkite so you can restart tests (and more easily examine them), also to ddev team on GitHub. I think that lets you restart GitHub tests.

@GuySartorelli
Copy link
Collaborator Author

Awesome, thank you! 💙

@GuySartorelli
Copy link
Collaborator Author

This should be good to go

@stasadev
Copy link
Member

I will review this later this week.

Copy link
Member

@stasadev stasadev left a comment

Choose a reason for hiding this comment

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

This is a great addition to DDEV!

Works as expected, thank you @GuySartorelli.

@stasadev stasadev merged commit 82925f2 into ddev:master Jan 26, 2024
21 checks passed
@GuySartorelli GuySartorelli deleted the 20240116_GuySartorelli_truly-global-commands branch January 26, 2024 21:20
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.

None yet

3 participants