summaryrefslogtreecommitdiff
path: root/pkg/packages.go
blob: fdce87c21ebdfee3deda168f4b8af5d0c2254177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package gonf

import "log/slog"

// ----- Globals ---------------------------------------------------------------
var packages []*PackagePromise

// packages management functions
var packages_install_function func([]string) (Status, []string)
var packages_update_function *CommandPromise

// ----- Init ------------------------------------------------------------------
func init() {
	packages = make([]*PackagePromise, 0)
}

// ----- Public ----------------------------------------------------------------
func SetPackagesConfiguration(install func([]string) (Status, []string), update *CommandPromise) {
	packages_install_function = install
	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 = append(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()
		}
	}
}

func (p PackagePromise) Status() Status {
	return p.status
}

// ----- 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
}