From 1c18f171c1bc9f6de5f7ce3d781e342ee85fe62c Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Mon, 2 Dec 2024 10:53:50 -0600 Subject: [PATCH] stub in windows and darwin files. golang is neat --- apt_darwin.go | 11 ++++++++ apt_linux.go | 58 +++++++++++++++++++++++++++++++++++++++- apt_macos.go | 72 -------------------------------------------------- apt_windows.go | 11 ++++++++ 4 files changed, 79 insertions(+), 73 deletions(-) create mode 100644 apt_darwin.go delete mode 100644 apt_macos.go create mode 100644 apt_windows.go diff --git a/apt_darwin.go b/apt_darwin.go new file mode 100644 index 0000000..0fc8e0f --- /dev/null +++ b/apt_darwin.go @@ -0,0 +1,11 @@ +package zoopb + +import ( + "go.wit.com/log" +) + +// getPackageList returns the list of installed packages based on the distro +func getPackageList(distro string) (map[string]string, error) { + log.Info("zoopb: have not done macos yet, skipping okay") + return nil, nil +} diff --git a/apt_linux.go b/apt_linux.go index feb54d1..3e71d1a 100644 --- a/apt_linux.go +++ b/apt_linux.go @@ -11,6 +11,62 @@ import ( // getPackageList returns the list of installed packages based on the distro func getPackageList(distro string) (map[string]string, error) { - log.Info("zoopb: have not done macos yet, skipping okay") + var cmd *exec.Cmd + + // Run the appropriate command based on the detected distribution + switch distro { + case "ubuntu", "debian": + return dpkgQuery() + 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) + } + + // todo: Split the output into lines and return + lines := strings.Split(string(output), "\n") + log.Info("output had", len(lines), "lines") return nil, nil } + +func dpkgQuery() (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_macos.go b/apt_macos.go deleted file mode 100644 index 3e71d1a..0000000 --- a/apt_macos.go +++ /dev/null @@ -1,72 +0,0 @@ -package zoopb - -import ( - "bufio" - "fmt" - "os/exec" - "strings" - - "go.wit.com/log" -) - -// getPackageList returns the list of installed packages based on the distro -func getPackageList(distro string) (map[string]string, error) { - var cmd *exec.Cmd - - // Run the appropriate command based on the detected distribution - switch distro { - case "ubuntu", "debian": - return dpkgQuery() - 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) - } - - // todo: Split the output into lines and return - lines := strings.Split(string(output), "\n") - log.Info("output had", len(lines), "lines") - return nil, nil -} - -func dpkgQuery() (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_windows.go b/apt_windows.go new file mode 100644 index 0000000..feec104 --- /dev/null +++ b/apt_windows.go @@ -0,0 +1,11 @@ +package zoopb + +import ( + "go.wit.com/log" +) + +// getPackageList returns the list of installed packages based on the distro +func getPackageList(distro string) (map[string]string, error) { + log.Info("zoopb: have not done windows yet, skipping okay") + return nil, nil +}