From 756ae621bd97a8b776fefb15e5e7e2292f98ee45 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Thu, 9 Jul 2026 00:14:33 +0300 Subject: dependencies --- api/options/option.go | 21 +++++++++++++++++++++ api/resource.go | 4 ++++ 2 files changed, 25 insertions(+) (limited to 'api') diff --git a/api/options/option.go b/api/options/option.go index 1d9b931..22156a8 100644 --- a/api/options/option.go +++ b/api/options/option.go @@ -5,6 +5,8 @@ package options import ( "log" "os" + + "codeberg.org/snonux/gonf/internal/resource" ) // Option configures a resource. It is applied to the concrete resource value @@ -22,12 +24,31 @@ type ( Prunable interface{ SetPrune() } Absentable interface{ SetAbsent() } Latestable interface{ SetLatest() } + Dependable interface{ AddDependency(id string) } Linkable interface { SetSymlink(target string) SetHardlink(target string) } ) +// DependsOn declares that the resource being configured must be applied only +// after every given resource has been applied. Each argument may be a single +// resource or a Multi; a Multi is expanded so the dependency is recorded for +// each of its members individually. +func DependsOn(deps ...resource.Dependency) Option { + return func(t any) { + r, ok := t.(Dependable) + if !ok { + log.Fatalf("%T does not support DependsOn", t) + } + for _, dep := range deps { + for _, id := range dep.Dependencies() { + r.AddDependency(id) + } + } + } +} + // WithOwner sets the owning user of the resource. func WithOwner(owner string) Option { return func(t any) { diff --git a/api/resource.go b/api/resource.go index e254e1b..5c95211 100644 --- a/api/resource.go +++ b/api/resource.go @@ -10,6 +10,10 @@ import ( type Resource interface { ID() string String() string + // Dependencies returns the flattened resource IDs this value represents, + // so it can be passed to the DependsOn option. A single resource yields + // its own ID; a Multi yields the IDs of all its members. + Dependencies() []string } func Apply() error { -- cgit v1.2.3