Skip to content

Commit

Permalink
🐛 rework windows.computerInfo to not go over size limit (#4802)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev authored Nov 1, 2024
1 parent a042ce2 commit 4557897
Show file tree
Hide file tree
Showing 2 changed files with 223 additions and 192 deletions.
24 changes: 20 additions & 4 deletions providers/os/resources/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,39 @@ func (s *mqlWindows) computerInfo() (map[string]interface{}, error) {

// If the exit code is not 0, then we got an error and we should read stderr for details
if executedCmd.ExitStatus != 0 {
// First attempt to use the short computer info command
encodedCmd := powershell.Encode(windows.PSGetComputerInfoShort)
executedCmd, err = conn.RunCommand(encodedCmd)
stderr, err := io.ReadAll(executedCmd.Stderr)
if err != nil {
return nil, err
}
return nil, errors.New("failed to retrieve computer info: " + string(stderr))
}

parsedInfo, err := windows.ParseComputerInfo(executedCmd.Stdout)
if err != nil {
return nil, err
}

// If we have no error but OsProductType is nil, we need to run a custom command to get the info
// For reference, see https://github.com/mondoohq/cnquery/pull/4520
if parsedInfo["OsProductType"] == nil {
executedCmd, err := conn.RunCommand(powershell.Encode(windows.PSGetComputerInfoCustom))
if err != nil {
return nil, err
}
if executedCmd.ExitStatus != 0 {
stderr, err := io.ReadAll(executedCmd.Stderr)
if err != nil {
return nil, err
}
return nil, errors.New("failed to retrieve computer info: " + string(stderr))
}
parsedInfo, err = windows.ParseCustomComputerInfo(executedCmd.Stdout)
if err != nil {
return nil, err
}
}

return windows.ParseComputerInfo(executedCmd.Stdout)
return parsedInfo, nil
}

func (wh *mqlWindowsHotfix) id() (string, error) {
Expand Down
Loading

0 comments on commit 4557897

Please sign in to comment.