diff options
Diffstat (limited to 'internal/resource')
| -rw-r--r-- | internal/resource/dir/dir.go | 6 | ||||
| -rw-r--r-- | internal/resource/file/file.go | 20 | ||||
| -rw-r--r-- | internal/resource/link/link.go | 14 | ||||
| -rw-r--r-- | internal/resource/resource.go | 14 |
4 files changed, 36 insertions, 18 deletions
diff --git a/internal/resource/dir/dir.go b/internal/resource/dir/dir.go index 8be4395..047a591 100644 --- a/internal/resource/dir/dir.go +++ b/internal/resource/dir/dir.go @@ -12,6 +12,7 @@ import ( ) type Dir struct { + resource resource.Resource path string source string user string @@ -192,13 +193,14 @@ func Have(path string, opts ...opt.Option) resource.Resource { log.Fatalf("failed to apply directory resource %s: %v", path, err) } - res := resource.Register("Directory", d.path) + d.resource = resource.Register("Directory", d.path, + resource.ApplierFunc(func() error { return d.apply() })) if err := d.apply(); err != nil { log.Fatalf("failed to apply directory resource %s: %v", path, err) } - return res + return d.resource } func Absent(path string, opts ...opt.Option) resource.Resource { diff --git a/internal/resource/file/file.go b/internal/resource/file/file.go index 6395403..5a2453e 100644 --- a/internal/resource/file/file.go +++ b/internal/resource/file/file.go @@ -15,13 +15,14 @@ import ( ) type File struct { - path string - content string - source string // bare path, no "source://" prefix - user string - group string - mode os.FileMode - absent bool + resource resource.Resource + path string + content string + source string // bare path, no "source://" prefix + user string + group string + mode os.FileMode + absent bool } // SetContent implements opt.Contented. Setting literal content clears any @@ -223,13 +224,14 @@ func Have(path string, opts ...opt.Option) resource.Resource { log.Fatalf("failed to apply file resource %s: %v", path, err) } - res := resource.Register("File", f.targetPath()) + f.resource = resource.Register("File", f.targetPath(), + resource.ApplierFunc(func() error { return f.apply() })) if err := f.apply(); err != nil { log.Fatalf("failed to apply file resource %s: %v", path, err) } - return res + return f.resource } func Absent(path string, opts ...opt.Option) resource.Resource { diff --git a/internal/resource/link/link.go b/internal/resource/link/link.go index f9c6c8d..5f8e345 100644 --- a/internal/resource/link/link.go +++ b/internal/resource/link/link.go @@ -18,10 +18,11 @@ const ( ) type Link struct { - path string - target string - kind kind - absent bool + resource resource.Resource + path string + target string + kind kind + absent bool } // SetSymlink implements opt.Linkable. @@ -87,13 +88,14 @@ func Ensure(path string, opts ...opt.Option) error { func Have(path string, opts ...opt.Option) resource.Resource { l := build(path, opts...) - res := resource.Register(l.resourceType(), l.path) + l.resource = resource.Register(l.resourceType(), l.path, + resource.ApplierFunc(func() error { return l.apply() })) if err := l.apply(); err != nil { log.Fatalf("failed to apply link resource %s: %v", path, err) } - return res + return l.resource } func Absent(path string, opts ...opt.Option) resource.Resource { diff --git a/internal/resource/resource.go b/internal/resource/resource.go index f7951c3..4e72485 100644 --- a/internal/resource/resource.go +++ b/internal/resource/resource.go @@ -5,16 +5,28 @@ import ( "log" ) +type Applier interface { + Apply() error +} + +type ApplierFunc func() error + +func (f ApplierFunc) Apply() error { + return f() +} + type Resource struct { Type string Name string + Apply Applier dependsOn map[string]struct{} } -func Register(type_, name string) Resource { +func Register(type_, name string, apply Applier) Resource { r := Resource{ Type: type_, Name: name, + Apply: apply, dependsOn: make(map[string]struct{}), } |
