diff --git a/utils/osdetail/binmeta.go b/utils/osdetail/binmeta.go index 175aea2..d377075 100644 --- a/utils/osdetail/binmeta.go +++ b/utils/osdetail/binmeta.go @@ -7,9 +7,10 @@ import ( ) var ( - segmentsSplitter = regexp.MustCompile("[^A-Za-z0-9]*[A-Z]?[a-z0-9]*") - nameOnly = regexp.MustCompile("^[A-Za-z0-9]+$") - delimiters = regexp.MustCompile("^[^A-Za-z0-9]+") + segmentsSplitter = regexp.MustCompile("[^A-Za-z0-9]*[A-Z]?[a-z0-9]*") + nameOnly = regexp.MustCompile("^[A-Za-z0-9]+$") + delimitersAtStart = regexp.MustCompile("^[^A-Za-z0-9]+") + delimitersOnly = regexp.MustCompile("^[^A-Za-z0-9]+$") ) // GenerateBinaryNameFromPath generates a more human readable binary name from @@ -52,7 +53,7 @@ func GenerateBinaryNameFromPath(path string) string { // Post-process name parts for i := range nameParts { // Remove any leading delimiters. - nameParts[i] = delimiters.ReplaceAllString(nameParts[i], "") + nameParts[i] = delimitersAtStart.ReplaceAllString(nameParts[i], "") // Title-case name-only parts. if nameOnly.MatchString(nameParts[i]) { @@ -63,7 +64,9 @@ func GenerateBinaryNameFromPath(path string) string { return strings.Join(nameParts, " ") } -func cleanFileDescription(fields []string) string { +func cleanFileDescription(fileDescr string) string { + fields := strings.Fields(fileDescr) + // If there is a 1 or 2 character delimiter field, only use fields before it. endIndex := len(fields) for i, field := range fields { @@ -82,5 +85,10 @@ func cleanFileDescription(fields []string) string { binName = strings.SplitN(binName, ". ", 2)[0] } - return binName + // If does not have any characters or numbers, return an empty string. + if delimitersOnly.MatchString(binName) { + return "" + } + + return strings.TrimSpace(binName) } diff --git a/utils/osdetail/binmeta_test.go b/utils/osdetail/binmeta_test.go index 3cb50d4..41f32c7 100644 --- a/utils/osdetail/binmeta_test.go +++ b/utils/osdetail/binmeta_test.go @@ -1,7 +1,6 @@ package osdetail import ( - "strings" "testing" "github.com/stretchr/testify/assert" @@ -21,15 +20,19 @@ func TestGenerateBinaryNameFromPath(t *testing.T) { } func TestCleanFileDescription(t *testing.T) { - assert.Equal(t, "Product Name", cleanFileDescription(strings.Fields("Product Name. Does this and that."))) - assert.Equal(t, "Product Name", cleanFileDescription(strings.Fields("Product Name - Does this and that."))) - assert.Equal(t, "Product Name", cleanFileDescription(strings.Fields("Product Name / Does this and that."))) - assert.Equal(t, "Product Name", cleanFileDescription(strings.Fields("Product Name :: Does this and that."))) - assert.Equal(t, "/ Product Name", cleanFileDescription(strings.Fields("/ Product Name"))) - assert.Equal(t, "Product", cleanFileDescription(strings.Fields("Product / Name"))) + assert.Equal(t, "Product Name", cleanFileDescription("Product Name. Does this and that.")) + assert.Equal(t, "Product Name", cleanFileDescription("Product Name - Does this and that.")) + assert.Equal(t, "Product Name", cleanFileDescription("Product Name / Does this and that.")) + assert.Equal(t, "Product Name", cleanFileDescription("Product Name :: Does this and that.")) + assert.Equal(t, "/ Product Name", cleanFileDescription("/ Product Name")) + assert.Equal(t, "Product", cleanFileDescription("Product / Name")) + assert.Equal(t, "", cleanFileDescription(". / Name")) + assert.Equal(t, "", cleanFileDescription(". ")) + assert.Equal(t, "", cleanFileDescription(".")) + assert.Equal(t, "N/A", cleanFileDescription("N/A")) assert.Equal(t, "Product Name a Does this and that.", - cleanFileDescription(strings.Fields("Product Name a Does this and that.")), + cleanFileDescription("Product Name a Does this and that."), ) } diff --git a/utils/osdetail/binmeta_windows.go b/utils/osdetail/binmeta_windows.go index 01650f4..9683bf0 100644 --- a/utils/osdetail/binmeta_windows.go +++ b/utils/osdetail/binmeta_windows.go @@ -1,13 +1,10 @@ package osdetail import ( - "bufio" - "bytes" "fmt" - "strings" ) -const powershellGetFileDescription = `Get-ItemProperty %q | Format-List` +const powershellGetFileDescription = `Get-ItemProperty %q | Select -ExpandProperty VersionInfo | Select -ExpandProperty FileDescription` // GetBinaryNameFromSystem queries the operating system for a human readable // name for the given binary path. @@ -18,25 +15,10 @@ func GetBinaryNameFromSystem(path string) (string, error) { return "", fmt.Errorf("failed to get file properties of %s: %s", path, err) } - // Create scanner for the output. - scanner := bufio.NewScanner(bytes.NewBufferString(output)) - scanner.Split(bufio.ScanLines) - - // Search for the FileDescription line. - for scanner.Scan() { - // Split line up into fields. - fields := strings.Fields(scanner.Text()) - // Discard lines with less than two fields. - if len(fields) < 2 { - continue - } - // Skip all lines that we aren't looking for. - if fields[0] != "FileDescription:" { - continue - } - - // Clean and return. - return cleanFileDescription(fields[1:]), nil + // Clean name. + binName := cleanFileDescription(output) + if binName != "" { + return binName, nil } // Generate a default name as default. diff --git a/utils/osdetail/test/main_windows.go b/utils/osdetail/test/main_windows.go index 1d46950..77928bd 100644 --- a/utils/osdetail/test/main_windows.go +++ b/utils/osdetail/test/main_windows.go @@ -30,7 +30,7 @@ func main() { } func printBinaryName(name, path string) { - binName, err := osdetail.GetBinaryName(path) + binName, err := osdetail.GetBinaryNameFromSystem(path) if err != nil { fmt.Printf("%s: ERROR: %s\n", name, err) } else { @@ -39,7 +39,7 @@ func printBinaryName(name, path string) { } func printBinaryIcon(name, path string) { - binIcon, err := osdetail.GetBinaryIcon(path) + binIcon, err := osdetail.GetBinaryIconFromSystem(path) if err != nil { fmt.Printf("%s: ERROR: %s\n", name, err) } else {