summaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/options/option.go21
-rw-r--r--api/resource.go4
2 files changed, 25 insertions, 0 deletions
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 {