From ea96d4880eedd92d9c8228b84de028185ec941b2 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 15 Nov 2024 09:50:13 -0600 Subject: does apt_linux.go vs apt_windows.go work automagically? --- apt.go | 41 ++++++++++++++++++++++++++++++++++++++ apt_linux.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ dpkgQuery.go | 41 -------------------------------------- main.go | 25 ++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 41 deletions(-) create mode 100644 apt.go create mode 100644 apt_linux.go delete mode 100644 dpkgQuery.go diff --git a/apt.go b/apt.go new file mode 100644 index 0000000..7369d62 --- /dev/null +++ b/apt.go @@ -0,0 +1,41 @@ +package main + +import ( + "bufio" + "os/exec" + "strings" +) + +func getInstalledPackages() (map[string]string, error) { + // Run the dpkg-query command to list installed packages and versions + cmd := exec.Command("dpkg-query", "-W", "-f=${Package} ${Version}\n") + stdout, err := cmd.StdoutPipe() + if err != nil { + return nil, err + } + + // Start the command execution + if err := cmd.Start(); err != nil { + return nil, err + } + defer cmd.Wait() + + // Create a map to store package names and versions + installedPackages := make(map[string]string) + + // Use a scanner to read the output of the command line by line + scanner := bufio.NewScanner(stdout) + for scanner.Scan() { + line := scanner.Text() + // Split each line into package name and version + parts := strings.SplitN(line, " ", 2) + if len(parts) == 2 { + packageName := parts[0] + version := parts[1] + installedPackages[packageName] = version + } + } + + // Return the map with package names and versions + return installedPackages, scanner.Err() +} diff --git a/apt_linux.go b/apt_linux.go new file mode 100644 index 0000000..5d6eb16 --- /dev/null +++ b/apt_linux.go @@ -0,0 +1,64 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "os/exec" + "runtime" + "strings" +) + +// detectDistro returns the Linux distribution name (if possible) +func detectDistro() string { + // Check if we're on Linux + if runtime.GOOS != "linux" { + return "" + } + + // Try to read /etc/os-release to determine the distro + file, err := os.Open("/etc/os-release") + if err != nil { + return "" + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + if strings.HasPrefix(line, "ID=") { + parts := strings.SplitN(line, "=", 2) + if len(parts) == 2 { + return strings.Trim(parts[1], `"`) + } + } + } + return "" +} + +// getPackageList returns the list of installed packages based on the distro +func getPackageList(distro string) ([]string, error) { + var cmd *exec.Cmd + + // Run the appropriate command based on the detected distribution + switch distro { + case "ubuntu", "debian": + cmd = exec.Command("dpkg-query", "-W", "-f=${Package} ${Version}\n") + case "fedora", "centos", "rhel": + cmd = exec.Command("rpm", "-qa") + case "arch", "manjaro": + cmd = exec.Command("pacman", "-Q") + default: + return nil, fmt.Errorf("unsupported distribution: %s", distro) + } + + // Capture the command's output + output, err := cmd.CombinedOutput() + if err != nil { + return nil, fmt.Errorf("error running command: %v", err) + } + + // Split the output into lines and return + lines := strings.Split(string(output), "\n") + return lines, nil +} diff --git a/dpkgQuery.go b/dpkgQuery.go deleted file mode 100644 index 7369d62..0000000 --- a/dpkgQuery.go +++ /dev/null @@ -1,41 +0,0 @@ -package main - -import ( - "bufio" - "os/exec" - "strings" -) - -func getInstalledPackages() (map[string]string, error) { - // Run the dpkg-query command to list installed packages and versions - cmd := exec.Command("dpkg-query", "-W", "-f=${Package} ${Version}\n") - stdout, err := cmd.StdoutPipe() - if err != nil { - return nil, err - } - - // Start the command execution - if err := cmd.Start(); err != nil { - return nil, err - } - defer cmd.Wait() - - // Create a map to store package names and versions - installedPackages := make(map[string]string) - - // Use a scanner to read the output of the command line by line - scanner := bufio.NewScanner(stdout) - for scanner.Scan() { - line := scanner.Text() - // Split each line into package name and version - parts := strings.SplitN(line, " ", 2) - if len(parts) == 2 { - packageName := parts[0] - version := parts[1] - installedPackages[packageName] = version - } - } - - // Return the map with package names and versions - return installedPackages, scanner.Err() -} diff --git a/main.go b/main.go index ab7717c..3368042 100644 --- a/main.go +++ b/main.go @@ -55,6 +55,31 @@ func main() { for pkg, version := range installedPackages { fmt.Printf("%s: %s\n", pkg, version) } + + // Detect the Linux distribution + distro := detectDistro() + if distro == "" { + fmt.Println("Unable to detect Linux distribution.") + return + } + + fmt.Printf("Detected distribution: %s\n", distro) + + // Get the list of installed packages for the detected distro + packages, err := getPackageList(distro) + if err != nil { + fmt.Println("Error:", err) + return + } + + // Print the installed packages + fmt.Println("Installed Packages:") + for _, pkg := range packages { + if pkg != "" { + fmt.Println(pkg) + } + } + go NewWatchdog() startHTTP() -- cgit v1.2.3