diff options
| author | Paul Buetow <paul@buetow.org> | 2026-07-08 23:17:39 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-07-08 23:17:39 +0300 |
| commit | 3f8b79f2b1384e3abc2b1bb2fe913688c7ec251c (patch) | |
| tree | 5cff57875a1f5b63536423c0b7fe7a3788ff15ac /api/api.go | |
| parent | 920f2ea88c45e972cd87b56580c90a0277b7130e (diff) | |
add multi which can return multiple resources
Diffstat (limited to 'api/api.go')
| -rw-r--r-- | api/api.go | 119 |
1 files changed, 95 insertions, 24 deletions
@@ -2,48 +2,119 @@ package api import ( "codeberg.org/snonux/gonf/api/options" + "codeberg.org/snonux/gonf/internal/resource" "codeberg.org/snonux/gonf/internal/resource/dir" "codeberg.org/snonux/gonf/internal/resource/file" "codeberg.org/snonux/gonf/internal/resource/link" "codeberg.org/snonux/gonf/internal/resource/pkg" ) -// File creates a file resource. -func File(path string, opts ...options.Option) Resource { - return file.Present(path, opts...) +// Path constraint for resources that can be defined as a single item or a list. +type Path interface { + string | []string } -// NoFile creates a file resource that is ensured to be absent. -func NoFile(path string, opts ...options.Option) Resource { - return file.Absent(path, opts...) +// File creates one or more file resources. +func File[T Path](path T, opts ...options.Option) Resource { + switch v := any(path).(type) { + case string: + return file.Present(v, opts...) + case []string: + return Files(v, opts...) + default: + panic("File: path must be string or []string") + } } -// Dir creates a directory resource. -func Dir(path string, opts ...options.Option) Resource { - return dir.Present(path, opts...) +func Files(paths []string, opts ...options.Option) Resource { + var resources []resource.Resource + for _, path := range paths { + resources = append(resources, file.Present(path, opts...)) + } + return resource.Multi(resources) } -// NoDir creates a directory resource that is ensured to be absent. -func NoDir(path string, opts ...options.Option) Resource { - return dir.Absent(path, opts...) +// NoFile creates one or more file resources that are ensured to be absent. +func NoFile[T Path](path T, opts ...options.Option) Resource { + return File(path, append(opts, options.IsAbsent)...) } -// Link creates a link resource (symbolic or hard). -func Link(path string, opts ...options.Option) Resource { - return link.Present(path, opts...) +// Dir creates one or more directory resources. +func Dir[T Path](path T, opts ...options.Option) Resource { + switch v := any(path).(type) { + case string: + return dir.Present(v, opts...) + case []string: + return Dirs(v, opts...) + default: + panic("Dir: path must be string or []string") + } } -// NoLink creates a link resource that is ensured to be absent. -func NoLink(path string, opts ...options.Option) Resource { - return link.Absent(path, opts...) +func Dirs(paths []string, opts ...options.Option) Resource { + var resources []resource.Resource + for _, path := range paths { + resources = append(resources, dir.Present(path, opts...)) + } + return resource.Multi(resources) } -// Package creates a package resource. -func Package(name string, opts ...options.Option) Resource { - return pkg.Present(name, opts...) +// NoDir creates one or more directory resources that are ensured to be absent. +func NoDir[T Path](path T, opts ...options.Option) Resource { + return Dir(path, append(opts, options.IsAbsent)...) } -// NoPackage creates a package resource that is ensured to be absent. -func NoPackage(name string, opts ...options.Option) Resource { - return pkg.Absent(name, opts...) +// Link creates one or more link resources (symbolic or hard). +func Link[T Path](path T, opts ...options.Option) Resource { + switch v := any(path).(type) { + case string: + return link.Present(v, opts...) + case []string: + return Links(v, opts...) + default: + panic("Link: path must be string or []string") + } +} + +func Links(paths []string, opts ...options.Option) Resource { + var resources []resource.Resource + for _, path := range paths { + resources = append(resources, link.Present(path, opts...)) + } + return resource.Multi(resources) +} + +// NoLink creates one or more link resources that are ensured to be absent. +func NoLink[T Path](path T, opts ...options.Option) Resource { + return Link(path, append(opts, options.IsAbsent)...) +} + +// Elems is a helper to create a slice of strings from variadic arguments. +func Elems(paths ...string) []string { + return paths +} + +// Package creates one or more package resources. +func Package[T Path](name T, opts ...options.Option) Resource { + switch v := any(name).(type) { + case string: + return pkg.Present(v, opts...) + case []string: + return Packages(v, opts...) + default: + panic("Package: name must be string or []string") + } +} + +func Packages(names []string, opts ...options.Option) Resource { + var resources []resource.Resource + for _, name := range names { + resources = append(resources, pkg.Present(name, opts...)) + } + return resource.Multi(resources) +} + +// NoPackage creates one or more package resources that are ensured to be absent. +func NoPackage[T Path](name T, opts ...options.Option) Resource { + return Package(name, append(opts, options.IsAbsent)...) } |
