Improved error message for remote version package output
This commit is contained in:
@ -109,7 +109,7 @@ func (a *AptManager) SetAuthCommand(authCommand string) {
|
||||
}
|
||||
|
||||
// Parse parses the apt-cache policy output to extract Installed and Candidate versions.
|
||||
func (a *AptManager) ParseRemotePackageManagerVersionOutput(output string) ([]packagemanagercommon.Package, []error) {
|
||||
func (a *AptManager) ParseRemotePackageManagerVersionOutput(output string) ([]packagemanagercommon.Package, error) {
|
||||
var (
|
||||
packageName string
|
||||
installedString string
|
||||
@ -118,11 +118,12 @@ func (a *AptManager) ParseRemotePackageManagerVersionOutput(output string) ([]pa
|
||||
)
|
||||
// Check for error message in the output
|
||||
if strings.Contains(output, "Unable to locate package") {
|
||||
return nil, []error{fmt.Errorf("error: %s", strings.TrimSpace(output))}
|
||||
return nil, fmt.Errorf("error: %s", strings.TrimSpace(output))
|
||||
}
|
||||
packages := []packagemanagercommon.Package{}
|
||||
outputBuf := bytes.NewBufferString(output)
|
||||
outputScan := bufio.NewScanner(outputBuf)
|
||||
var packageCount uint
|
||||
for outputScan.Scan() {
|
||||
line := outputScan.Text()
|
||||
if !strings.HasPrefix(line, " ") && strings.HasSuffix(line, ":") {
|
||||
@ -141,17 +142,24 @@ func (a *AptManager) ParseRemotePackageManagerVersionOutput(output string) ([]pa
|
||||
|
||||
if countRelevantLines == 2 {
|
||||
countRelevantLines = 0
|
||||
packages = append(packages, packagemanagercommon.Package{
|
||||
Name: packageName,
|
||||
VersionCheck: packagemanagercommon.PackageVersion{
|
||||
Installed: strings.TrimSpace(installedString),
|
||||
Candidate: strings.TrimSpace(candidateString),
|
||||
Match: installedString == candidateString,
|
||||
}},
|
||||
)
|
||||
if !strings.Contains(installedString, " (none)") {
|
||||
packageCount++
|
||||
packages = append(packages, packagemanagercommon.Package{
|
||||
Name: packageName,
|
||||
VersionCheck: packagemanagercommon.PackageVersion{
|
||||
Installed: strings.TrimSpace(installedString),
|
||||
Candidate: strings.TrimSpace(candidateString),
|
||||
Match: installedString == candidateString,
|
||||
}},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if packageCount == 0 {
|
||||
return nil, fmt.Errorf("no packages found")
|
||||
}
|
||||
|
||||
return packages, nil
|
||||
}
|
||||
|
||||
|
@ -92,11 +92,11 @@ func (d *DnfManager) CheckVersion(pkgs []packagemanagercommon.Package) (string,
|
||||
}
|
||||
|
||||
// Parse parses the dnf info output to extract Installed and Candidate versions.
|
||||
func (d DnfManager) ParseRemotePackageManagerVersionOutput(output string) ([]packagemanagercommon.Package, []error) {
|
||||
func (d DnfManager) ParseRemotePackageManagerVersionOutput(output string) ([]packagemanagercommon.Package, error) {
|
||||
|
||||
// Check for error message in the output
|
||||
if strings.Contains(output, "No matching packages to list") {
|
||||
return nil, []error{fmt.Errorf("error: package not listed")}
|
||||
return nil, fmt.Errorf("error: package not listed")
|
||||
}
|
||||
|
||||
// Define regular expressions to capture installed and available versions
|
||||
@ -118,7 +118,7 @@ func (d DnfManager) ParseRemotePackageManagerVersionOutput(output string) ([]pac
|
||||
}
|
||||
|
||||
if installedVersion == "" && candidateVersion == "" {
|
||||
return nil, []error{fmt.Errorf("failed to parse versions from dnf output")}
|
||||
return nil, fmt.Errorf("failed to parse versions from dnf output")
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
|
@ -16,7 +16,7 @@ type PackageManager interface {
|
||||
Upgrade(pkgs []packagemanagercommon.Package) (string, []string) // Upgrade a specific package
|
||||
UpgradeAll() (string, []string)
|
||||
CheckVersion(pkgs []packagemanagercommon.Package) (string, []string)
|
||||
ParseRemotePackageManagerVersionOutput(output string) ([]packagemanagercommon.Package, []error)
|
||||
ParseRemotePackageManagerVersionOutput(output string) ([]packagemanagercommon.Package, error)
|
||||
// Configure applies functional options to customize the package manager.
|
||||
Configure(options ...packagemanagercommon.PackageManagerOption)
|
||||
}
|
||||
|
@ -90,11 +90,11 @@ func (y *YumManager) CheckVersion(pkgs []packagemanagercommon.Package) (string,
|
||||
}
|
||||
|
||||
// Parse parses the dnf info output to extract Installed and Candidate versions.
|
||||
func (y YumManager) ParseRemotePackageManagerVersionOutput(output string) ([]packagemanagercommon.Package, []error) {
|
||||
func (y YumManager) ParseRemotePackageManagerVersionOutput(output string) ([]packagemanagercommon.Package, error) {
|
||||
|
||||
// Check for error message in the output
|
||||
if strings.Contains(output, "No matching packages to list") {
|
||||
return nil, []error{fmt.Errorf("error: package not listed")}
|
||||
return nil, fmt.Errorf("error: package not listed")
|
||||
}
|
||||
|
||||
// Define regular expressions to capture installed and available versions
|
||||
@ -116,7 +116,7 @@ func (y YumManager) ParseRemotePackageManagerVersionOutput(output string) ([]pac
|
||||
}
|
||||
|
||||
if installedVersion == "" && candidateVersion == "" {
|
||||
return nil, []error{fmt.Errorf("failed to parse versions from dnf output")}
|
||||
return nil, fmt.Errorf("failed to parse versions from dnf output")
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
|
Reference in New Issue
Block a user