try macos build to see if it works

This commit is contained in:
Jeff Carr 2024-12-02 10:44:03 -06:00
parent 0be7da7cea
commit ba5c32d244
3 changed files with 73 additions and 94 deletions

37
apt.go
View File

@ -40,40 +40,3 @@ func (me *Machine) addNew(name string, version string) bool {
new1.Version = version new1.Version = version
return me.Packages.Append(new1) return me.Packages.Append(new1)
} }
/*
func (me *Machine) UpdatePackages() string {
// Get the list of installed packages for the detected distro
newP, err := getPackageList(me.Distro)
if err != nil {
fmt.Println("Error:", err)
return fmt.Sprintln("getPackageList()", err)
}
var newCounter, changeCounter int
// Print the installed packages and their versions
for pkg, version := range newP {
found := me.Packages.FindByName(pkg)
if found == nil {
log.Info("adding new", pkg, version)
me.addNew(pkg, version)
newCounter += 1
} else {
found.Version = version
panic("redo this. broken after autogenpb. was never right anyway")
//if me.Packages.Update(found) {
// changeCounter += 1
//}
}
}
footer := fmt.Sprintf("%s has distro %s with %d packages installed", me.Hostname, me.Distro, me.Packages.Len())
if changeCounter != 0 {
footer += fmt.Sprintf(" (%d changed)", changeCounter)
}
if newCounter != 0 {
footer += fmt.Sprintf(" (%d new)", newCounter)
}
return footer
}
*/

View File

@ -11,62 +11,6 @@ import (
// getPackageList returns the list of installed packages based on the distro // getPackageList returns the list of installed packages based on the distro
func getPackageList(distro string) (map[string]string, error) { func getPackageList(distro string) (map[string]string, error) {
var cmd *exec.Cmd log.Info("zoopb: have not done macos yet, skipping okay")
// 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 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()
}

72
apt_macos.go Normal file
View File

@ -0,0 +1,72 @@
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()
}