summaryrefslogtreecommitdiff
path: root/api/api.go
blob: baa7cc7a4cdac66860ecb4fa83aad948994b7bfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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"
)

// Path constraint for resources that can be defined as a single item or a list.
type Path interface {
	string | []string
}

// 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")
	}
}

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)
}

// 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)...)
}

// 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")
	}
}

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)
}

// 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)...)
}

// 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)...)
}