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

refactor (shell) : detect shell on windows using gopsutil too (#4588) #4591

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rohanKanojia
Copy link
Contributor

@rohanKanojia rohanKanojia commented Feb 3, 2025

Description

As a follow up to #4572, updating shell detection logic on windows to also rely on gopsutil library to detect the shell name by inspecting parent processes of crc.exe process.

Fixes: #4588

Relates to: PR #4572

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change
  • Chore (non-breaking change which doesn't affect codebase;
    test, version modification, documentation, etc.)

Proposed changes

This PR mostly includes moving some methods so that they become accessible in windows environments too.

  • Move shell detection using gopsutil logic from shell_unix.go -> shell.go so that it can be reused in shell_windows.go as well
  • Remove shell_linux.go/ shell_darwin.go as they only contained a single constant that I moved to shell_unix.go
  • Remove getProcessEntry and related methods from shell_windows.go as we're reusing gopsutil approach that is working well on Windows

Testing

  1. Start CRC in windows environment
  2. Verify crc oc-env is working as expected in
    • CMD
    • PowerShell
    • GitBash
    • WSL
      • bash
      • fish
      • zsh

Contribution Checklist

  • I Keep It Small and Simple: The smaller the PR is, the easier it is to review and have it merged
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Which platform have you tested the code changes on?
    • Linux
    • Windows
    • MacOS

Copy link

openshift-ci bot commented Feb 3, 2025

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

pkg/os/util.go Outdated
@@ -127,3 +127,12 @@ func RemoveFileGlob(glob string) error {
}
return nil
}

func IsPresentInList(arr []string, str string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

It already exists as Contains

Copy link
Contributor Author

@rohanKanojia rohanKanojia Feb 3, 2025

Choose a reason for hiding this comment

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

Thanks, I was going to use this but I noticed a unit test is failing on Windows due to these changes. This line was returning bash on Windows powershells:

userShell, err := shell.GetShell("")

On checking the process tree during unit test execution, it shows processes like this:
cmd.test.exe -> go.exe -> bash.exe

I've added another utility method in order to just match the prefix instead of matching entire string.

if err != nil {
return ""
}
if crcos.IsPresentInList(supportedShell, processName) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This would be easier to review if it did not mix code movement with functional changes. I'd suggest having a commit where this code is moved to shell.go without any changes, and then a commit porting the Windows code to use detectShellByCheckingProcessTree with these crcos.IsPresentInList and such.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I've split the PR into two commits with first commit containing code reorganization and second including actual change

@rohanKanojia rohanKanojia force-pushed the pr/issue4588 branch 3 times, most recently from 1d18663 to 9e42792 Compare February 3, 2025 16:43
@rohanKanojia rohanKanojia marked this pull request as ready for review February 4, 2025 09:41
@openshift-ci openshift-ci bot requested review from evidolob and gbraad February 4, 2025 09:42
pkg/os/shell/shell.go Show resolved Hide resolved
})
}

func IsPresentInListSatisfying(input []string, toMatch string, matchingPredicate func(string, string) bool) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

While looking for naming ideas in go standard packages (usual name for such a function is ContainsFunc), I realized the slices package has both Contains and ContainsFunc, we can use these instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, thanks a lot! I'm sorry I should've checked this.

Copy link
Contributor

@cfergeau cfergeau left a comment

Choose a reason for hiding this comment

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

There's an outdated + Add a utility method in strings for verifying a slice contains an element matching the provided predicate. in the commit log of the second commit, otherwise looks good to me!

@rohanKanojia
Copy link
Contributor Author

@cfergeau : Thanks for noticing! I've updated the commit message.

I'll also create a follow up PR to make use of slice.Contains to replace the equivalent method in crcstrings.Contains

…ows refactor

Related to crc-org#4588

Move shell detection related methods from shell_unix.go to shell.go so
that they can be accessed from Windows code as well

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
+ Update `shell_windows.go` to use detectShellByCheckingProcessTree
instead of relying on SHELL environment variable.
+ Remove hardcoded check from detectShellByCheckingProcessTree for shell
  types, use already present supportedShell slice.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
@openshift-ci openshift-ci bot added the lgtm label Feb 5, 2025
Copy link

openshift-ci bot commented Feb 5, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cfergeau

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link

openshift-ci bot commented Feb 5, 2025

@rohanKanojia: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/security 8398309 link false /test security
ci/prow/e2e-crc 8398309 link true /test e2e-crc

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

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.

Refactor Shell detection on windows in order to use gopsutil
2 participants