chore(promises): make a Status function part of the Promise interface

This commit is contained in:
Julien Dessaux 2024-03-23 14:50:37 +01:00
parent 4d3e266b07
commit eb9d5f861a
Signed by: adyxax
GPG key ID: F92E51B86E07177E
7 changed files with 27 additions and 15 deletions

View file

@ -26,7 +26,7 @@ func CommandWithEnv(env []string, cmd string, args ...string) *CommandPromise {
cmd: cmd,
env: env,
err: nil,
Status: PROMISED,
status: PROMISED,
}
}
@ -36,7 +36,7 @@ type CommandPromise struct {
cmd string
env []string
err error
Status Status
status Status
Stdout bytes.Buffer
Stderr bytes.Buffer
}
@ -60,16 +60,16 @@ func (c *CommandPromise) Resolve() {
cmd.Stderr = &c.Stderr
if c.err = cmd.Run(); c.err != nil {
c.Status = BROKEN
c.status = BROKEN
slog.Error("command", "args", c.args, "cmd", c.cmd, "env", c.env, "err", c.err, "stdout", c.Stdout.String(), "stderr", c.Stderr.String(), "status", c.Status)
return
}
if c.Stdout.Len() == 0 && c.Stderr.Len() > 0 {
c.Status = BROKEN
c.status = BROKEN
slog.Error("command", "args", c.args, "cmd", c.cmd, "env", c.env, "stdout", c.Stdout.String(), "stderr", c.Stderr.String(), "status", c.Status)
return
}
c.Status = REPAIRED
c.status = REPAIRED
slog.Info("command", "args", c.args, "cmd", c.cmd, "env", c.env, "stderr", c.Stderr.String(), "status", c.Status)
// TODO add a notion of repaired?
for _, p := range c.chain {
@ -77,13 +77,17 @@ func (c *CommandPromise) Resolve() {
}
}
func (c CommandPromise) Status() Status {
return c.status
}
// ----- Internal --------------------------------------------------------------
func resolveCommands() (status Status) {
status = KEPT
for _, c := range commands {
if c.Status == PROMISED {
if c.status == PROMISED {
c.Resolve()
switch c.Status {
switch c.status {
case BROKEN:
return BROKEN
case REPAIRED:

View file

@ -6,16 +6,11 @@ func init() {
customPromises = make([]*CustomPromise, 0)
}
type CustomPromiseInterface interface {
Promise
Status() Status
}
type CustomPromise struct {
promise CustomPromiseInterface
promise Promise
}
func MakeCustomPromise(p CustomPromiseInterface) *CustomPromise {
func MakeCustomPromise(p Promise) *CustomPromise {
return &CustomPromise{
promise: p,
}

View file

@ -123,6 +123,10 @@ func (f *FilePromise) Resolve() {
}
}
func (f FilePromise) Status() Status {
return f.status
}
// ----- Internal --------------------------------------------------------------
func resolveFiles() (status Status) {
status = KEPT

View file

@ -63,6 +63,10 @@ func (p *PackagePromise) Resolve() {
}
}
func (p PackagePromise) Status() Status {
return p.status
}
// ----- Internal --------------------------------------------------------------
func resolvePackages() (status Status) {
status = KEPT

View file

@ -4,6 +4,7 @@ type Promise interface {
IfRepaired(...Promise) Promise
Promise() Promise
Resolve()
Status() Status
}
//type Operation int

View file

@ -78,6 +78,10 @@ func (s *ServicePromise) Resolve() {
}
}
func (s ServicePromise) Status() Status {
return s.status
}
// ----- Internal --------------------------------------------------------------
func resolveServices() (status Status) {
status = KEPT

View file

@ -58,7 +58,7 @@ func packages_install(names []string) (gonf.Status, []string) {
cmd := gonf.CommandWithEnv([]string{"DEBIAN_FRONTEND=noninteractive", "LC_ALL=C"}, "apt-get", args...)
cmd.Resolve()
packages_list()
return cmd.Status, names
return cmd.Status(), names
}
func packages_list() {