fix packagemanager
This commit is contained in:
		
							parent
							
								
									d8ed6f1abf
								
							
						
					
					
						commit
						fb3a4af3c9
					
				| @ -4,7 +4,7 @@ import ( | ||||
| 	"os/exec" | ||||
| ) | ||||
| 
 | ||||
| // PackageManager is an abstraction over distro package tooling.
 | ||||
| // PackageManager abstracts distro package handling.
 | ||||
| type PackageManager interface { | ||||
| 	Distro() string | ||||
| 	InstallPackages(pkgs ...string) error | ||||
| @ -16,9 +16,12 @@ type PackageManager interface { | ||||
| /* ------------------------------------------------------------------ */ | ||||
| 
 | ||||
| type aptManager struct { | ||||
| 	logf func(string, ...any) | ||||
| 	logf    func(string, ...any) // logger from App
 | ||||
| 	updated bool                 // true after first apt-update
 | ||||
| } | ||||
| 
 | ||||
| // newPackageManager is called once in app.go; the same instance is
 | ||||
| // passed to every collector, so apt-update can be executed exactly once.
 | ||||
| func newPackageManager(logf func(string, ...any)) PackageManager { | ||||
| 	return &aptManager{logf: logf} | ||||
| } | ||||
| @ -31,11 +34,27 @@ func (a *aptManager) ExecExists(bin string) bool { | ||||
| } | ||||
| 
 | ||||
| func (a *aptManager) InstallPackages(pkgs ...string) error { | ||||
| 	args := append([]string{"-y", "install"}, pkgs...) | ||||
| 	if len(pkgs) == 0 { | ||||
| 		return nil | ||||
| 	} | ||||
| 
 | ||||
| 	// refresh package index only the first time we install anything
 | ||||
| 	if !a.updated { | ||||
| 		a.logf("apt update") | ||||
| 		if err := exec.Command( | ||||
| 			"apt-get", "-qq", "-y", "update", | ||||
| 		).Run(); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		a.updated = true | ||||
| 	} | ||||
| 
 | ||||
| 	args := append( | ||||
| 		[]string{"-qq", "-y", "--no-install-recommends", "install"}, | ||||
| 		pkgs..., | ||||
| 	) | ||||
| 	cmd := exec.Command("apt-get", args...) | ||||
| 	cmd.Env = append(cmd.Env, "DEBIAN_FRONTEND=noninteractive") | ||||
| 	cmd.Stdout = nil | ||||
| 	cmd.Stderr = nil | ||||
| 	a.logf("apt install %v", pkgs) | ||||
| 	return cmd.Run() | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user