summaryrefslogtreecommitdiffstats
path: root/internal/cms/status.go
blob: 8147ee752a6d933112665498ebbebf43dbe9d3f1 (plain) (blame)
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
package cms

import (
	"os"
	"path/filepath"
)

func Status(root string) (StatusReport, error) {
	project, err := LoadProject(root)
	if err != nil {
		return StatusReport{}, err
	}
	check, err := Check(root)
	if err != nil {
		return StatusReport{}, err
	}
	report := StatusReport{
		Root:        root,
		Config:      project.Config,
		Menus:       len(project.Menus),
		Sections:    len(project.Sections),
		Widgets:     len(project.Widgets),
		PublicFiles: countPublicFiles(filepath.Join(root, "public")),
		Issues:      check.Issues,
	}
	for _, doc := range project.Docs {
		switch doc.Type {
		case "page":
			report.Pages++
		case "article":
			report.Articles++
		}
		if doc.Draft {
			report.Drafts++
		}
	}
	return report, nil
}

func countPublicFiles(root string) int {
	count := 0
	filepath.WalkDir(root, func(_ string, entry os.DirEntry, err error) error {
		if err != nil || entry.IsDir() {
			return nil
		}
		count++
		return nil
	})
	return count
}