From 00cb3a2488db11c8ce96d182915b8c448edce976 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 4 Sep 2024 09:11:35 +0200 Subject: chore(gonf): add a DECLARED promise status and make it the default instead of PROMISED --- pkg/commands.go | 7 +++++-- pkg/files.go | 9 ++++++--- pkg/packages.go | 7 +++++-- pkg/promises.go | 5 ++++- stdlib/backups/borg/client.go | 20 ++++++++++++++------ stdlib/backups/borg/server.go | 22 +++++++++++++++------- 6 files changed, 49 insertions(+), 21 deletions(-) diff --git a/pkg/commands.go b/pkg/commands.go index 4222de7..e3f6242 100644 --- a/pkg/commands.go +++ b/pkg/commands.go @@ -23,7 +23,7 @@ func CommandWithEnv(env []string, cmd string, args ...string) *CommandPromise { cmd: cmd, env: env, err: nil, - status: PROMISED, + status: DECLARED, } } @@ -44,7 +44,10 @@ func (c *CommandPromise) IfRepaired(p ...Promise) Promise { } func (c *CommandPromise) Promise() *CommandPromise { - commands = append(commands, c) + if c.status == DECLARED { + c.status = PROMISED + commands = append(commands, c) + } return c } diff --git a/pkg/files.go b/pkg/files.go index 6f22176..ac63718 100644 --- a/pkg/files.go +++ b/pkg/files.go @@ -45,7 +45,7 @@ func Directory(filename any) *FilePromise { filename: interfaceToTemplateValue(filename), fileType: DIRECTORY, permissions: nil, - status: PROMISED, + status: DECLARED, } } @@ -58,7 +58,7 @@ func File(filename any) *FilePromise { filename: interfaceToTemplateValue(filename), fileType: FILE, permissions: nil, - status: PROMISED, + status: DECLARED, } } @@ -88,7 +88,10 @@ func (f *FilePromise) IfRepaired(p ...Promise) Promise { } func (f *FilePromise) Promise() *FilePromise { - files = append(files, f) + if f.status == DECLARED { + f.status = PROMISED + files = append(files, f) + } return f } diff --git a/pkg/packages.go b/pkg/packages.go index 7ed8584..dea8f7c 100644 --- a/pkg/packages.go +++ b/pkg/packages.go @@ -19,7 +19,7 @@ func Package(names ...string) *PackagePromise { chain: nil, err: nil, names: names, - status: PROMISED, + status: DECLARED, } } @@ -36,7 +36,10 @@ func (p *PackagePromise) IfRepaired(ps ...Promise) Promise { } func (p *PackagePromise) Promise() *PackagePromise { - packages = append(packages, p) + if p.status == DECLARED { + p.status = PROMISED + packages = append(packages, p) + } return p } diff --git a/pkg/promises.go b/pkg/promises.go index 0198296..340f35d 100644 --- a/pkg/promises.go +++ b/pkg/promises.go @@ -9,7 +9,8 @@ type Promise interface { type Status int const ( - PROMISED = iota + DECLARED = iota + PROMISED BROKEN KEPT REPAIRED @@ -17,6 +18,8 @@ const ( func (s Status) String() string { switch s { + case DECLARED: + return "declared" case PROMISED: return "promised" case BROKEN: diff --git a/stdlib/backups/borg/client.go b/stdlib/backups/borg/client.go index a203ae2..95d8d0d 100644 --- a/stdlib/backups/borg/client.go +++ b/stdlib/backups/borg/client.go @@ -33,13 +33,18 @@ type BorgClient struct { status gonf.Status } +var borgClient *BorgClient = nil + func (b *BorgClient) IfRepaired(p ...gonf.Promise) gonf.Promise { b.chain = append(b.chain, p...) return b } func (b *BorgClient) Promise() *BorgClient { - gonf.MakeCustomPromise(b).Promise() + if b.status == gonf.DECLARED { + b.status = gonf.PROMISED + gonf.MakeCustomPromise(b).Promise() + } return b } @@ -101,12 +106,15 @@ func (b BorgClient) Status() gonf.Status { } func Client() *BorgClient { - return &BorgClient{ - chain: nil, - jobs: make(map[string]*Job), - path: "/etc/borg/", - status: gonf.PROMISED, + if borgClient == nil { + borgClient = &BorgClient{ + chain: nil, + jobs: make(map[string]*Job), + path: "/etc/borg/", + status: gonf.DECLARED, + } } + return borgClient } func (b *BorgClient) Add(name string, path string, privateKey []byte, hostname string) *BorgClient { diff --git a/stdlib/backups/borg/server.go b/stdlib/backups/borg/server.go index 4fe0e74..29794d8 100644 --- a/stdlib/backups/borg/server.go +++ b/stdlib/backups/borg/server.go @@ -15,13 +15,18 @@ type BorgServer struct { status gonf.Status } +var borgServer *BorgServer = nil + func (b *BorgServer) IfRepaired(p ...gonf.Promise) gonf.Promise { b.chain = append(b.chain, p...) return b } func (b *BorgServer) Promise() *BorgServer { - gonf.MakeCustomPromise(b).Promise() + if b.status == gonf.DECLARED { + b.status = gonf.PROMISED + gonf.MakeCustomPromise(b).Promise() + } return b } @@ -80,13 +85,16 @@ func (b BorgServer) Status() gonf.Status { } func Server() *BorgServer { - return &BorgServer{ - chain: nil, - clients: make(map[string][]byte), - path: "/srv/borg/", - user: "borg", - status: gonf.PROMISED, + if borgServer == nil { + borgServer = &BorgServer{ + chain: nil, + clients: make(map[string][]byte), + path: "/srv/borg/", + user: "borg", + status: gonf.DECLARED, + } } + return borgServer } func (b *BorgServer) Add(name string, publicKey []byte) *BorgServer { -- cgit v1.2.3