-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme_fs.go
More file actions
70 lines (60 loc) · 1.26 KB
/
theme_fs.go
File metadata and controls
70 lines (60 loc) · 1.26 KB
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
package wpry
import (
"context"
"errors"
"fmt"
"io/fs"
"strings"
)
// ParseThemeFS scans the main Stylesheet (style.css) under fsys (style.css) and
// attempts to parse its headers. If successful, a [Theme] struct and its path
// are returned. Otherwise, it returns an error.
func ParseThemeFS(ctx context.Context, fsys fs.FS) (Theme, string, error) {
type result struct {
theme Theme
path string
err error
}
out := make(chan result, 1)
go func() {
defer close(out)
ents, err := fs.ReadDir(fsys, ".")
if err != nil {
out <- result{err: fmt.Errorf("reading directory: %v", err)}
return
}
var name string
for _, ent := range ents {
if ent.IsDir() {
continue
}
if strings.EqualFold(ent.Name(), "style.css") {
name = ent.Name()
break
}
}
if name == "" {
out <- result{err: errors.New("style.css not found")}
return
}
f, err := fsys.Open(name)
if err != nil {
out <- result{err: fmt.Errorf("opening %s: %v", name, err)}
return
}
defer f.Close()
t, err := ParseTheme(f)
if err != nil {
out <- result{err: err}
return
}
out <- result{theme: t, path: name}
}()
select {
case r := <-out:
return r.theme, r.path, r.err
case <-ctx.Done():
var zero Theme
return zero, "", ctx.Err()
}
}