feat(packages): finished implementing packages install promises

This commit is contained in:
Julien Dessaux 2024-02-28 00:53:57 +01:00
parent b71ff8278c
commit 24c4bde14a
Signed by: adyxax
GPG key ID: F92E51B86E07177E
3 changed files with 32 additions and 14 deletions

View file

@ -1,10 +1,12 @@
package gonf package gonf
import "log/slog"
// ----- Globals --------------------------------------------------------------- // ----- Globals ---------------------------------------------------------------
var packages []*PackagePromise var packages []*PackagePromise
// packages management functions // packages management functions
var packages_install_function func([]string) Status var packages_install_function func([]string) (Status, []string)
var packages_list_function func() var packages_list_function func()
var packages_update_function *CommandPromise var packages_update_function *CommandPromise
@ -14,7 +16,7 @@ func init() {
} }
// ----- Public ---------------------------------------------------------------- // ----- Public ----------------------------------------------------------------
func SetPackagesConfiguration(install func([]string) Status, list func(), update *CommandPromise) { func SetPackagesConfiguration(install func([]string) (Status, []string), list func(), update *CommandPromise) {
packages_install_function = install packages_install_function = install
packages_list_function = list packages_list_function = list
packages_update_function = update packages_update_function = update
@ -47,8 +49,14 @@ func (p *PackagePromise) Promise() Promise {
} }
func (p *PackagePromise) Resolve() { func (p *PackagePromise) Resolve() {
status := packages_install_function(p.names) status, affected := packages_install_function(p.names)
if status == REPAIRED { switch status {
case BROKEN:
slog.Error("package", "names", p.names, "status", status, "broke", affected)
case KEPT:
slog.Debug("package", "names", p.names, "status", status)
case REPAIRED:
slog.Info("package", "names", p.names, "status", status, "repaired", affected)
for _, pp := range p.chain { for _, pp := range p.chain {
pp.Resolve() pp.Resolve()
} }

View file

@ -9,6 +9,17 @@ var builtinTemplateFunctions = map[string]any{
"var": getVariable, "var": getVariable,
} }
func FilterSlice[T any](slice *[]T, predicate func(T) bool) {
i := 0
for _, element := range *slice {
if predicate(element) { // if the element matches the predicate function
(*slice)[i] = element // then we keep it in the slice
i++
} // otherwise the element will get overwritten
}
*slice = (*slice)[:i] // or truncated out of the slice
}
func sha256sum(contents []byte) []byte { func sha256sum(contents []byte) []byte {
h := sha256.New() h := sha256.New()
h.Write(contents) h.Write(contents)

View file

@ -7,6 +7,7 @@ import (
"log/slog" "log/slog"
"os" "os"
"os/exec" "os/exec"
"strings"
"git.adyxax.org/adyxax/gonf/v2/gonf" "git.adyxax.org/adyxax/gonf/v2/gonf"
"git.adyxax.org/adyxax/gonf/v2/stdlib/os/systemd" "git.adyxax.org/adyxax/gonf/v2/stdlib/os/systemd"
@ -32,21 +33,19 @@ func Promise() {
systemd.Promise() systemd.Promise()
} }
func packages_install(names []string) gonf.Status { func packages_install(names []string) (gonf.Status, []string) {
allKept := true gonf.FilterSlice(&names, func(n string) bool {
for _, n := range names { _, ok := packages[n]
if _, ok := packages[n]; !ok { return !ok
allKept = false })
} if len(names) == 0 {
} return gonf.KEPT, nil
if allKept {
return gonf.KEPT
} }
args := append([]string{"install", "-y", "--no-install-recommends"}, names...) args := append([]string{"install", "-y", "--no-install-recommends"}, names...)
cmd := gonf.CommandWithEnv([]string{"DEBIAN_FRONTEND=noninteractive", "LC_ALL=C"}, "apt-get", args...) cmd := gonf.CommandWithEnv([]string{"DEBIAN_FRONTEND=noninteractive", "LC_ALL=C"}, "apt-get", args...)
cmd.Resolve() cmd.Resolve()
packages_list() packages_list()
return cmd.Status return cmd.Status, names
} }
func packages_list() { func packages_list() {