aboutsummaryrefslogtreecommitdiff
path: root/page/checkup.go
blob: c6ca4479ec346e12d8afa308de8d20e177cafafd (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
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
package page

import (
	"fmt"
	"io/fs"
	"path/filepath"
	"reflect"
	"strings"
)

// Checkup will return a map[string]PageList of all the pages broken down by
// the status of their fields. For instance, whehter or not they have a date.
func (p *Page) Checkup() (map[string]PageList, error) {
	Logger.Println("Checking up on all files...")

	out := make(map[string]PageList)

	_ = filepath.Walk(".",
		func(path string, info fs.FileInfo, err error) error {
			if err != nil {
				return err
			}

			if !info.IsDir() && strings.HasSuffix(info.Name(), Suffix) { //nolint:nestif
				p2 := NewPage(strings.ReplaceAll(path, Suffix, ""))
				err = p2.Read()

				if err != nil {
					Logger.Println("Error encountered: ", err)

					return err
				}

				r := reflect.ValueOf(*p2)

				for i := 0; i < r.NumField(); i++ {
					key := ""
					f := r.Field(i)
					fi := r.Type().Field(i)

					if fi.PkgPath != "" {
						continue
					}

					name := fi.Name

					switch f.Interface().(type) {
					case string:
						if f.String() == "" {
							key = fmt.Sprintf("Empty \"%s\"", name)
						} else {
							key = fmt.Sprintf("Not Empty \"%s\"", name)
						}
					case bool:
						if !f.Bool() {
							key = fmt.Sprintf("\"%s\" is false", name)
						} else {
							key = fmt.Sprintf("\"%s\" is true", name)
						}
					case map[string]interface{}:
					case *PageTime:
						if f.IsNil() {
							key = fmt.Sprintf("\"%s\" is empty", name)
						}
					}

					if key == "" {
						continue
					}

					if _, ok := out[key]; !ok {
						out[key] = []*Page{p2}
					} else {
						out[key] = append(out[key], p2)
					}
				}
			}

			return nil
		})

	return out, nil
}