diff options
| author | Paul Buetow <paul@buetow.org> | 2026-07-05 10:28:26 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-07-05 10:28:26 +0300 |
| commit | 7fdcb5ed7639508e05c9a3851704cf1de9f4476a (patch) | |
| tree | 816f26ea806f5656469216e3c592d059cb2f7027 /internal/resource/file | |
| parent | fc0ce6f1918c4603254091d9fe77785996b56411 (diff) | |
make options pattern more generic with dot imports a bit less boilerplate for the user
Diffstat (limited to 'internal/resource/file')
| -rw-r--r-- | internal/resource/file/file.go | 65 | ||||
| -rw-r--r-- | internal/resource/file/file_test.go | 2 |
2 files changed, 28 insertions, 39 deletions
diff --git a/internal/resource/file/file.go b/internal/resource/file/file.go index 257c407..6395403 100644 --- a/internal/resource/file/file.go +++ b/internal/resource/file/file.go @@ -11,6 +11,7 @@ import ( "text/template" "codeberg.org/snonux/gonf/internal/resource" + "codeberg.org/snonux/gonf/internal/resource/opt" ) type File struct { @@ -23,47 +24,33 @@ type File struct { absent bool } -type Option func(*File) - -func WithContent(content string) Option { - return func(f *File) { - f.content = content - f.source = "" - } +// SetContent implements opt.Contented. Setting literal content clears any +// previously configured source, as the two are mutually exclusive. +func (f *File) SetContent(content string) { + f.content = content + f.source = "" } -func WithSource(source string) Option { - return func(f *File) { - f.source = source - f.content = "" - } +// SetSource implements opt.Sourced. Setting a source clears any previously +// configured literal content, as the two are mutually exclusive. +func (f *File) SetSource(source string) { + f.source = source + f.content = "" } -func WithUser(user string) Option { - return func(f *File) { - f.user = user - } -} +// SetOwner implements opt.Owner. +func (f *File) SetOwner(user string) { f.user = user } -func WithGroup(group string) Option { - return func(f *File) { - f.group = group - } -} +// SetGroup implements opt.Grouped. +func (f *File) SetGroup(group string) { f.group = group } -func WithMode(mode os.FileMode) Option { - return func(f *File) { - f.mode = mode - } -} +// SetMode implements opt.Moded. +func (f *File) SetMode(mode os.FileMode) { f.mode = mode } -func IsAbsent() Option { - return func(f *File) { - f.absent = true - } -} +// SetAbsent implements opt.Absentable. +func (f *File) SetAbsent() { f.absent = true } -func build(path string, opts ...Option) (*File, error) { +func build(path string, opts ...opt.Option) (*File, error) { curr, err := user.Current() if err != nil { return nil, fmt.Errorf("failed to get current user for default: %w", err) @@ -76,8 +63,8 @@ func build(path string, opts ...Option) (*File, error) { group: curr.Gid, } - for _, opt := range opts { - opt(f) + for _, o := range opts { + o(f) } return f, nil @@ -222,7 +209,7 @@ func ensureAbsent(path string) error { // Ensure builds and applies the file resource described by opts, without // registering it. Used by other resource packages (e.g. dir) to write an // individual file without it becoming its own top-level resource. -func Ensure(path string, opts ...Option) error { +func Ensure(path string, opts ...opt.Option) error { f, err := build(path, opts...) if err != nil { return err @@ -230,7 +217,7 @@ func Ensure(path string, opts ...Option) error { return f.apply() } -func Have(path string, opts ...Option) resource.Resource { +func Have(path string, opts ...opt.Option) resource.Resource { f, err := build(path, opts...) if err != nil { log.Fatalf("failed to apply file resource %s: %v", path, err) @@ -245,7 +232,7 @@ func Have(path string, opts ...Option) resource.Resource { return res } -func Absent(path string, opts ...Option) resource.Resource { - opts = append(opts, IsAbsent()) +func Absent(path string, opts ...opt.Option) resource.Resource { + opts = append(opts, opt.IsAbsent()) return Have(path, opts...) } diff --git a/internal/resource/file/file_test.go b/internal/resource/file/file_test.go index b3b7711..49deb4c 100644 --- a/internal/resource/file/file_test.go +++ b/internal/resource/file/file_test.go @@ -5,6 +5,8 @@ import ( "path/filepath" "strings" "testing" + + . "codeberg.org/snonux/gonf/internal/resource/opt" ) func TestGetChecksum(t *testing.T) { |
