// Package detect decides whether an application is already present on the // machine, independently of which package manager (if any) put it there. package detect import ( "context" "os/exec" "path/filepath" "runtime" "strconv" "strings" "time" "git.starwarslab.us/ATOM_IT/Deployr/internal/catalog" "git.starwarslab.us/ATOM_IT/Deployr/internal/platform" "git.starwarslab.us/ATOM_IT/Deployr/internal/run" ) // Entry is one Windows uninstall-registry record. type Entry struct { DisplayName string DisplayVersion string Publisher string UninstallString string QuietUninstallString string } // Installed evaluates the detect rules and reports presence plus, when it can // be determined cheaply, the installed version. func Installed(d catalog.Detect) (bool, string) { if d.Empty() { return false, "" } if len(d.PrinterDrivers) > 0 && runtime.GOOS == "windows" { if driver, ok := FindPrinterDriver(d.PrinterDrivers); ok { // The version lives inside the name for most printer drivers, so // report the name rather than a bare major version number. return true, driverVersion(driver) } } if len(d.Registry) > 0 && runtime.GOOS == "windows" { if entry, ok := FindRegistry(d.Registry); ok { return true, entry.DisplayVersion } } for _, p := range d.Paths { if platform.PathExists(p) { return true, bundleVersion(p) } } for _, c := range d.Commands { if run.Available(c) { return true, "" } } return false, "" } // driverVersion pulls the version out of a printer driver name, which is where // vendors put it -- "HP Universal Printing PCL 6 (v7.1.0)" yields "v7.1.0". // Falls back to the spooler's major version when the name carries none. func driverVersion(d PrinterDriver) string { if open := strings.LastIndex(d.Name, "("); open >= 0 { if close := strings.LastIndex(d.Name, ")"); close > open { if inner := strings.TrimSpace(d.Name[open+1 : close]); inner != "" { return inner } } } if d.MajorVersion > 0 { return "v" + strconv.Itoa(d.MajorVersion) } return "" } // bundleVersion reads CFBundleShortVersionString from a macOS .app bundle. // Everything else has no cheap, universal version probe, so we report none // rather than guessing. func bundleVersion(path string) string { if runtime.GOOS != "darwin" { return "" } path = platform.ExpandPath(path) if !strings.HasSuffix(path, ".app") { return "" } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() out, err := exec.CommandContext(ctx, "/usr/bin/defaults", "read", filepath.Join(path, "Contents", "Info"), "CFBundleShortVersionString").Output() if err != nil { return "" } return strings.TrimSpace(string(out)) } // FindRegistry returns the first Windows uninstall entry whose DisplayName // matches one of the patterns. Matching is a case-insensitive prefix test, so // "Google Chrome" matches "Google Chrome" and "Google Chrome (64-bit)" alike. func FindRegistry(patterns []string) (Entry, bool) { if runtime.GOOS != "windows" || len(patterns) == 0 { return Entry{}, false } entries := RegistryEntries() for _, pattern := range patterns { p := strings.ToLower(strings.TrimSpace(pattern)) if p == "" { continue } for _, e := range entries { name := strings.ToLower(e.DisplayName) if strings.HasPrefix(name, p) || strings.Contains(name, p) { return e, true } } } return Entry{}, false }