From eb9d5f861a7b46a1827c2caa6abbfb8d77e29ffb Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 23 Mar 2024 14:50:37 +0100 Subject: chore(promises): make a Status function part of the Promise interface --- pkg/commands.go | 18 +++++++++++------- pkg/custom.go | 9 ++------- pkg/files.go | 4 ++++ pkg/packages.go | 4 ++++ pkg/promises.go | 1 + pkg/services.go | 4 ++++ stdlib/os/debian/apt.go | 2 +- 7 files changed, 27 insertions(+), 15 deletions(-) diff --git a/pkg/commands.go b/pkg/commands.go index b5a11ca..df42919 100644 --- a/pkg/commands.go +++ b/pkg/commands.go @@ -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: diff --git a/pkg/custom.go b/pkg/custom.go index 9ed2bca..acca226 100644 --- a/pkg/custom.go +++ b/pkg/custom.go @@ -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, } diff --git a/pkg/files.go b/pkg/files.go index 379b437..366bacc 100644 --- a/pkg/files.go +++ b/pkg/files.go @@ -123,6 +123,10 @@ func (f *FilePromise) Resolve() { } } +func (f FilePromise) Status() Status { + return f.status +} + // ----- Internal -------------------------------------------------------------- func resolveFiles() (status Status) { status = KEPT diff --git a/pkg/packages.go b/pkg/packages.go index 114a227..20d3e3d 100644 --- a/pkg/packages.go +++ b/pkg/packages.go @@ -63,6 +63,10 @@ func (p *PackagePromise) Resolve() { } } +func (p PackagePromise) Status() Status { + return p.status +} + // ----- Internal -------------------------------------------------------------- func resolvePackages() (status Status) { status = KEPT diff --git a/pkg/promises.go b/pkg/promises.go index 326d500..8495650 100644 --- a/pkg/promises.go +++ b/pkg/promises.go @@ -4,6 +4,7 @@ type Promise interface { IfRepaired(...Promise) Promise Promise() Promise Resolve() + Status() Status } //type Operation int diff --git a/pkg/services.go b/pkg/services.go index 58c7383..658cd0e 100644 --- a/pkg/services.go +++ b/pkg/services.go @@ -78,6 +78,10 @@ func (s *ServicePromise) Resolve() { } } +func (s ServicePromise) Status() Status { + return s.status +} + // ----- Internal -------------------------------------------------------------- func resolveServices() (status Status) { status = KEPT diff --git a/stdlib/os/debian/apt.go b/stdlib/os/debian/apt.go index aad8b65..5b0b54f 100644 --- a/stdlib/os/debian/apt.go +++ b/stdlib/os/debian/apt.go @@ -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() { -- cgit v1.2.3