diff options
author | Julien Dessaux | 2024-03-07 00:54:35 +0100 |
---|---|---|
committer | Julien Dessaux | 2024-03-07 01:02:23 +0100 |
commit | 560becfd32dd7355547938f3c6229060dd395aab (patch) | |
tree | 42329f4e47c64349ae8a0205f867632ac52bcdc6 /pkg/packages.go | |
parent | feat(stdlib): began adding systemd services support (diff) | |
download | gonf-560becfd32dd7355547938f3c6229060dd395aab.tar.gz gonf-560becfd32dd7355547938f3c6229060dd395aab.tar.bz2 gonf-560becfd32dd7355547938f3c6229060dd395aab.zip |
chore(repo): renamed gonf subfolder to something more traditional in go land
Diffstat (limited to 'pkg/packages.go')
-rw-r--r-- | pkg/packages.go | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/pkg/packages.go b/pkg/packages.go new file mode 100644 index 0000000..114a227 --- /dev/null +++ b/pkg/packages.go @@ -0,0 +1,81 @@ +package gonf + +import "log/slog" + +// ----- Globals --------------------------------------------------------------- +var packages []*PackagePromise + +// packages management functions +var packages_install_function func([]string) (Status, []string) +var packages_list_function func() +var packages_update_function *CommandPromise + +// ----- Init ------------------------------------------------------------------ +func init() { + packages = make([]*PackagePromise, 0) +} + +// ----- Public ---------------------------------------------------------------- +func SetPackagesConfiguration(install func([]string) (Status, []string), list func(), update *CommandPromise) { + packages_install_function = install + packages_list_function = list + packages_update_function = update +} + +func Package(names ...string) *PackagePromise { + return &PackagePromise{ + chain: nil, + err: nil, + names: names, + status: PROMISED, + } +} + +type PackagePromise struct { + chain []Promise + err error + names []string + status Status +} + +func (p *PackagePromise) IfRepaired(ps ...Promise) Promise { + p.chain = ps + return p +} + +func (p *PackagePromise) Promise() Promise { + packages = append(packages, p) + return p +} + +func (p *PackagePromise) Resolve() { + status, affected := packages_install_function(p.names) + 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 { + pp.Resolve() + } + } +} + +// ----- Internal -------------------------------------------------------------- +func resolvePackages() (status Status) { + status = KEPT + for _, c := range packages { + if c.status == PROMISED { + c.Resolve() + switch c.status { + case BROKEN: + return BROKEN + case REPAIRED: + status = REPAIRED + } + } + } + return +} |